Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
import nltk
|
| 6 |
+
from nltk.stem import PorterStemmer
|
| 7 |
+
from nltk.tokenize import word_tokenize
|
| 8 |
+
from moviepy.editor import VideoFileClip
|
| 9 |
+
from pytesseract import image_to_string
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import cv2
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
import concurrent.futures
|
| 14 |
+
|
| 15 |
+
# Downloads
|
| 16 |
+
nltk.download('punkt')
|
| 17 |
+
nltk.download('averaged_perceptron_tagger')
|
| 18 |
+
|
| 19 |
+
# Use faster summarization model
|
| 20 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 21 |
+
|
| 22 |
+
# Functions
|
| 23 |
+
def extract_audio(video_path):
|
| 24 |
+
video = VideoFileClip(video_path)
|
| 25 |
+
audio_path = "extracted_audio.wav"
|
| 26 |
+
video.audio.write_audiofile(audio_path, verbose=False, logger=None)
|
| 27 |
+
return audio_path
|
| 28 |
+
|
| 29 |
+
def transcribe_audio(audio_path):
|
| 30 |
+
recognizer = sr.Recognizer()
|
| 31 |
+
with sr.AudioFile(audio_path) as source:
|
| 32 |
+
audio = recognizer.record(source, duration=30) # limit to 30s
|
| 33 |
+
return recognizer.recognize_google(audio)
|
| 34 |
+
|
| 35 |
+
def extract_keywords(text):
|
| 36 |
+
tokens = word_tokenize(text)
|
| 37 |
+
pos_tags = nltk.pos_tag(tokens)
|
| 38 |
+
stemmer = PorterStemmer()
|
| 39 |
+
return list(set(f"{stemmer.stem(w.lower())} ({t})" for w, t in pos_tags if t.startswith("NN") or t.startswith("VB")))
|
| 40 |
+
|
| 41 |
+
def summarize_text(text, ratio="short"):
|
| 42 |
+
max_len, min_len = (100, 30) if ratio == "short" else (150, 50) if ratio == "medium" else (250, 80)
|
| 43 |
+
if len(text.split()) < min_len:
|
| 44 |
+
return "Transcript is too short to summarize."
|
| 45 |
+
chunks = [text[i:i+1000] for i in range(0, len(text), 1000)]
|
| 46 |
+
summary = ""
|
| 47 |
+
for chunk in chunks:
|
| 48 |
+
sum_out = summarizer(chunk, max_length=max_len, min_length=min_len, do_sample=False)
|
| 49 |
+
summary += sum_out[0]['summary_text'] + " "
|
| 50 |
+
return summary.strip()
|
| 51 |
+
|
| 52 |
+
def extract_slide_text(video_path):
|
| 53 |
+
cap = cv2.VideoCapture(video_path)
|
| 54 |
+
frame_count = 0
|
| 55 |
+
ocr_texts = set()
|
| 56 |
+
while cap.isOpened() and frame_count < 20:
|
| 57 |
+
ret, frame = cap.read()
|
| 58 |
+
if not ret:
|
| 59 |
+
break
|
| 60 |
+
if frame_count % 30 == 0:
|
| 61 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 62 |
+
text = image_to_string(image)
|
| 63 |
+
if text.strip():
|
| 64 |
+
ocr_texts.add(text.strip())
|
| 65 |
+
frame_count += 1
|
| 66 |
+
cap.release()
|
| 67 |
+
return "\n\n".join(ocr_texts)
|
| 68 |
+
|
| 69 |
+
# Gradio UI
|
| 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
|
| 74 |
+
|
| 75 |
+
audio_path = file_path
|
| 76 |
+
slide_text = ""
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
if file_path.lower().endswith((".mp4", ".mov", ".avi", ".mkv")):
|
| 80 |
+
audio_path = extract_audio(file_path)
|
| 81 |
+
|
| 82 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 83 |
+
# Running OCR and transcription in parallel
|
| 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 |
+
results = {}
|
| 91 |
+
|
| 92 |
+
if slide_text:
|
| 93 |
+
results["slide_text"] = slide_text
|
| 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 |
+
return results
|
| 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 |
+
|