|
import gradio as gr |
|
import os |
|
from moviepy.editor import VideoFileClip |
|
from transformers import pipeline |
|
|
|
|
|
asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en") |
|
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") |
|
|
|
stored_transcript = "" |
|
|
|
def transcribe_and_summarize(video_file): |
|
global stored_transcript |
|
|
|
if video_file is None: |
|
return "Error: No file provided.", "" |
|
|
|
try: |
|
video = VideoFileClip(video_file) |
|
audio_path = "temp_audio.wav" |
|
video.audio.write_audiofile(audio_path, codec='pcm_s16le') |
|
|
|
transcription_result = asr(audio_path, return_timestamps=True) |
|
transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']]) |
|
stored_transcript = transcribed_text |
|
|
|
if len(transcribed_text.split()) < 50: |
|
summarized_text = "Text too short to summarize." |
|
else: |
|
summary_result = summarizer(transcribed_text, max_length=500, min_length=100, do_sample=False) |
|
summarized_text = summary_result[0]['summary_text'] |
|
|
|
return transcribed_text, summarized_text |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}", "" |
|
|
|
def answer_question(question): |
|
global stored_transcript |
|
if not stored_transcript: |
|
return "Please transcribe a video first." |
|
result = qa_pipeline(question=question, context=stored_transcript) |
|
return result['answer'] |
|
|
|
with gr.Blocks(css=""" |
|
body { background-color: black !important; } |
|
.gradio-container { color: #FFFF33 !important; } |
|
button { background-color: #FFFF33 !important; color: black !important; border: none !important; } |
|
input, textarea, .gr-textbox, .gr-video { background-color: #111 !important; color: #FFFF33 !important; border-color: #FFFF33 !important; } |
|
""") as iface: |
|
gr.HTML("<h1 style='color:#FFFF33'>π₯ Video Transcriber, Summarizer & Q&A Tool</h1>") |
|
gr.HTML("<p style='color:#CCCC33'>Upload a video to get a transcript, summary, and ask questions about its content.</p>") |
|
|
|
with gr.Tab("π Transcription & Summary"): |
|
video_input = gr.Video(label="Upload Video (.mp4)", interactive=True) |
|
transcribe_btn = gr.Button("π Transcribe and Summarize") |
|
transcribed_text = gr.Textbox(label="Transcribed Text", lines=8, interactive=False) |
|
summarized_text = gr.Textbox(label="Summarized Text", lines=8, interactive=False) |
|
|
|
transcribe_btn.click(fn=transcribe_and_summarize, inputs=video_input, outputs=[transcribed_text, summarized_text]) |
|
|
|
with gr.Tab("β Ask Questions"): |
|
question_input = gr.Textbox(label="Ask a question based on the transcript", placeholder="E.g., What is the main topic?") |
|
ask_btn = gr.Button("π Get Answer") |
|
answer_output = gr.Textbox(label="Answer", interactive=False) |
|
|
|
ask_btn.click(fn=answer_question, inputs=question_input, outputs=answer_output) |
|
|
|
|
|
port = int(os.environ.get('PORT1', 7860)) |
|
iface.launch(share=True, server_port=port) |
|
|