Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ nltk.download('averaged_perceptron_tagger')
|
|
| 19 |
# Use faster summarization model
|
| 20 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def extract_audio(video_path):
|
| 24 |
video = VideoFileClip(video_path)
|
| 25 |
audio_path = "extracted_audio.wav"
|
|
@@ -66,8 +66,7 @@ def extract_slide_text(video_path):
|
|
| 66 |
cap.release()
|
| 67 |
return "\n\n".join(ocr_texts)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
def process_file(uploaded_file):
|
| 71 |
with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as temp_file:
|
| 72 |
temp_file.write(uploaded_file.read())
|
| 73 |
file_path = temp_file.name
|
|
@@ -80,37 +79,44 @@ def process_file(uploaded_file):
|
|
| 80 |
audio_path = extract_audio(file_path)
|
| 81 |
|
| 82 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 83 |
-
|
| 84 |
ocr_future = executor.submit(extract_slide_text, file_path) if file_path.endswith((".mp4", ".mov", ".avi", ".mkv")) else None
|
| 85 |
trans_future = executor.submit(transcribe_audio, audio_path)
|
| 86 |
|
| 87 |
transcript = trans_future.result()
|
| 88 |
slide_text = ocr_future.result() if ocr_future else ""
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
results["transcript"] = transcript
|
| 96 |
-
results["keywords"] = extract_keywords(transcript)
|
| 97 |
-
summary_mode = "short"
|
| 98 |
-
results["summary"] = summarize_text(transcript, ratio=summary_mode)
|
| 99 |
|
|
|
|
| 100 |
os.remove(file_path)
|
| 101 |
if audio_path != file_path and os.path.exists(audio_path):
|
| 102 |
os.remove(audio_path)
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# Gradio Interface
|
| 107 |
inputs = gr.File(label="Upload Audio/Video File (Any Format)", type="file")
|
| 108 |
-
outputs = [
|
| 109 |
-
gr.Textbox(label="Full Transcription", lines=10),
|
| 110 |
-
gr.Textbox(label="Keywords", lines=2),
|
| 111 |
-
gr.Textbox(label="Lecture Summary", lines=10),
|
| 112 |
-
gr.Textbox(label="Slide/Whiteboard Text", lines=10)
|
| 113 |
-
]
|
| 114 |
-
|
| 115 |
-
gr.Interface(fn=process_file, inputs=inputs, outputs=outputs, live=True).launch()
|
| 116 |
|
|
|
|
|
|
| 19 |
# Use faster summarization model
|
| 20 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 21 |
|
| 22 |
+
# Gradio interface
|
| 23 |
def extract_audio(video_path):
|
| 24 |
video = VideoFileClip(video_path)
|
| 25 |
audio_path = "extracted_audio.wav"
|
|
|
|
| 66 |
cap.release()
|
| 67 |
return "\n\n".join(ocr_texts)
|
| 68 |
|
| 69 |
+
def process_uploaded_file(uploaded_file):
|
|
|
|
| 70 |
with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as temp_file:
|
| 71 |
temp_file.write(uploaded_file.read())
|
| 72 |
file_path = temp_file.name
|
|
|
|
| 79 |
audio_path = extract_audio(file_path)
|
| 80 |
|
| 81 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 82 |
+
st.info("๐ Running OCR and transcription in parallel...")
|
| 83 |
ocr_future = executor.submit(extract_slide_text, file_path) if file_path.endswith((".mp4", ".mov", ".avi", ".mkv")) else None
|
| 84 |
trans_future = executor.submit(transcribe_audio, audio_path)
|
| 85 |
|
| 86 |
transcript = trans_future.result()
|
| 87 |
slide_text = ocr_future.result() if ocr_future else ""
|
| 88 |
|
| 89 |
+
return transcript, slide_text
|
| 90 |
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"๐ซ Error: {e}", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
finally:
|
| 95 |
os.remove(file_path)
|
| 96 |
if audio_path != file_path and os.path.exists(audio_path):
|
| 97 |
os.remove(audio_path)
|
| 98 |
|
| 99 |
+
# Gradio Interface for input and output
|
| 100 |
+
def generate_notes(uploaded_file):
|
| 101 |
+
transcript, slide_text = process_uploaded_file(uploaded_file)
|
| 102 |
+
|
| 103 |
+
if slide_text:
|
| 104 |
+
slide_text_display = f"๐ผ๏ธ Slide/Whiteboard Extracted Text: \n{slide_text}"
|
| 105 |
+
else:
|
| 106 |
+
slide_text_display = "No slide/whiteboard text extracted."
|
| 107 |
+
|
| 108 |
+
if len(transcript.split()) < 30:
|
| 109 |
+
transcript_display = "Transcript too short for a meaningful summary."
|
| 110 |
+
else:
|
| 111 |
+
summary_mode = "short" # Default summary mode
|
| 112 |
+
summary = summarize_text(transcript, ratio=summary_mode)
|
| 113 |
+
transcript_display = f"๐ Full Transcription: \n{transcript}\n\n๐ Lecture Summary: \n{summary}"
|
| 114 |
+
|
| 115 |
+
return slide_text_display, transcript_display
|
| 116 |
+
|
| 117 |
|
| 118 |
# Gradio Interface
|
| 119 |
inputs = gr.File(label="Upload Audio/Video File (Any Format)", type="file")
|
| 120 |
+
outputs = [gr.Textbox(label="Slide Text"), gr.Textbox(label="Lecture Transcript and Summary")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
gr.Interface(fn=generate_notes, inputs=inputs, outputs=outputs, live=True).launch()
|