Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import whisper
|
| 4 |
-
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
api_key=os.getenv("
|
|
|
|
| 9 |
)
|
| 10 |
|
| 11 |
# Load Whisper model
|
| 12 |
speech_model = whisper.load_model("tiny")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
ai_model = genai.GenerativeModel(
|
| 16 |
-
"gemini-2.0-flash"
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Main function
|
| 20 |
def voice_assistant(audio):
|
| 21 |
|
| 22 |
-
# Check if audio exists
|
| 23 |
if audio is None:
|
| 24 |
-
return "Please record
|
| 25 |
|
| 26 |
try:
|
| 27 |
# Convert speech to text
|
|
@@ -29,10 +24,20 @@ def voice_assistant(audio):
|
|
| 29 |
|
| 30 |
user_text = result["text"]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
return f"You said: {user_text}\n\nAI: {
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error: {str(e)}"
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import whisper
|
| 4 |
+
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# OpenRouter client
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
api_key=os.getenv("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"),
|
| 9 |
+
base_url="https://openrouter.ai/api/v1"
|
| 10 |
)
|
| 11 |
|
| 12 |
# Load Whisper model
|
| 13 |
speech_model = whisper.load_model("tiny")
|
| 14 |
|
| 15 |
+
# Voice assistant function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def voice_assistant(audio):
|
| 17 |
|
|
|
|
| 18 |
if audio is None:
|
| 19 |
+
return "Please record audio first."
|
| 20 |
|
| 21 |
try:
|
| 22 |
# Convert speech to text
|
|
|
|
| 24 |
|
| 25 |
user_text = result["text"]
|
| 26 |
|
| 27 |
+
# AI response from OpenRouter
|
| 28 |
+
completion = client.chat.completions.create(
|
| 29 |
+
model="openai/gpt-oss-120b:free",
|
| 30 |
+
messages=[
|
| 31 |
+
{
|
| 32 |
+
"role": "user",
|
| 33 |
+
"content": user_text
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
ai_reply = completion.choices[0].message.content
|
| 39 |
|
| 40 |
+
return f"You said: {user_text}\n\nAI: {ai_reply}"
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error: {str(e)}"
|