Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,61 +3,112 @@ import whisper
|
|
| 3 |
from transformers import pipeline
|
| 4 |
import gradio as gr
|
| 5 |
import concurrent.futures
|
|
|
|
| 6 |
|
| 7 |
-
|
| 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 |
print("Transcribing audio...")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def summarize_text(text):
|
|
|
|
|
|
|
|
|
|
| 20 |
print("Summarizing text using BART...")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
def generate_questions(text):
|
|
|
|
|
|
|
|
|
|
| 26 |
print("Generating questions using FLAN-T5...")
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
questions = []
|
| 29 |
-
|
| 30 |
-
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 31 |
future_questions = [
|
| 32 |
executor.submit(
|
| 33 |
lambda chunk: question_generator(
|
| 34 |
-
f"You are an AI tutor. Your task is to generate **insightful, topic-specific** questions based on the following passage. Ensure that the questions are relevant to the **key concepts, definitions, and explanations** present in the text. Avoid generic questions.\n\nPassage:\n{chunk}",
|
| 35 |
-
max_length=
|
| 36 |
),
|
| 37 |
chunk
|
| 38 |
) for chunk in text_chunks
|
| 39 |
]
|
| 40 |
-
|
| 41 |
for future in future_questions:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return "\n".join(questions)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
def process_audio(audio_path):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
summarize_future = executor.submit(summarize_text, transcript)
|
| 53 |
-
questions_future = executor.submit(generate_questions, transcript)
|
| 54 |
-
|
| 55 |
-
summary = summarize_future.result()
|
| 56 |
-
questions = questions_future.result()
|
| 57 |
-
|
| 58 |
combined_text = f"📝 Transcription:\n{transcript}\n\n📜 Summary:\n{summary}\n\n🤔 Practice Questions:\n{questions}"
|
| 59 |
file_path = "lecture_summary.txt"
|
| 60 |
-
|
| 61 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 62 |
f.write(combined_text)
|
| 63 |
|
|
@@ -66,6 +117,9 @@ def process_audio(audio_path):
|
|
| 66 |
def gradio_interface(audio):
|
| 67 |
return process_audio(audio)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
with gr.Blocks(css="""
|
| 70 |
#submit-btn, #download-btn {
|
| 71 |
background-color: blue !important;
|
|
@@ -106,4 +160,4 @@ with gr.Blocks(css="""
|
|
| 106 |
|
| 107 |
download_button.click(lambda x: x, inputs=[download_btn], outputs=[download_btn])
|
| 108 |
|
| 109 |
-
demo.launch(share=True)
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
import gradio as gr
|
| 5 |
import concurrent.futures
|
| 6 |
+
import os # For environment variables
|
| 7 |
|
| 8 |
+
print("Starting up...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# *** Model Loading - CPU Optimized & Size Considerations ***
|
| 11 |
+
try:
|
| 12 |
+
# Option 1: Try "tiny" model. Significantly faster on CPU, but lower accuracy.
|
| 13 |
+
whisper_model = whisper.load_model("tiny")
|
| 14 |
+
print("Using whisper 'tiny' model.")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error loading whisper 'tiny' model: {e}. Trying 'small'.")
|
| 17 |
+
try:
|
| 18 |
+
whisper_model = whisper.load_model("small")
|
| 19 |
+
print("Using whisper 'small' model.")
|
| 20 |
+
except Exception as e2:
|
| 21 |
+
print(f"Error loading whisper 'small' model: {e2}. Whisper will not work.")
|
| 22 |
+
whisper_model = None # Disable whisper functionality
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=-1) # device=-1 forces CPU
|
| 26 |
+
question_generator = pipeline("text2text-generation", model="google/flan-t5-large", device=-1) # device=-1 forces CPU
|
| 27 |
+
print("Summarizer and Question Generator loaded successfully.")
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error loading Summarizer or Question Generator: {e}")
|
| 31 |
+
summarizer = None
|
| 32 |
+
question_generator = None
|
| 33 |
+
print("Summarization and Question Generation will not work.")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
print("Models loaded (or failed gracefully).")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# *** Transcription ***
|
| 40 |
def transcribe_audio(audio_path):
|
| 41 |
print("Transcribing audio...")
|
| 42 |
+
if whisper_model is None:
|
| 43 |
+
return "Error: Whisper model failed to load."
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
result = whisper_model.transcribe(audio_path)
|
| 47 |
+
return result["text"]
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error transcribing audio: {e}")
|
| 50 |
+
return f"Error during transcription: {e}"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# *** Summarization ***
|
| 54 |
def summarize_text(text):
|
| 55 |
+
if summarizer is None:
|
| 56 |
+
return "Error: Summarizer model failed to load."
|
| 57 |
+
|
| 58 |
print("Summarizing text using BART...")
|
| 59 |
+
# Chunk the text into smaller parts, even smaller than before for CPU
|
| 60 |
+
text_chunks = [text[i:i + 768] for i in range(0, len(text), 768)] # More aggressive chunking
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
summaries = summarizer(text_chunks, max_length=150, min_length=30, do_sample=False) # Reduce length
|
| 64 |
+
return " ".join([s['summary_text'] for s in summaries])
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error during summarization: {e}")
|
| 67 |
+
return f"Error during summarization: {e}"
|
| 68 |
+
|
| 69 |
|
| 70 |
+
# *** Question Generation ***
|
| 71 |
def generate_questions(text):
|
| 72 |
+
if question_generator is None:
|
| 73 |
+
return "Error: Question Generation model failed to load."
|
| 74 |
+
|
| 75 |
print("Generating questions using FLAN-T5...")
|
| 76 |
+
# Even smaller chunks for question generation (CPU is struggling)
|
| 77 |
+
text_chunks = [text[i:i + 512] for i in range(0, len(text), 512)]
|
| 78 |
+
|
| 79 |
questions = []
|
| 80 |
+
|
| 81 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor: # Explicitly limit threads
|
| 82 |
future_questions = [
|
| 83 |
executor.submit(
|
| 84 |
lambda chunk: question_generator(
|
| 85 |
+
f"You are an AI tutor. Your task is to generate **insightful, topic-specific** questions based on the following passage. Ensure that the questions are relevant to the **key concepts, definitions, and explanations** present in the text. Avoid generic questions.\n\nPassage:\n{chunk}",
|
| 86 |
+
max_length=80, num_return_sequences=2, do_sample=True # Reduce length and sequences
|
| 87 |
),
|
| 88 |
chunk
|
| 89 |
) for chunk in text_chunks
|
| 90 |
]
|
| 91 |
+
|
| 92 |
for future in future_questions:
|
| 93 |
+
try:
|
| 94 |
+
generated = future.result()
|
| 95 |
+
questions.extend([q['generated_text'] for q in generated])
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Error generating questions for a chunk: {e}")
|
| 98 |
+
questions.append(f"Error generating questions: {e}") # Report the error
|
| 99 |
+
|
| 100 |
return "\n".join(questions)
|
| 101 |
|
| 102 |
+
|
| 103 |
+
# *** Main Processing Function ***
|
| 104 |
def process_audio(audio_path):
|
| 105 |
+
transcript = transcribe_audio(audio_path)
|
| 106 |
+
summary = summarize_text(transcript)
|
| 107 |
+
questions = generate_questions(transcript)
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
combined_text = f"📝 Transcription:\n{transcript}\n\n📜 Summary:\n{summary}\n\n🤔 Practice Questions:\n{questions}"
|
| 110 |
file_path = "lecture_summary.txt"
|
| 111 |
+
|
| 112 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 113 |
f.write(combined_text)
|
| 114 |
|
|
|
|
| 117 |
def gradio_interface(audio):
|
| 118 |
return process_audio(audio)
|
| 119 |
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# *** Gradio Interface ***
|
| 123 |
with gr.Blocks(css="""
|
| 124 |
#submit-btn, #download-btn {
|
| 125 |
background-color: blue !important;
|
|
|
|
| 160 |
|
| 161 |
download_button.click(lambda x: x, inputs=[download_btn], outputs=[download_btn])
|
| 162 |
|
| 163 |
+
demo.launch(share=True)
|