CultriX commited on
Commit
7b71415
·
verified ·
1 Parent(s): 642e9cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -12,10 +12,10 @@ from functools import lru_cache
12
  # -------------------- Configuration --------------------
13
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
15
- # -------------------- External Model Call (with Caching) --------------------
16
  @lru_cache(maxsize=128) # Cache up to 128 responses
17
- async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None) -> str:
18
- """Sends a prompt to the OpenAI API endpoint, with caching."""
19
  if api_key is None:
20
  api_key = os.getenv("OPENAI_API_KEY")
21
  if api_key is None:
@@ -29,11 +29,30 @@ async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None) ->
29
  "model": model,
30
  "messages": [{"role": "user", "content": prompt}],
31
  }
32
- async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client:
33
- response = await client.post(url, headers=headers, json=payload)
34
- response.raise_for_status()
35
- response_json = response.json()
36
- return response_json["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # -------------------- Shared Context --------------------
39
  class Context:
@@ -393,4 +412,4 @@ dummy_iface = gr.Interface(lambda x:x, "textbox", "textbox")
393
 
394
  if __name__ == "__main__":
395
  demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"])
396
- demo.launch()
 
12
  # -------------------- Configuration --------------------
13
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
15
+ # -------------------- External Model Call (with Caching and Retry) --------------------
16
  @lru_cache(maxsize=128) # Cache up to 128 responses
17
+ async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str:
18
+ """Sends a prompt to the OpenAI API endpoint, with caching and retries."""
19
  if api_key is None:
20
  api_key = os.getenv("OPENAI_API_KEY")
21
  if api_key is None:
 
29
  "model": model,
30
  "messages": [{"role": "user", "content": prompt}],
31
  }
32
+
33
+ for attempt in range(max_retries):
34
+ try:
35
+ async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client:
36
+ response = await client.post(url, headers=headers, json=payload)
37
+ response.raise_for_status()
38
+ response_json = response.json()
39
+ return response_json["choices"][0]["message"]["content"]
40
+ except httpx.HTTPStatusError as e:
41
+ logging.error(f"HTTP error (attempt {attempt + 1}/{max_retries}): {e}")
42
+ if e.response.status_code in (502, 503, 504): # Retry on 502, 503, 504
43
+ await asyncio.sleep(2 ** attempt) # Exponential backoff
44
+ continue
45
+ else:
46
+ raise # Re-raise for other HTTP errors
47
+ except httpx.RequestError as e:
48
+ logging.error(f"Request error (attempt {attempt + 1}/{max_retries}): {e}")
49
+ await asyncio.sleep(2 ** attempt)
50
+ continue
51
+ except Exception as e:
52
+ logging.error(f"An unexpected error occurred (attempt {attempt+1}/{max_retries}): {e}")
53
+ raise
54
+ raise Exception(f"Failed to get response from OpenAI API after {max_retries} attempts.")
55
+
56
 
57
  # -------------------- Shared Context --------------------
58
  class Context:
 
412
 
413
  if __name__ == "__main__":
414
  demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"])
415
+ demo.launch(share=True)