Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,7 +62,6 @@ class GeminiHandler(AsyncStreamHandler):
|
|
| 62 |
self.input_queue: asyncio.Queue = asyncio.Queue()
|
| 63 |
self.output_queue: asyncio.Queue = asyncio.Queue()
|
| 64 |
self.quit: asyncio.Event = asyncio.Event()
|
| 65 |
-
self.system_prompt = None
|
| 66 |
|
| 67 |
def copy(self) -> "GeminiHandler":
|
| 68 |
return GeminiHandler(
|
|
@@ -77,55 +76,68 @@ class GeminiHandler(AsyncStreamHandler):
|
|
| 77 |
api_key, voice_name, prompt_key, custom_prompt = self.latest_args[1:]
|
| 78 |
|
| 79 |
# Determine which prompt to use
|
|
|
|
| 80 |
if prompt_key and prompt_key in SYSTEM_PROMPTS:
|
| 81 |
-
|
| 82 |
elif custom_prompt:
|
| 83 |
-
|
| 84 |
else:
|
| 85 |
api_key, voice_name = None, "Puck"
|
| 86 |
-
|
| 87 |
|
| 88 |
client = genai.Client(
|
| 89 |
api_key=api_key or os.getenv("GEMINI_API_KEY"),
|
| 90 |
http_options={"api_version": "v1alpha"},
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# Create
|
| 94 |
-
|
| 95 |
-
response_modalities=["AUDIO"], # type: ignore
|
| 96 |
-
speech_config=SpeechConfig(
|
| 97 |
-
voice_config=VoiceConfig(
|
| 98 |
-
prebuilt_voice_config=PrebuiltVoiceConfig(
|
| 99 |
-
voice_name=voice_name,
|
| 100 |
-
)
|
| 101 |
-
)
|
| 102 |
-
),
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# Get model reference
|
| 106 |
-
model = client.get_model("gemini-2.0-flash-exp")
|
| 107 |
-
|
| 108 |
-
# Apply system prompt if available
|
| 109 |
-
if self.system_prompt:
|
| 110 |
try:
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
-
print(f"
|
| 116 |
-
#
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
# Create session
|
| 120 |
async with client.aio.live.connect(
|
| 121 |
model="gemini-2.0-flash-exp", config=config
|
| 122 |
) as session:
|
| 123 |
-
# If we couldn't set the
|
| 124 |
-
|
| 125 |
-
if self.system_prompt:
|
| 126 |
try:
|
| 127 |
-
|
| 128 |
-
await session.send_message(f"SYSTEM: {self.system_prompt}\n\nPlease acknowledge this system instruction.")
|
| 129 |
# Wait for a response
|
| 130 |
async for response in session.stream_response():
|
| 131 |
# Just need one response to confirm it was received
|
|
@@ -134,7 +146,6 @@ class GeminiHandler(AsyncStreamHandler):
|
|
| 134 |
except Exception as e:
|
| 135 |
print(f"Could not send system prompt as message: {e}")
|
| 136 |
|
| 137 |
-
# Now start the audio stream
|
| 138 |
async for audio in session.start_stream(
|
| 139 |
stream=self.stream(), mime_type="audio/pcm"
|
| 140 |
):
|
|
|
|
| 62 |
self.input_queue: asyncio.Queue = asyncio.Queue()
|
| 63 |
self.output_queue: asyncio.Queue = asyncio.Queue()
|
| 64 |
self.quit: asyncio.Event = asyncio.Event()
|
|
|
|
| 65 |
|
| 66 |
def copy(self) -> "GeminiHandler":
|
| 67 |
return GeminiHandler(
|
|
|
|
| 76 |
api_key, voice_name, prompt_key, custom_prompt = self.latest_args[1:]
|
| 77 |
|
| 78 |
# Determine which prompt to use
|
| 79 |
+
system_prompt = None
|
| 80 |
if prompt_key and prompt_key in SYSTEM_PROMPTS:
|
| 81 |
+
system_prompt = SYSTEM_PROMPTS[prompt_key]
|
| 82 |
elif custom_prompt:
|
| 83 |
+
system_prompt = custom_prompt
|
| 84 |
else:
|
| 85 |
api_key, voice_name = None, "Puck"
|
| 86 |
+
system_prompt = None
|
| 87 |
|
| 88 |
client = genai.Client(
|
| 89 |
api_key=api_key or os.getenv("GEMINI_API_KEY"),
|
| 90 |
http_options={"api_version": "v1alpha"},
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# Create config with system instructions if available
|
| 94 |
+
if system_prompt:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
+
# Try with system_instruction as a list
|
| 97 |
+
config = LiveConnectConfig(
|
| 98 |
+
response_modalities=["AUDIO"], # type: ignore
|
| 99 |
+
speech_config=SpeechConfig(
|
| 100 |
+
voice_config=VoiceConfig(
|
| 101 |
+
prebuilt_voice_config=PrebuiltVoiceConfig(
|
| 102 |
+
voice_name=voice_name,
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
),
|
| 106 |
+
system_instruction=[system_prompt],
|
| 107 |
+
)
|
| 108 |
except Exception as e:
|
| 109 |
+
print(f"Error with system_instruction: {e}")
|
| 110 |
+
# Fall back to basic config without system instruction
|
| 111 |
+
config = LiveConnectConfig(
|
| 112 |
+
response_modalities=["AUDIO"], # type: ignore
|
| 113 |
+
speech_config=SpeechConfig(
|
| 114 |
+
voice_config=VoiceConfig(
|
| 115 |
+
prebuilt_voice_config=PrebuiltVoiceConfig(
|
| 116 |
+
voice_name=voice_name,
|
| 117 |
+
)
|
| 118 |
+
)
|
| 119 |
+
),
|
| 120 |
+
)
|
| 121 |
+
else:
|
| 122 |
+
# Basic config without system instruction
|
| 123 |
+
config = LiveConnectConfig(
|
| 124 |
+
response_modalities=["AUDIO"], # type: ignore
|
| 125 |
+
speech_config=SpeechConfig(
|
| 126 |
+
voice_config=VoiceConfig(
|
| 127 |
+
prebuilt_voice_config=PrebuiltVoiceConfig(
|
| 128 |
+
voice_name=voice_name,
|
| 129 |
+
)
|
| 130 |
+
)
|
| 131 |
+
),
|
| 132 |
+
)
|
| 133 |
|
|
|
|
| 134 |
async with client.aio.live.connect(
|
| 135 |
model="gemini-2.0-flash-exp", config=config
|
| 136 |
) as session:
|
| 137 |
+
# If we have a system prompt but couldn't set it in the config, try sending it as a message
|
| 138 |
+
if system_prompt and 'system_instruction' not in str(config):
|
|
|
|
| 139 |
try:
|
| 140 |
+
await session.send_message(f"SYSTEM: {system_prompt}\n\nPlease acknowledge this system instruction.")
|
|
|
|
| 141 |
# Wait for a response
|
| 142 |
async for response in session.stream_response():
|
| 143 |
# Just need one response to confirm it was received
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
print(f"Could not send system prompt as message: {e}")
|
| 148 |
|
|
|
|
| 149 |
async for audio in session.start_stream(
|
| 150 |
stream=self.stream(), mime_type="audio/pcm"
|
| 151 |
):
|