File size: 1,255 Bytes
401c8ca
d7c0552
 
41e9fec
d7c0552
41e9fec
 
52f745c
41e9fec
d7c0552
 
37ed705
401c8ca
d7c0552
41e9fec
d7c0552
 
37ed705
41e9fec
d7c0552
37ed705
 
 
d7c0552
37ed705
d7c0552
41e9fec
 
3431395
41e9fec
 
 
 
 
 
 
 
 
d7c0552
41e9fec
37ed705
 
 
 
 
d7c0552
 
37ed705
 
 
 
d7c0552
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()