amyakir commited on
Commit
548a255
·
verified ·
1 Parent(s): 19f74b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -7
app.py CHANGED
@@ -1,12 +1,56 @@
1
- from transformers import pipeline
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta")
 
 
5
 
6
- def generate_questions(text):
7
- prompt = f"Ask 5 short, simple comprehension questions about this English coursebook text:\n\n\"{text}\""
8
- response = pipe(prompt, max_new_tokens=200)[0]["generated_text"]
9
- return response
10
 
11
- demo = gr.Interface(fn=generate_questions, inputs="text", outputs="text")
12
  demo.launch()
 
1
+ # app.py
2
  import gradio as gr
3
+ from transformers import pipeline
4
+ import torch
5
+ import tempfile
6
+ import os
7
+ from TTS.api import TTS
8
+ import whisper
9
+
10
+ # Load question-generation pipeline (use a lightweight model)
11
+ qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
12
+
13
+ # Load TTS model
14
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
15
+
16
+ # Load Whisper STT model
17
+ whisper_model = whisper.load_model("base")
18
+
19
+ # Generate question and audio from input text
20
+ def generate_question(text):
21
+ output = qg_pipeline("generate question: " + text, max_length=64, clean_up_tokenization_spaces=True)[0]['generated_text']
22
+
23
+ # Save TTS audio to temp file
24
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
25
+ tts.tts_to_file(text=output, file_path=fp.name)
26
+ audio_path = fp.name
27
+
28
+ return output, audio_path
29
+
30
+ # Transcribe user audio answer
31
+ def transcribe_audio(audio):
32
+ audio = whisper.load_audio(audio)
33
+ audio = whisper.pad_or_trim(audio)
34
+ mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
35
+ options = whisper.DecodingOptions()
36
+ result = whisper.decode(whisper_model, mel, options)
37
+ return result.text
38
+
39
+ # Gradio interface
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("### Voice Q&A Generator")
42
+ with gr.Row():
43
+ input_text = gr.Textbox(label="Coursebook Text")
44
+ generate_btn = gr.Button("Generate Question")
45
+
46
+ question_out = gr.Textbox(label="Generated Question")
47
+ audio_out = gr.Audio(label="AI Question (Audio)", type="filepath")
48
 
49
+ with gr.Row():
50
+ user_audio = gr.Audio(source="microphone", type="filepath", label="Your Answer")
51
+ transcribed_text = gr.Textbox(label="Transcribed Answer")
52
 
53
+ generate_btn.click(fn=generate_question, inputs=input_text, outputs=[question_out, audio_out])
54
+ user_audio.change(fn=transcribe_audio, inputs=user_audio, outputs=transcribed_text)
 
 
55
 
 
56
  demo.launch()