jeremierostan commited on
Commit
9e304d5
·
verified ·
1 Parent(s): 0ea3700

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -3,7 +3,7 @@ import base64
3
  import json
4
  import os
5
  import pathlib
6
- from typing import AsyncGenerator, Literal, Optional, List
7
 
8
  import gradio as gr
9
  import numpy as np
@@ -32,26 +32,10 @@ load_dotenv()
32
 
33
  # Define preset system prompts with more detailed and actionable instructions
34
  SYSTEM_PROMPTS = {
35
- "default": [
36
- "You are a helpful, harmless, and honest AI assistant.",
37
- "Provide clear, concise, and accurate information.",
38
- "Adapt your communication style to be most helpful to the user."
39
- ],
40
- "Behavior Analyst": [
41
- "You are an expert in behavior analysis, Positive Behavior Interventions and Supports, and Restorative approaches and practices.",
42
- "Your primary role is to help educators understand and address student behavior issues.",
43
- "Provide evidence-based strategies, empathetic insights, and practical solutions.",
44
- "Focus on understanding the underlying causes of behavior and developing supportive interventions."
45
- ],
46
- "UDL Expert": [
47
- "You are an expert in Universal Design for Learning (UDL).",
48
- "Your role is to help educators design units, lessons, and activities to be universally accessible.",
49
- "Provide detailed guidance on addressing student variability and potential learning barriers.",
50
- "Offer concrete strategies for multiple means of engagement, representation, and expression."
51
- ],
52
- "prompt3": [
53
- "PLACEHOLDER FOR PROMPT 3 WITH MULTIPLE INSTRUCTION PARTS"
54
- ]
55
  }
56
 
57
 
@@ -90,7 +74,7 @@ class GeminiHandler(AsyncStreamHandler):
90
  self,
91
  prompt_key: Optional[str] = None,
92
  custom_prompt: Optional[str] = None
93
- ) -> Optional[List[str]]:
94
  """
95
  Prepare system instruction based on preset or custom prompt.
96
 
@@ -99,12 +83,11 @@ class GeminiHandler(AsyncStreamHandler):
99
  custom_prompt: Custom user-defined system prompt
100
 
101
  Returns:
102
- List of system instruction parts or None
103
  """
104
  # Check for custom prompt first
105
  if custom_prompt:
106
- # If custom prompt is provided, split it into paragraphs
107
- return [p.strip() for p in custom_prompt.split('\n') if p.strip()]
108
 
109
  # Then check for preset prompt
110
  if prompt_key and prompt_key in SYSTEM_PROMPTS:
@@ -128,19 +111,26 @@ class GeminiHandler(AsyncStreamHandler):
128
  http_options={"api_version": "v1alpha"},
129
  )
130
 
131
- # Create config with system instructions if available
132
- config = LiveConnectConfig(
133
- response_modalities=["AUDIO"], # type: ignore
134
- speech_config=SpeechConfig(
135
  voice_config=VoiceConfig(
136
  prebuilt_voice_config=PrebuiltVoiceConfig(
137
  voice_name=voice_name,
138
  )
139
  )
140
- ),
141
- # Add system instruction if available
142
- **({"system_instruction": system_instruction} if system_instruction else {})
143
- )
 
 
 
 
 
 
 
144
 
145
  try:
146
  async with client.aio.live.connect(
@@ -175,7 +165,6 @@ class GeminiHandler(AsyncStreamHandler):
175
  def shutdown(self) -> None:
176
  self.quit.set()
177
 
178
-
179
  stream = Stream(
180
  modality="audio",
181
  mode="send-receive",
 
3
  import json
4
  import os
5
  import pathlib
6
+ from typing import AsyncGenerator, Literal, Optional, List, Union
7
 
8
  import gradio as gr
9
  import numpy as np
 
32
 
33
  # Define preset system prompts with more detailed and actionable instructions
34
  SYSTEM_PROMPTS = {
35
+ "default": "You are a helpful, harmless, and honest AI assistant. Provide clear, concise, and accurate information.",
36
+ "Behavior Analyst": "You are an expert in behavior analysis, Positive Behavior Interventions and Supports, and Restorative approaches and practices. Your primary role is to help educators understand and address student behavior issues. Provide evidence-based strategies, empathetic insights, and practical solutions.",
37
+ "UDL Expert": "You are an expert in Universal Design for Learning (UDL). Your role is to help educators design units, lessons, and activities to be universally accessible. Provide detailed guidance on addressing student variability and potential learning barriers.",
38
+ "prompt3": "PLACEHOLDER FOR PROMPT 3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  }
40
 
41
 
 
74
  self,
75
  prompt_key: Optional[str] = None,
76
  custom_prompt: Optional[str] = None
77
+ ) -> Optional[str]:
78
  """
79
  Prepare system instruction based on preset or custom prompt.
80
 
 
83
  custom_prompt: Custom user-defined system prompt
84
 
85
  Returns:
86
+ System instruction as a string or None
87
  """
88
  # Check for custom prompt first
89
  if custom_prompt:
90
+ return custom_prompt
 
91
 
92
  # Then check for preset prompt
93
  if prompt_key and prompt_key in SYSTEM_PROMPTS:
 
111
  http_options={"api_version": "v1alpha"},
112
  )
113
 
114
+ # Create config for the connection
115
+ config_kwargs = {
116
+ "response_modalities": ["AUDIO"],
117
+ "speech_config": SpeechConfig(
118
  voice_config=VoiceConfig(
119
  prebuilt_voice_config=PrebuiltVoiceConfig(
120
  voice_name=voice_name,
121
  )
122
  )
123
+ )
124
+ }
125
+
126
+ # Add system instruction if available
127
+ if system_instruction:
128
+ config_kwargs["system_instruction"] = {
129
+ "parts": [{"text": system_instruction}]
130
+ }
131
+
132
+ # Create the configuration
133
+ config = LiveConnectConfig(**config_kwargs)
134
 
135
  try:
136
  async with client.aio.live.connect(
 
165
  def shutdown(self) -> None:
166
  self.quit.set()
167
 
 
168
  stream = Stream(
169
  modality="audio",
170
  mode="send-receive",