Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import whisper | |
| from openai import OpenAI | |
| # OpenRouter client | |
| client = OpenAI( | |
| api_key=os.getenv("OPENROUTER_API_KEY"), | |
| base_url="https://openrouter.ai/api/v1" | |
| ) | |
| # Load Whisper model | |
| speech_model = whisper.load_model("tiny") | |
| # Voice assistant function | |
| def voice_assistant(audio): | |
| if audio is None: | |
| return "Please record audio first." | |
| try: | |
| # Convert speech to text | |
| result = speech_model.transcribe(audio) | |
| user_text = result["text"] | |
| # AI response from OpenRouter | |
| completion = client.chat.completions.create( | |
| model="openai/gpt-oss-20b:free", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": user_text | |
| } | |
| ] | |
| ) | |
| ai_reply = completion.choices[0].message.content | |
| return f"You said: {user_text}\n\nAI: {ai_reply}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Gradio UI | |
| interface = gr.Interface( | |
| fn=voice_assistant, | |
| inputs=gr.Audio( | |
| sources=["microphone"], | |
| type="filepath" | |
| ), | |
| outputs="text", | |
| title="AI Voice Assistant", | |
| description="Speak and get AI responses" | |
| ) | |
| interface.launch() |