Tonic commited on
Commit
551a0b5
Β·
1 Parent(s): 05b9190

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -4,29 +4,39 @@ from dotenv import load_dotenv
4
  import os
5
  import time
6
 
 
 
 
7
  examples = [
8
- ["My Eucalyptus tree is struggling outside in the cold weather in europe"],
9
- ["My callatea house plant is yellowing."],
10
- ["We have a catcus as work that suddently started yellowing and wilting."]
11
  ]
12
 
13
  load_dotenv()
14
  openai.api_key = os.getenv('OPENAI_API_KEY')
15
- assistant_id=os.getenv('ASSISTANT_ID')
16
  client = openai.OpenAI(api_key=openai.api_key)
17
 
18
  def ask_openai(question):
 
 
19
  try:
20
- thread = client.beta.threads.create()
 
 
 
 
 
21
 
22
  client.beta.threads.messages.create(
23
- thread_id=thread.id,
24
  role="user",
25
  content=question,
26
  )
27
 
28
  run = client.beta.threads.runs.create(
29
- thread_id=thread.id,
30
  assistant_id=assistant_id
31
  )
32
 
@@ -36,19 +46,19 @@ def ask_openai(question):
36
 
37
  while not response_received and time.time() - start_time < timeout:
38
  run_status = client.beta.threads.runs.retrieve(
39
- thread_id=thread.id,
40
  run_id=run.id,
41
  )
42
  if run_status.status == 'completed':
43
  response_received = True
44
  else:
45
- time.sleep(4)
46
 
47
  if not response_received:
48
  return "Response timed out."
49
 
50
  steps = client.beta.threads.runs.steps.list(
51
- thread_id=thread.id,
52
  run_id=run.id
53
  )
54
 
@@ -57,13 +67,13 @@ def ask_openai(question):
57
  if last_step.type == 'message_creation':
58
  message_id = last_step.step_details.message_creation.message_id
59
  message = client.beta.threads.messages.retrieve(
60
- thread_id=thread.id,
61
  message_id=message_id
62
  )
63
  if message.content and message.content[0].type == 'text':
64
  return message.content[0].text.value
65
  return "No response."
66
-
67
  except Exception as e:
68
  return f"An error occurred: {str(e)}"
69
 
@@ -71,9 +81,9 @@ iface = gr.Interface(
71
  fn=ask_openai,
72
  inputs=gr.Textbox(lines=5, placeholder="Hi there, I have a plant that's..."),
73
  outputs=gr.Markdown(),
74
- title="Wecome to Tonic's Bulbi Plant Doctor",
75
  description="""Introduce your plant below. Be as descriptive as possible. Respond with additional information when prompted. Save your plants with Bulbi Plant Doctor""",
76
  examples=examples
77
  )
78
 
79
- iface.launch()
 
4
  import os
5
  import time
6
 
7
+ # Global variable to store the current thread ID
8
+ current_thread_id = None
9
+
10
  examples = [
11
+ ["My Eucalyptus tree is struggling outside in the cold weather in Europe"],
12
+ ["My calathea house plant is yellowing."],
13
+ ["We have a cactus at work that suddenly started yellowing and wilting."]
14
  ]
15
 
16
  load_dotenv()
17
  openai.api_key = os.getenv('OPENAI_API_KEY')
18
+ assistant_id = os.getenv('ASSISTANT_ID')
19
  client = openai.OpenAI(api_key=openai.api_key)
20
 
21
  def ask_openai(question):
22
+ global current_thread_id
23
+
24
  try:
25
+
26
+ if current_thread_id is None:
27
+ thread = client.beta.threads.create()
28
+ current_thread_id = thread.id
29
+ else:
30
+ thread = client.beta.threads.retrieve(thread_id=current_thread_id)
31
 
32
  client.beta.threads.messages.create(
33
+ thread_id=current_thread_id,
34
  role="user",
35
  content=question,
36
  )
37
 
38
  run = client.beta.threads.runs.create(
39
+ thread_id=current_thread_id,
40
  assistant_id=assistant_id
41
  )
42
 
 
46
 
47
  while not response_received and time.time() - start_time < timeout:
48
  run_status = client.beta.threads.runs.retrieve(
49
+ thread_id=current_thread_id,
50
  run_id=run.id,
51
  )
52
  if run_status.status == 'completed':
53
  response_received = True
54
  else:
55
+ time.sleep(4)
56
 
57
  if not response_received:
58
  return "Response timed out."
59
 
60
  steps = client.beta.threads.runs.steps.list(
61
+ thread_id=current_thread_id,
62
  run_id=run.id
63
  )
64
 
 
67
  if last_step.type == 'message_creation':
68
  message_id = last_step.step_details.message_creation.message_id
69
  message = client.beta.threads.messages.retrieve(
70
+ thread_id=current_thread_id,
71
  message_id=message_id
72
  )
73
  if message.content and message.content[0].type == 'text':
74
  return message.content[0].text.value
75
  return "No response."
76
+
77
  except Exception as e:
78
  return f"An error occurred: {str(e)}"
79
 
 
81
  fn=ask_openai,
82
  inputs=gr.Textbox(lines=5, placeholder="Hi there, I have a plant that's..."),
83
  outputs=gr.Markdown(),
84
+ title="Welcome to Tonic's Bulbi Plant Doctor",
85
  description="""Introduce your plant below. Be as descriptive as possible. Respond with additional information when prompted. Save your plants with Bulbi Plant Doctor""",
86
  examples=examples
87
  )
88
 
89
+ iface.launch()