Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import whisper
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import concurrent.futures
|
| 6 |
+
|
| 7 |
+
# β
Load models once (Prevents reloading in every function call)
|
| 8 |
+
print("Loading models...")
|
| 9 |
+
whisper_model = whisper.load_model("small")
|
| 10 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 11 |
+
question_generator = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 12 |
+
print("Models loaded successfully!")
|
| 13 |
+
|
| 14 |
+
def transcribe_audio(audio_path):
|
| 15 |
+
result = whisper_model.transcribe(audio_path)
|
| 16 |
+
return result["text"]
|
| 17 |
+
|
| 18 |
+
def summarize_text(text):
|
| 19 |
+
text_chunks = [text[i:i+1024] for i in range(0, len(text), 1024)]
|
| 20 |
+
summaries = summarizer(text_chunks, max_length=200, min_length=50, do_sample=False)
|
| 21 |
+
return " ".join([s['summary_text'] for s in summaries])
|
| 22 |
+
|
| 23 |
+
def generate_questions(text):
|
| 24 |
+
text_chunks = [text[i:i+1024] for i in range(0, len(text), 1024)]
|
| 25 |
+
questions = []
|
| 26 |
+
|
| 27 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 28 |
+
future_questions = [
|
| 29 |
+
executor.submit(
|
| 30 |
+
lambda chunk: question_generator(
|
| 31 |
+
f"Generate some meaningful questions based on the topic from following passage: {chunk}",
|
| 32 |
+
max_length=100, num_return_sequences=3, do_sample=True
|
| 33 |
+
),
|
| 34 |
+
chunk
|
| 35 |
+
) for chunk in text_chunks
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
for future in future_questions:
|
| 39 |
+
generated = future.result()
|
| 40 |
+
questions.extend([q['generated_text'] for q in generated])
|
| 41 |
+
|
| 42 |
+
return "\n".join(questions)
|
| 43 |
+
|
| 44 |
+
def process_audio(audio_path):
|
| 45 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 46 |
+
transcribe_future = executor.submit(transcribe_audio, audio_path)
|
| 47 |
+
transcript = transcribe_future.result()
|
| 48 |
+
|
| 49 |
+
summarize_future = executor.submit(summarize_text, transcript)
|
| 50 |
+
questions_future = executor.submit(generate_questions, transcript)
|
| 51 |
+
|
| 52 |
+
summary = summarize_future.result()
|
| 53 |
+
questions = questions_future.result()
|
| 54 |
+
|
| 55 |
+
combined_text = f"π Transcription:\n{transcript}\n\nπ Summary:\n{summary}\n\nπ€ Practice Questions:\n{questions}"
|
| 56 |
+
file_path = "lecture_summary.txt"
|
| 57 |
+
|
| 58 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 59 |
+
f.write(combined_text)
|
| 60 |
+
|
| 61 |
+
return transcript, summary, questions, file_path
|
| 62 |
+
|
| 63 |
+
def gradio_interface(audio):
|
| 64 |
+
return process_audio(audio)
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("# π LectureGenie: Transcribe, Summarize & Quiz")
|
| 68 |
+
gr.Markdown("Upload a lecture audio file. The system will **transcribe**, **summarize**, and **generate questions** automatically.")
|
| 69 |
+
|
| 70 |
+
audio_input = gr.Audio(type="filepath", label="π€ Upload Audio File", interactive=True, elem_classes="small-audio-box")
|
| 71 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
with gr.Column():
|
| 75 |
+
transcript_box = gr.Textbox(label="π Transcription", lines=10, interactive=False, show_copy_button=True)
|
| 76 |
+
|
| 77 |
+
with gr.Column():
|
| 78 |
+
summary_box = gr.Textbox(label="π Summary", lines=10, interactive=False, show_copy_button=True)
|
| 79 |
+
|
| 80 |
+
with gr.Column():
|
| 81 |
+
questions_box = gr.Textbox(label="π€ Practice Questions", lines=10, interactive=False, show_copy_button=True)
|
| 82 |
+
|
| 83 |
+
download_btn = gr.File(label="π₯ Download All", interactive=False, visible=False)
|
| 84 |
+
download_button = gr.Button("π₯ Download", elem_id="download-btn")
|
| 85 |
+
|
| 86 |
+
submit_btn.click(
|
| 87 |
+
gradio_interface,
|
| 88 |
+
inputs=[audio_input],
|
| 89 |
+
outputs=[transcript_box, summary_box, questions_box, download_btn]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
download_button.click(lambda x: x, inputs=[download_btn], outputs=[download_btn])
|
| 93 |
+
|
| 94 |
+
demo.launch()
|