Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -177,29 +177,40 @@ def stream_chat(prompt: str, history=None):
|
|
| 177 |
|
| 178 |
messages.append({"role": "user", "content": prompt})
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
"temperature": 0.7,
|
| 184 |
-
"max_tokens": 4188,
|
| 185 |
-
"stream": True,
|
| 186 |
-
}
|
| 187 |
|
| 188 |
last_error = "All Groq API keys failed."
|
| 189 |
|
| 190 |
for index, api_key in enumerate(GROQ_CHAT_KEYS, start=1):
|
| 191 |
-
print(f"[CHAT-DEBUG] π Trying GROQ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 194 |
|
| 195 |
try:
|
| 196 |
response = requests.post(
|
| 197 |
-
GROQ_URL_CHAT,
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
response.raise_for_status()
|
| 201 |
|
| 202 |
-
|
|
|
|
| 203 |
for line in response.iter_lines():
|
| 204 |
if not line:
|
| 205 |
continue
|
|
@@ -210,7 +221,6 @@ def stream_chat(prompt: str, history=None):
|
|
| 210 |
chunk = line[6:]
|
| 211 |
if chunk == "[DONE]":
|
| 212 |
break
|
| 213 |
-
|
| 214 |
try:
|
| 215 |
out = json.loads(chunk)["choices"][0]["delta"].get("content", "")
|
| 216 |
if out:
|
|
@@ -218,25 +228,24 @@ def stream_chat(prompt: str, history=None):
|
|
| 218 |
except:
|
| 219 |
continue
|
| 220 |
|
| 221 |
-
print(f"[CHAT-DEBUG] β
Key #{index}
|
| 222 |
return
|
| 223 |
|
| 224 |
except requests.exceptions.HTTPError as e:
|
| 225 |
try:
|
| 226 |
-
detail = json.loads(e.response.text).get("error", {}).get("message", "Unknown
|
| 227 |
except:
|
| 228 |
detail = e.response.text
|
| 229 |
|
| 230 |
-
last_error = f"
|
| 231 |
print(f"[CHAT-DEBUG] β {last_error}")
|
| 232 |
|
| 233 |
except requests.exceptions.RequestException as e:
|
| 234 |
-
last_error = f"
|
| 235 |
print(f"[CHAT-DEBUG] β {last_error}")
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
yield f"Maaf, server tidak bisa memproses permintaanmu saat ini. {last_error}"
|
| 240 |
|
| 241 |
# =========================
|
| 242 |
# π Chat Endpoint (Text + Voice)
|
|
|
|
| 177 |
|
| 178 |
messages.append({"role": "user", "content": prompt})
|
| 179 |
|
| 180 |
+
# Default model
|
| 181 |
+
primary_model = "moonshotai/kimi-k2-instruct-0905"
|
| 182 |
+
fallback_model = "openai/gpt-oss-120b"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
last_error = "All Groq API keys failed."
|
| 185 |
|
| 186 |
for index, api_key in enumerate(GROQ_CHAT_KEYS, start=1):
|
| 187 |
+
print(f"[CHAT-DEBUG] π Trying GROQ KEY #{index}")
|
| 188 |
+
|
| 189 |
+
# Jika key kedua β pakai model fallback
|
| 190 |
+
model_to_use = fallback_model if index == 2 else primary_model
|
| 191 |
+
|
| 192 |
+
payload = {
|
| 193 |
+
"model": model_to_use,
|
| 194 |
+
"messages": messages,
|
| 195 |
+
"temperature": 0.7,
|
| 196 |
+
"max_tokens": 4500,
|
| 197 |
+
"stream": True,
|
| 198 |
+
}
|
| 199 |
|
| 200 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 201 |
|
| 202 |
try:
|
| 203 |
response = requests.post(
|
| 204 |
+
GROQ_URL_CHAT,
|
| 205 |
+
headers=headers,
|
| 206 |
+
json=payload,
|
| 207 |
+
stream=True,
|
| 208 |
+
timeout=120
|
| 209 |
)
|
| 210 |
response.raise_for_status()
|
| 211 |
|
| 212 |
+
print(f"[CHAT-DEBUG] π Connected. Using model: {model_to_use}")
|
| 213 |
+
|
| 214 |
for line in response.iter_lines():
|
| 215 |
if not line:
|
| 216 |
continue
|
|
|
|
| 221 |
chunk = line[6:]
|
| 222 |
if chunk == "[DONE]":
|
| 223 |
break
|
|
|
|
| 224 |
try:
|
| 225 |
out = json.loads(chunk)["choices"][0]["delta"].get("content", "")
|
| 226 |
if out:
|
|
|
|
| 228 |
except:
|
| 229 |
continue
|
| 230 |
|
| 231 |
+
print(f"[CHAT-DEBUG] β
Key #{index} SUCCESS.")
|
| 232 |
return
|
| 233 |
|
| 234 |
except requests.exceptions.HTTPError as e:
|
| 235 |
try:
|
| 236 |
+
detail = json.loads(e.response.text).get("error", {}).get("message", "Unknown")
|
| 237 |
except:
|
| 238 |
detail = e.response.text
|
| 239 |
|
| 240 |
+
last_error = f"Key #{index} failed (HTTP {e.response.status_code}): {detail}"
|
| 241 |
print(f"[CHAT-DEBUG] β {last_error}")
|
| 242 |
|
| 243 |
except requests.exceptions.RequestException as e:
|
| 244 |
+
last_error = f"Key #{index} connection failed: {e}"
|
| 245 |
print(f"[CHAT-DEBUG] β {last_error}")
|
| 246 |
|
| 247 |
+
print("[CHAT-DEBUG] π All keys failed.")
|
| 248 |
+
yield f"Sorry, error coming!. {last_error}"
|
|
|
|
| 249 |
|
| 250 |
# =========================
|
| 251 |
# π Chat Endpoint (Text + Voice)
|