Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from dotenv import load_dotenv
|
| 2 |
from openai import OpenAI
|
| 3 |
from datetime import datetime
|
|
@@ -55,10 +56,12 @@ def push(text: str) -> bool:
|
|
| 55 |
|
| 56 |
|
| 57 |
def record_user_details(email, name="Name not provided", notes="not provided"):
|
|
|
|
| 58 |
push(f"Recording {name} with email {email} and notes {notes}")
|
| 59 |
return {"recorded": "ok"}
|
| 60 |
|
| 61 |
def record_unknown_question(question):
|
|
|
|
| 62 |
push(f"Recording {question}")
|
| 63 |
return {"recorded": "ok"}
|
| 64 |
|
|
@@ -154,12 +157,19 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 154 |
]
|
| 155 |
|
| 156 |
def run(client, model):
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Clients
|
| 165 |
gemini = OpenAI(
|
|
@@ -167,12 +177,26 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 167 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 168 |
)
|
| 169 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
try:
|
| 172 |
response = run(gemini, "gemini-2.0-flash")
|
| 173 |
except Exception as e:
|
| 174 |
if "quota" in str(e).lower() or "resource_exhausted" in str(e).lower():
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
else:
|
| 177 |
raise
|
| 178 |
|
|
@@ -186,8 +210,7 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 186 |
messages.extend(tool_results)
|
| 187 |
|
| 188 |
# Final answer (NO tools this time)
|
| 189 |
-
response = run(
|
| 190 |
-
|
| 191 |
return response.choices[0].message.content
|
| 192 |
|
| 193 |
|
|
|
|
| 1 |
+
from pydoc import cli
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from openai import OpenAI
|
| 4 |
from datetime import datetime
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 59 |
+
print(f"Recording {name} with email {email} and notes {notes}")
|
| 60 |
push(f"Recording {name} with email {email} and notes {notes}")
|
| 61 |
return {"recorded": "ok"}
|
| 62 |
|
| 63 |
def record_unknown_question(question):
|
| 64 |
+
print(f"Recording {question}")
|
| 65 |
push(f"Recording {question}")
|
| 66 |
return {"recorded": "ok"}
|
| 67 |
|
|
|
|
| 157 |
]
|
| 158 |
|
| 159 |
def run(client, model):
|
| 160 |
+
if client is open_router_client:
|
| 161 |
+
return client.chat.completions.create(
|
| 162 |
+
model=model,
|
| 163 |
+
messages=messages,
|
| 164 |
+
tools=tools
|
| 165 |
+
)
|
| 166 |
+
else:
|
| 167 |
+
return client.chat.completions.create(
|
| 168 |
+
model=model,
|
| 169 |
+
messages=messages,
|
| 170 |
+
tools=tools,
|
| 171 |
+
max_tokens=512 # 🔒 hard cap
|
| 172 |
+
)
|
| 173 |
|
| 174 |
# Clients
|
| 175 |
gemini = OpenAI(
|
|
|
|
| 177 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 178 |
)
|
| 179 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 180 |
+
open_router_client = OpenAI(
|
| 181 |
+
api_key=os.getenv("OPEN_ROUTER_API_KEY"),
|
| 182 |
+
base_url="https://openrouter.ai/api/v1"
|
| 183 |
+
)
|
| 184 |
|
| 185 |
try:
|
| 186 |
response = run(gemini, "gemini-2.0-flash")
|
| 187 |
except Exception as e:
|
| 188 |
if "quota" in str(e).lower() or "resource_exhausted" in str(e).lower():
|
| 189 |
+
print("Google Limit Exceeded! Falling to open API")
|
| 190 |
+
try:
|
| 191 |
+
response = run(openai_client, "gpt-4o-mini")
|
| 192 |
+
except Exception as gpt:
|
| 193 |
+
if "rate limit" in str(gpt).lower() or "rate_limit_exceeded" in str(gpt).lower():
|
| 194 |
+
print("Open API rate limit exceeded! Falling to Open Router API")
|
| 195 |
+
# xiaomi/mimo-v2-flash:free
|
| 196 |
+
# allenai/olmo-3.1-32b-think:free
|
| 197 |
+
response = run(open_router_client, "xiaomi/mimo-v2-flash:free")
|
| 198 |
+
else:
|
| 199 |
+
raise
|
| 200 |
else:
|
| 201 |
raise
|
| 202 |
|
|
|
|
| 210 |
messages.extend(tool_results)
|
| 211 |
|
| 212 |
# Final answer (NO tools this time)
|
| 213 |
+
response = run(open_router_client, "xiaomi/mimo-v2-flash:free")
|
|
|
|
| 214 |
return response.choices[0].message.content
|
| 215 |
|
| 216 |
|