Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
|
4 |
+
|
5 |
+
title="Wecome to Tonic's Bulbi Plant Doctor",
|
6 |
+
description="""Introduce your plant below. Respond with additional information when prompted. Save your plants with Bulbi Plant Doctor"""
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
10 |
+
assistant_id=os.getenv('ASSISTANT_ID')
|
11 |
+
client = openai.OpenAI(api_key=openai.api_key)
|
12 |
+
|
13 |
+
def ask_openai(question):
|
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 |
+
run = client.beta.threads.runs.create(
|
21 |
+
thread_id=thread.id,
|
22 |
+
)
|
23 |
+
run = client.beta.threads.runs.retrieve(
|
24 |
+
thread_id=thread.id,
|
25 |
+
run_id=run.id
|
26 |
+
)
|
27 |
+
|
28 |
+
messages = client.beta.threads.messages.list(
|
29 |
+
thread_id=thread.id
|
30 |
+
)
|
31 |
+
|
32 |
+
response = next((msg for msg in messages['data'] if msg['role'] == 'assistant'), None)
|
33 |
+
return response['content'][0]['text']['value'] if response else "No response."
|
34 |
+
|
35 |
+
examples = [
|
36 |
+
["My Eucalyptus tree is struggling outside in the cold weather in europe"],
|
37 |
+
["My callatea house plant is yellowing."],
|
38 |
+
["We have a catcus as work that suddently started yellowing and wilting."]
|
39 |
+
]
|
40 |
+
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=ask_openai,
|
43 |
+
inputs=gr.Textbox(lines=2, placeholder="Hi there, I have a plant that's..."),
|
44 |
+
outputs=gr.outputs.Markdown(),
|
45 |
+
title=title,
|
46 |
+
description=description
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|