Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,31 +10,46 @@ assistant_id=os.getenv('ASSISTANT_ID')
|
|
10 |
client = openai.OpenAI(api_key=openai.api_key)
|
11 |
|
12 |
def ask_openai(question):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
examples = [
|
40 |
["My Eucalyptus tree is struggling outside in the cold weather in europe"],
|
|
|
10 |
client = openai.OpenAI(api_key=openai.api_key)
|
11 |
|
12 |
def ask_openai(question):
|
13 |
+
try:
|
14 |
+
thread = client.beta.threads.create()
|
15 |
+
client.beta.threads.messages.create(
|
16 |
+
thread_id=thread.id,
|
17 |
+
role="user",
|
18 |
+
content=question,
|
19 |
+
)
|
20 |
+
|
21 |
+
run = client.beta.threads.runs.create(
|
22 |
+
thread_id=thread.id,
|
23 |
+
assistant_id=assistant_id
|
24 |
+
)
|
25 |
+
|
26 |
+
response_received = False
|
27 |
+
timeout = 150
|
28 |
+
start_time = time.time()
|
29 |
+
|
30 |
+
while not response_received and time.time() - start_time < timeout:
|
31 |
+
run_status = client.beta.threads.runs.retrieve(
|
32 |
+
thread_id=thread.id,
|
33 |
+
run_id=run.id,
|
34 |
+
)
|
35 |
+
if run_status.status == 'completed':
|
36 |
+
response_received = True
|
37 |
+
else:
|
38 |
+
time.sleep(4)
|
39 |
+
|
40 |
+
if not response_received:
|
41 |
+
return "Response timed out."
|
42 |
+
|
43 |
+
messages = client.beta.threads.messages.list(
|
44 |
+
thread_id=thread.id,
|
45 |
+
)
|
46 |
+
for msg in messages:
|
47 |
+
if msg.role == 'assistant':
|
48 |
+
return msg.content[0]['text']['value']
|
49 |
+
|
50 |
+
return "No response."
|
51 |
+
except Exception as e:
|
52 |
+
return f"An error occurred: {str(e)}"
|
53 |
|
54 |
examples = [
|
55 |
["My Eucalyptus tree is struggling outside in the cold weather in europe"],
|