dtkne commited on
Commit
477c4c7
Β·
verified Β·
1 Parent(s): c8a13a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -19
app.py CHANGED
@@ -10,26 +10,46 @@ qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distil
10
 
11
  stored_transcript = ""
12
 
13
- def transcribe_and_summarize(video_file):
14
  global stored_transcript
15
-
16
  if video_file is None:
17
- return "Error: No file provided.", ""
18
-
19
  try:
20
  video = VideoFileClip(video_file)
21
  audio_path = "temp_audio.wav"
22
  video.audio.write_audiofile(audio_path, codec='pcm_s16le')
23
 
24
  transcription_result = asr(audio_path, return_timestamps=True)
25
- transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  stored_transcript = transcribed_text
27
 
28
  if len(transcribed_text.split()) < 50:
29
  summarized_text = "Text too short to summarize."
30
  else:
31
- summary_result = summarizer(transcribed_text, max_length=500, min_length=100, do_sample=False)
32
- summarized_text = summary_result[0]['summary_text']
33
 
34
  return transcribed_text, summarized_text
35
 
@@ -39,34 +59,43 @@ def transcribe_and_summarize(video_file):
39
  def answer_question(question):
40
  global stored_transcript
41
  if not stored_transcript:
42
- return "Please transcribe a video first."
43
  result = qa_pipeline(question=question, context=stored_transcript)
44
- return result['answer']
45
 
 
46
  with gr.Blocks(css="""
47
  body { background-color: black !important; }
48
  .gradio-container { color: #FFFF33 !important; }
49
  button { background-color: #FFFF33 !important; color: black !important; border: none !important; }
50
- input, textarea, .gr-textbox, .gr-video { background-color: #111 !important; color: #FFFF33 !important; border-color: #FFFF33 !important; }
51
  """) as iface:
52
- gr.HTML("<h1 style='color:#FFFF33'>πŸŽ₯ Video Transcriber, Summarizer & Q&A Tool</h1>")
53
- gr.HTML("<p style='color:#CCCC33'>Upload a video to get a transcript, summary, and ask questions about its content.</p>")
54
 
55
- with gr.Tab("πŸ“ Transcription & Summary"):
56
  video_input = gr.Video(label="Upload Video (.mp4)", interactive=True)
57
- transcribe_btn = gr.Button("πŸš€ Transcribe and Summarize")
58
- transcribed_text = gr.Textbox(label="Transcribed Text", lines=8, interactive=False)
59
- summarized_text = gr.Textbox(label="Summarized Text", lines=8, interactive=False)
 
 
 
 
 
 
 
 
60
 
61
- transcribe_btn.click(fn=transcribe_and_summarize, inputs=video_input, outputs=[transcribed_text, summarized_text])
62
 
63
  with gr.Tab("❓ Ask Questions"):
64
- question_input = gr.Textbox(label="Ask a question based on the transcript", placeholder="E.g., What is the main topic?")
65
  ask_btn = gr.Button("πŸ” Get Answer")
66
  answer_output = gr.Textbox(label="Answer", interactive=False)
67
 
68
  ask_btn.click(fn=answer_question, inputs=question_input, outputs=answer_output)
69
 
70
- # Launch app
71
  port = int(os.environ.get('PORT1', 7860))
72
  iface.launch(share=True, server_port=port)
 
10
 
11
  stored_transcript = ""
12
 
13
+ def transcribe_from_video(video_file):
14
  global stored_transcript
 
15
  if video_file is None:
16
+ return "Error: No video file provided.", ""
17
+
18
  try:
19
  video = VideoFileClip(video_file)
20
  audio_path = "temp_audio.wav"
21
  video.audio.write_audiofile(audio_path, codec='pcm_s16le')
22
 
23
  transcription_result = asr(audio_path, return_timestamps=True)
