Abs6187 commited on
Commit
2293c14
·
verified ·
1 Parent(s): cc40246

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -23,15 +23,23 @@ class GPT5Model:
23
  "prompt": full_prompt,
24
  "max_tokens": 500
25
  }
26
- response = requests.post("https://api.pplx.ai/v1/generate", json=data, headers=headers)
 
 
 
 
27
 
28
  if response.status_code != 200:
29
- return f"Error: {response.text}"
30
 
31
  try:
32
- return response.json()["choices"][0]["text"].strip()
33
- except Exception as e:
34
- return f"Error parsing response: {e}"
 
 
 
 
35
 
36
  # -----------------------------
37
  # Load API key
@@ -79,4 +87,3 @@ with gr.Blocks(css="""
79
  txt.submit(respond, [txt, chatbot], [txt, chatbot])
80
 
81
  demo.launch()
82
-
 
23
  "prompt": full_prompt,
24
  "max_tokens": 500
25
  }
26
+
27
+ try:
28
+ response = requests.post("https://api.pplx.ai/v1/generate", json=data, headers=headers, timeout=15)
29
+ except requests.exceptions.RequestException as e:
30
+ return f"Error: Could not reach API. {e}"
31
 
32
  if response.status_code != 200:
33
+ return f"Error: API key exhausted or invalid. ({response.status_code})"
34
 
35
  try:
36
+ result = response.json()
37
+ choices = result.get("choices", [])
38
+ if not choices or "text" not in choices[0] or not choices[0]["text"].strip():
39
+ return "Error: API key exhausted or invalid."
40
+ return choices[0]["text"].strip()
41
+ except Exception:
42
+ return "Error: API key exhausted or invalid."
43
 
44
  # -----------------------------
45
  # Load API key
 
87
  txt.submit(respond, [txt, chatbot], [txt, chatbot])
88
 
89
  demo.launch()