Spaces:
Running
Running
yuntian-deng
commited on
Commit
•
4610ea3
1
Parent(s):
f6626f6
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ import sys
|
|
4 |
import json
|
5 |
import requests
|
6 |
import random
|
7 |
-
|
|
|
8 |
|
9 |
MODEL = "o1-preview"
|
10 |
API_URL = os.getenv("API_URL")
|
@@ -16,6 +17,13 @@ NUM_THREADS = int(os.getenv("NUM_THREADS"))
|
|
16 |
|
17 |
print (NUM_THREADS)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def exception_handler(exception_type, exception, traceback):
|
20 |
print("%s: %s" % (exception_type.__name__, exception))
|
21 |
sys.excepthook = exception_handler
|
@@ -78,7 +86,7 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
78 |
|
79 |
try:
|
80 |
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
81 |
-
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
82 |
response_code = f"{response}"
|
83 |
#if response_code.strip() != "<Response [200]>":
|
84 |
# #print(f"response code - {response}")
|
|
|
4 |
import json
|
5 |
import requests
|
6 |
import random
|
7 |
+
import timeout_decorator
|
8 |
+
from tenacity import retry, wait_fixed, stop_after_attempt
|
9 |
|
10 |
MODEL = "o1-preview"
|
11 |
API_URL = os.getenv("API_URL")
|
|
|
17 |
|
18 |
print (NUM_THREADS)
|
19 |
|
20 |
+
@retry(stop=stop_after_attempt(5), wait=wait_fixed(2))
|
21 |
+
@timeout_decorator.timeout(120)
|
22 |
+
def call_openai_api(payload, headers):
|
23 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
24 |
+
response.raise_for_status()
|
25 |
+
return response
|
26 |
+
|
27 |
def exception_handler(exception_type, exception, traceback):
|
28 |
print("%s: %s" % (exception_type.__name__, exception))
|
29 |
sys.excepthook = exception_handler
|
|
|
86 |
|
87 |
try:
|
88 |
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
89 |
+
response = call_openai_api(payload, headers) #requests.post(API_URL, headers=headers, json=payload, stream=True)
|
90 |
response_code = f"{response}"
|
91 |
#if response_code.strip() != "<Response [200]>":
|
92 |
# #print(f"response code - {response}")
|