| 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) |
| 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) |