Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
api_key = os.environ["OPENAI_API_KEY_PUBLIC"]
|
| 5 |
+
|
| 6 |
+
class ask_me:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.client = OpenAI(api_key=api_key)
|
| 9 |
+
self.thread = self.client.beta.threads.create()
|
| 10 |
+
|
| 11 |
+
def ask(self,question):
|
| 12 |
+
run = self.client.beta.threads.runs.create(
|
| 13 |
+
thread_id=self.thread.id,
|
| 14 |
+
assistant_id='asst_cqL6gztTsqBGDdkKegpcQ32Y',
|
| 15 |
+
instructions="How many MOF with Cu?"
|
| 16 |
+
)
|
| 17 |
+
while run.status in ['queued', 'in_progress', 'cancelling']:
|
| 18 |
+
time.sleep(1) # Wait for 1 second
|
| 19 |
+
run = self.client.beta.threads.runs.retrieve(
|
| 20 |
+
thread_id=self.thread.id,
|
| 21 |
+
run_id=run.id
|
| 22 |
+
)
|
| 23 |
+
if run.status == 'completed':
|
| 24 |
+
messages = self.client.beta.threads.messages.list(
|
| 25 |
+
thread_id=self.thread.id
|
| 26 |
+
)
|
| 27 |
+
return messages.data[0].content[0].text.value
|
| 28 |
+
else:
|
| 29 |
+
return run.status
|
| 30 |
+
|
| 31 |
+
def clear(self):
|
| 32 |
+
self.thread = self.client.beta.threads.create()
|
| 33 |
+
return "New search is there."
|
| 34 |
+
def wait():
|
| 35 |
+
time.sleep(1)
|
| 36 |
+
return "Waiting for the answer."
|
| 37 |
+
|
| 38 |
+
temp = ask_me()
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column(scale=5):
|
| 43 |
+
search_box = gr.Textbox(label= "Your questions",value="How many MOF with Cu?",interactive = True)
|
| 44 |
+
with gr.Column(scale=2):
|
| 45 |
+
sub = gr.Button("Ask it!")
|
| 46 |
+
clear = gr.Button("New search")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
text = gr.Textbox(label= "Result",value="Answer is out there.")
|
| 50 |
+
|
| 51 |
+
sub.click(temp.ask,inputs=search_box,outputs=text)
|
| 52 |
+
clear.click(temp.clear,outputs=text)
|
| 53 |
+
demo.launch(debug=True)
|