Spaces:
Sleeping
Sleeping
Create app.py
Browse filesapp file added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122import gradio as grimport numpy as npimport tempfileimport scipy.io.wavfile as wav
# -------------------------------# 1. Load Models (Lightweight)# -------------------------------
# Whisper (Speech-to-Text)from transformers import pipelinestt = pipeline("automatic-speech-recognition", model="openai/whisper-small")
# Simple LLM (text generation)llm = pipeline("text-generation", model="distilgpt2")
# TTS (Coqui TTS)from TTS.api import TTStts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
# -------------------------------# 2. Core Functions# -------------------------------
def speech_to_text(audio): """ Converts speech (audio file) to text using Whisper """ if audio is None: return "No audio provided." sample_rate, data = audio with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: wav.write(tmp.name, sample_rate, data) result = stt(tmp.name) return result["text"]
def generate_response(text): """ Generates tutor-style response using LLM """ if not text or text == "No audio provided.": return "Please provide valid input."
# Simple AI tutor system prompt prompt = f""" You are a helpful AI tutor. Explain clearly, simply, and step-by-step.
Question: {text} Answer: """
output = llm(prompt, max_length=150, num_return_sequences=1) response = output[0]["generated_text"]
# Clean response (remove prompt repetition) return response.split("Answer:")[-1].strip()
def text_to_speech(text): """ Converts text to speech using Coqui TTS """ if not text: return None
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: tts_model.tts_to_file(text=text, file_path=tmp.name) return tmp.name
# -------------------------------# 3. Pipeline Function# -------------------------------
def voice_tutor(audio): """ Full pipeline: Audio β Text β Response β Voice """ transcription = speech_to_text(audio) response = generate_response(transcription) audio_output = text_to_speech(response)
return transcription, response, audio_output
# -------------------------------# 4. Gradio UI# -------------------------------
with gr.Blocks() as demo: gr.Markdown("## π AI Voice Tutor")
audio_input = gr.Audio( sources=["microphone", "upload"], type="numpy", label="Speak or Upload Audio" )
transcription_box = gr.Textbox(label="Transcription") response_box = gr.Textbox(label="Tutor Response")
audio_output = gr.Audio(label="Voice Output")
submit_btn = gr.Button("Generate Response")
submit_btn.click( fn=voice_tutor, inputs=audio_input, outputs=[transcription_box, response_box, audio_output] )
# -------------------------------# 5. Launch# -------------------------------
if __name__ == "__main__": demo.launch()app.pyAdded+122