Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,11 @@ from google import genai
|
|
| 5 |
|
| 6 |
app = Quart(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
client = genai.Client()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
MODEL = "gemini-
|
| 13 |
|
| 14 |
VOICE_MODES = {
|
| 15 |
'Zephyr': 'Zephyr', # Default / Balanced
|
|
@@ -28,6 +28,15 @@ async def index():
|
|
| 28 |
"supported_voices": list(VOICE_MODES.keys())
|
| 29 |
}
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.websocket('/stream')
|
| 32 |
async def ws_stream():
|
| 33 |
"""
|
|
@@ -37,7 +46,6 @@ async def ws_stream():
|
|
| 37 |
requested_voice = websocket.args.get("voice", "Zephyr")
|
| 38 |
voice_name = VOICE_MODES.get(requested_voice, "Zephyr")
|
| 39 |
|
| 40 |
-
# Using a dictionary for config prevents AttributeError on specific SDK versions
|
| 41 |
config = {
|
| 42 |
"response_modalities": ["AUDIO"],
|
| 43 |
"speech_config": {
|
|
@@ -56,17 +64,14 @@ async def ws_stream():
|
|
| 56 |
print(f"Connecting to Gemini Live API with voice: {voice_name}...")
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
# Pass the dictionary directly to the config parameter
|
| 60 |
async with client.aio.live.connect(model=MODEL, config=config) as session:
|
| 61 |
print("Live session established.")
|
| 62 |
|
| 63 |
-
# Task 1: Stream audio from Client (Termux) -> Gemini
|
| 64 |
async def client_to_gemini():
|
| 65 |
try:
|
| 66 |
while True:
|
| 67 |
data = await websocket.receive()
|
| 68 |
if isinstance(data, bytes):
|
| 69 |
-
# Sending 16kHz PCM data from client to Gemini
|
| 70 |
await session.send(
|
| 71 |
input={"data": data, "mime_type": "audio/pcm;rate=16000"}
|
| 72 |
)
|
|
@@ -75,7 +80,6 @@ async def ws_stream():
|
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error reading from client: {e}")
|
| 77 |
|
| 78 |
-
# Task 2: Stream audio from Gemini -> Client (Termux)
|
| 79 |
async def gemini_to_client():
|
| 80 |
try:
|
| 81 |
async for message in session.receive():
|
|
@@ -88,14 +92,12 @@ async def ws_stream():
|
|
| 88 |
if model_turn:
|
| 89 |
for part in model_turn.parts:
|
| 90 |
if part.inline_data and part.inline_data.data:
|
| 91 |
-
# Sending 24kHz PCM data back to client
|
| 92 |
await websocket.send(part.inline_data.data)
|
| 93 |
except asyncio.CancelledError:
|
| 94 |
pass
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error receiving from Gemini: {e}")
|
| 97 |
|
| 98 |
-
# Run both streaming directions concurrently
|
| 99 |
task1 = asyncio.create_task(client_to_gemini())
|
| 100 |
task2 = asyncio.create_task(gemini_to_client())
|
| 101 |
|
|
@@ -111,5 +113,4 @@ async def ws_stream():
|
|
| 111 |
print(f"Connection failed: {e}")
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
-
# HF Spaces standard port is 7860
|
| 115 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 5 |
|
| 6 |
app = Quart(__name__)
|
| 7 |
|
| 8 |
+
# Force v1beta version specifically for Multimodal Live API features
|
| 9 |
+
client = genai.Client(http_options={'api_version': 'v1beta'})
|
| 10 |
|
| 11 |
+
# This is the standard model ID for bidirectional streaming
|
| 12 |
+
MODEL = "gemini-2.0-flash-exp"
|
| 13 |
|
| 14 |
VOICE_MODES = {
|
| 15 |
'Zephyr': 'Zephyr', # Default / Balanced
|
|
|
|
| 28 |
"supported_voices": list(VOICE_MODES.keys())
|
| 29 |
}
|
| 30 |
|
| 31 |
+
@app.route('/models')
|
| 32 |
+
async def list_models():
|
| 33 |
+
"""Use this to see exactly which models your key has access to if it fails."""
|
| 34 |
+
try:
|
| 35 |
+
models = client.models.list()
|
| 36 |
+
return {"available_models": [m.name for m in models]}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return {"error": str(e)}
|
| 39 |
+
|
| 40 |
@app.websocket('/stream')
|
| 41 |
async def ws_stream():
|
| 42 |
"""
|
|
|
|
| 46 |
requested_voice = websocket.args.get("voice", "Zephyr")
|
| 47 |
voice_name = VOICE_MODES.get(requested_voice, "Zephyr")
|
| 48 |
|
|
|
|
| 49 |
config = {
|
| 50 |
"response_modalities": ["AUDIO"],
|
| 51 |
"speech_config": {
|
|
|
|
| 64 |
print(f"Connecting to Gemini Live API with voice: {voice_name}...")
|
| 65 |
|
| 66 |
try:
|
|
|
|
| 67 |
async with client.aio.live.connect(model=MODEL, config=config) as session:
|
| 68 |
print("Live session established.")
|
| 69 |
|
|
|
|
| 70 |
async def client_to_gemini():
|
| 71 |
try:
|
| 72 |
while True:
|
| 73 |
data = await websocket.receive()
|
| 74 |
if isinstance(data, bytes):
|
|
|
|
| 75 |
await session.send(
|
| 76 |
input={"data": data, "mime_type": "audio/pcm;rate=16000"}
|
| 77 |
)
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error reading from client: {e}")
|
| 82 |
|
|
|
|
| 83 |
async def gemini_to_client():
|
| 84 |
try:
|
| 85 |
async for message in session.receive():
|
|
|
|
| 92 |
if model_turn:
|
| 93 |
for part in model_turn.parts:
|
| 94 |
if part.inline_data and part.inline_data.data:
|
|
|
|
| 95 |
await websocket.send(part.inline_data.data)
|
| 96 |
except asyncio.CancelledError:
|
| 97 |
pass
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error receiving from Gemini: {e}")
|
| 100 |
|
|
|
|
| 101 |
task1 = asyncio.create_task(client_to_gemini())
|
| 102 |
task2 = asyncio.create_task(gemini_to_client())
|
| 103 |
|
|
|
|
| 113 |
print(f"Connection failed: {e}")
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
|
|
|
| 116 |
app.run(host="0.0.0.0", port=7860)
|