Tonic commited on
Commit
189f24f
·
1 Parent(s): 5c2d571

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -19,26 +19,27 @@ OpenAI doesnt let you use Agents without paying for it, so I made you an interfa
19
  Big thanks to 🤗Huggingface Organisation for the🫂Community Grant"""
20
 
21
  examples = [
22
- ["My Eucalyptus tree is struggling outside in the cold weather in Europe"],
23
- ["My calathea house plant is yellowing."],
24
- ["We have a cactus at work that suddenly started yellowing and wilting."]
25
  ]
26
 
27
  load_dotenv()
28
  openai.api_key = os.getenv('OPENAI_API_KEY')
29
  assistant_id = os.getenv('ASSISTANT_ID')
30
  client = openai.OpenAI(api_key=openai.api_key)
 
31
 
32
- def ask_openai(question):
33
- global current_thread_id
34
 
35
  try:
36
-
37
- if current_thread_id is None:
38
  thread = client.beta.threads.create()
39
  current_thread_id = thread.id
 
40
  else:
41
- thread = client.beta.threads.retrieve(thread_id=current_thread_id)
42
 
43
  client.beta.threads.messages.create(
44
  thread_id=current_thread_id,
@@ -92,7 +93,13 @@ iface = gr.Interface(
92
  title=title,
93
  description=description,
94
  fn=ask_openai,
95
- inputs=gr.Textbox(lines=5, placeholder="Hi there, I have a plant that's..."),
 
 
 
 
96
  outputs=gr.Markdown(),
97
  examples=examples
98
- )
 
 
 
19
  Big thanks to 🤗Huggingface Organisation for the🫂Community Grant"""
20
 
21
  examples = [
22
+ ["My Eucalyptus tree is struggling outside in the cold weather in Europe",True, None],
23
+ ["My calathea house plant is yellowing.",True, None],
24
+ ["We have a cactus at work that suddenly started yellowing and wilting.",True, None]
25
  ]
26
 
27
  load_dotenv()
28
  openai.api_key = os.getenv('OPENAI_API_KEY')
29
  assistant_id = os.getenv('ASSISTANT_ID')
30
  client = openai.OpenAI(api_key=openai.api_key)
31
+ thread_ids = {}
32
 
33
+ def ask_openai(question, start_new_thread=True, selected_thread_id=None):
34
+ global thread_ids
35
 
36
  try:
37
+ if start_new_thread or selected_thread_id not in thread_ids:
 
38
  thread = client.beta.threads.create()
39
  current_thread_id = thread.id
40
+ thread_ids[current_thread_id] = thread.id
41
  else:
42
+ current_thread_id = thread_ids[selected_thread_id]
43
 
44
  client.beta.threads.messages.create(
45
  thread_id=current_thread_id,
 
93
  title=title,
94
  description=description,
95
  fn=ask_openai,
96
+ inputs=[
97
+ gr.Textbox(lines=5, placeholder="Hi there, I have a plant that's..."),
98
+ gr.Checkbox(label="Start a new conversation thread"),
99
+ gr.Dropdown(label="Select previous thread", choices=list(thread_ids.keys()))
100
+ ],
101
  outputs=gr.Markdown(),
102
  examples=examples
103
+ )
104
+
105
+ iface.launch()