File size: 1,784 Bytes
4ba5085 163590e 4ba5085 b3b65c7 4ba5085 163590e 4ba5085 |
1 2 3 4 5 6 7 8 9 10 11 12 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import gradio as gr
import time, os
from openai import OpenAI
api_key = os.environ["OPENAI_API_KEY_PUBLIC"]
class ask_me:
def __init__(self):
self.client = OpenAI(api_key=api_key)
self.thread = self.client.beta.threads.create()
def ask(self,question):
run = self.client.beta.threads.runs.create(
thread_id=self.thread.id,
assistant_id='asst_cqL6gztTsqBGDdkKegpcQ32Y',
instructions=question
)
while run.status in ['queued', 'in_progress', 'cancelling']:
time.sleep(1) # Wait for 1 second
run = self.client.beta.threads.runs.retrieve(
thread_id=self.thread.id,
run_id=run.id
)
if run.status == 'completed':
messages = self.client.beta.threads.messages.list(
thread_id=self.thread.id
)
return messages.data[0].content[0].text.value
else:
return run.status
def clear(self):
self.thread = self.client.beta.threads.create()
return "New search is there."
def wait():
time.sleep(1)
return "Waiting for the answer."
temp = ask_me()
with gr.Blocks(css="footer {visibility: hidden}") as demo:
with gr.Row():
with gr.Column(scale=5):
search_box = gr.Textbox(label= "Your questions",value="How many MOF with Cu?",interactive = True)
with gr.Column(scale=2):
sub = gr.Button("Ask it!")
clear = gr.Button("New search")
with gr.Row():
text = gr.Textbox(label= "Result",value="Answer is out there.")
sub.click(temp.ask,inputs=search_box,outputs=text)
clear.click(temp.clear,outputs=text)
demo.launch(debug=True) |