Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,15 @@ import gradio as gr
|
|
| 3 |
import whisper
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
-
# Configure Gemini
|
| 7 |
genai.configure(
|
| 8 |
-
api_key=os.getenv("
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
speech_model = whisper.load_model("tiny")
|
| 13 |
|
|
|
|
| 14 |
ai_model = genai.GenerativeModel(
|
| 15 |
"gemini-1.5-flash"
|
| 16 |
)
|
|
@@ -18,20 +19,31 @@ ai_model = genai.GenerativeModel(
|
|
| 18 |
# Main function
|
| 19 |
def voice_assistant(audio):
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
response = ai_model.generate_content(user_text)
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=voice_assistant,
|
| 34 |
-
inputs=gr.Audio(
|
|
|
|
|
|
|
|
|
|
| 35 |
outputs="text",
|
| 36 |
title="AI Voice Assistant",
|
| 37 |
description="Speak and get AI responses"
|
|
|
|
| 3 |
import whisper
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
# Configure Gemini
|
| 7 |
genai.configure(
|
| 8 |
+
api_key=os.getenv("GEMINI_API_KEY")
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Load Whisper model
|
| 12 |
speech_model = whisper.load_model("tiny")
|
| 13 |
|
| 14 |
+
# Load Gemini model
|
| 15 |
ai_model = genai.GenerativeModel(
|
| 16 |
"gemini-1.5-flash"
|
| 17 |
)
|
|
|
|
| 19 |
# Main function
|
| 20 |
def voice_assistant(audio):
|
| 21 |
|
| 22 |
+
# Check if audio exists
|
| 23 |
+
if audio is None:
|
| 24 |
+
return "Please record your voice first."
|
| 25 |
|
| 26 |
+
try:
|
| 27 |
+
# Convert speech to text
|
| 28 |
+
result = speech_model.transcribe(audio)
|
| 29 |
|
| 30 |
+
user_text = result["text"]
|
|
|
|
| 31 |
|
| 32 |
+
# Gemini response
|
| 33 |
+
response = ai_model.generate_content(user_text)
|
| 34 |
|
| 35 |
+
return f"You said: {user_text}\n\nAI: {response.text}"
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error: {str(e)}"
|
| 39 |
+
|
| 40 |
+
# Gradio UI
|
| 41 |
interface = gr.Interface(
|
| 42 |
fn=voice_assistant,
|
| 43 |
+
inputs=gr.Audio(
|
| 44 |
+
sources=["microphone"],
|
| 45 |
+
type="filepath"
|
| 46 |
+
),
|
| 47 |
outputs="text",
|
| 48 |
title="AI Voice Assistant",
|
| 49 |
description="Speak and get AI responses"
|