24
+ transcribed_text = " ".join([chunk["text"] for chunk in transcription_result["chunks"]])
25
+ stored_transcript = transcribed_text
26
+
27
+ if len(transcribed_text.split()) < 50:
28
+ summarized_text = "Text too short to summarize."
29
+ else:
30
+ summary = summarizer(transcribed_text, max_length=500, min_length=100, do_sample=False)
31
+ summarized_text = summary[0]["summary_text"]
32
+
33
+ return transcribed_text, summarized_text
34
+
35
+ except Exception as e:
36
+ return f"Error: {str(e)}", ""
37
+
38
+ def transcribe_from_audio(audio_file):
39
+ global stored_transcript
40
+ if audio_file is None:
41
+ return "Error: No audio recorded.", ""
42
+
43
+ try:
44
+ transcription_result = asr(audio_file, return_timestamps=True)
45
+ transcribed_text = " ".join([chunk["text"] for chunk in transcription_result["chunks"]])
46
  stored_transcript = transcribed_text
47
 
48
  if len(transcribed_text.split()) < 50:
49
  summarized_text = "Text too short to summarize."
50
  else:
51
+ summary = summarizer(transcribed_text, max_length=500, min_length=100, do_sample=False)
52
+ summarized_text = summary[0]["summary_text"]
53
 
54
  return transcribed_text, summarized_text
55
 
 
59
  def answer_question(question):
60
  global stored_transcript
61
  if not stored_transcript:
62
+ return "Please transcribe a video or record audio first."
63
  result = qa_pipeline(question=question, context=stored_transcript)
64
+ return result["answer"]
65
 
66
+ # UI
67
  with gr.Blocks(css="""
68
  body { background-color: black !important; }
69
  .gradio-container { color: #FFFF33 !important; }
70
  button { background-color: #FFFF33 !important; color: black !important; border: none !important; }
71
+ input, textarea, .gr-textbox, .gr-video, .gr-audio { background-color: #111 !important; color: #FFFF33 !important; border-color: #FFFF33 !important; }
72
  """) as iface:
73
+ gr.HTML("<h1 style='color:#FFFF33'>🎀 Video & Voice Transcriber, Summarizer & Q&A</h1>")
74
+ gr.HTML("<p style='color:#CCCC33'>Upload a video or record speech to get transcript, summary, and ask questions.</p>")
75
 
76
+ with gr.Tab("πŸŽ₯ Video Upload"):
77
  video_input = gr.Video(label="Upload Video (.mp4)", interactive=True)
78
+ transcribe_btn = gr.Button("πŸš€ Transcribe from Video")
79
+ transcribed_text_v = gr.Textbox(label="Transcribed Text", lines=8, interactive=False)
80
+ summarized_text_v = gr.Textbox(label="Summarized Text", lines=8, interactive=False)
81
+
82
+ transcribe_btn.click(fn=transcribe_from_video, inputs=video_input, outputs=[transcribed_text_v, summarized_text_v])
83
+
84
+ with gr.Tab("πŸŽ™οΈ Record Speech"):
85
+ audio_input = gr.Audio(source="microphone", type="filepath", label="Record Audio")
86
+ record_btn = gr.Button("🎧 Transcribe from Audio")
87
+ transcribed_text_a = gr.Textbox(label="Transcribed Text", lines=8, interactive=False)
88
+ summarized_text_a = gr.Textbox(label="Summarized Text", lines=8, interactive=False)
89
 
90
+ record_btn.click(fn=transcribe_from_audio, inputs=audio_input, outputs=[transcribed_text_a, summarized_text_a])
91
 
92
  with gr.Tab("❓ Ask Questions"):
93
+ question_input = gr.Textbox(label="Ask a question about the transcript", placeholder="E.g., What was the main topic?")
94
  ask_btn = gr.Button("πŸ” Get Answer")
95
  answer_output = gr.Textbox(label="Answer", interactive=False)
96
 
97
  ask_btn.click(fn=answer_question, inputs=question_input, outputs=answer_output)
98
 
99
+ # Launch
100
  port = int(os.environ.get('PORT1', 7860))
101
  iface.launch(share=True, server_port=port)