saad-k7 commited on
Commit
3654597
1 Parent(s): 67b1bcc

init commit

Browse files
Files changed (2) hide show
  1. app.py +111 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+ from dotenv import load_dotenv
5
+ from pathlib import Path
6
+ import json
7
+ load_dotenv()
8
+
9
+ # client = OpenAI()
10
+ client = OpenAI()
11
+
12
+ assistant_id = 'asst_4DJlWbqXooIm6hzJYBW5TSpX'
13
+
14
+
15
+ def wait_on_run(run, thread):
16
+ while run.status == "queued" or run.status == "in_progress":
17
+ run = client.beta.threads.runs.retrieve(
18
+ thread_id=thread,
19
+ run_id=run.id,
20
+ )
21
+ time.sleep(0.5)
22
+ return run
23
+
24
+ # thread = client.beta.threads.create()
25
+ # print("first print thread ID", thread.id)
26
+
27
+
28
+ def send_message(message, thread_id):
29
+ message = client.beta.threads.messages.create(thread_id, role='user', content=message)
30
+ run = client.beta.threads.runs.create(
31
+ thread_id=thread_id,
32
+ assistant_id=assistant_id,
33
+ )
34
+ run = wait_on_run(run, thread_id)
35
+ messages = client.beta.threads.messages.list(thread_id=thread_id)
36
+ if run.status == "requires_action":
37
+ tool_call = run.required_action.submit_tool_outputs.tool_calls[0]
38
+ # name = tool_call.function.name
39
+ # arguments = json.loads(tool_call.function.arguments)
40
+ run = client.beta.threads.runs.submit_tool_outputs(
41
+ thread_id=thread_id,
42
+ run_id=run.id,
43
+ tool_outputs=[
44
+ {"tool_call_id": tool_call.id, "output": json.dumps("true")}
45
+ ]
46
+ )
47
+
48
+ try:
49
+ print("second print thread_id", thread_id)
50
+ run = wait_on_run(run, thread_id)
51
+ except:
52
+ # print("Third print thread", thread)
53
+ # run = wait_on_run(run, thread)
54
+ print("Third print thread", thread_id)
55
+ messages = client.beta.threads.messages.list(thread_id=thread_id)
56
+ last_message = messages.data[0].content[0].text.value
57
+ return last_message
58
+
59
+ def get_thread_id():
60
+ thread = client.beta.threads.create()
61
+ print("first print thread ID", thread.id)
62
+ return thread.id
63
+
64
+ def file_update_for_threads(filepath, user_message):
65
+ filename = filepath.name
66
+ file = client.files.create(
67
+ file=open(filename, "rb"),
68
+ purpose="assistants",
69
+ )
70
+ thread = client.beta.threads.create(
71
+ messages=[
72
+ {
73
+ "role": "user",
74
+ "content": user_message,
75
+ "attachments": [
76
+ { "file_id": file.id, "tools": [{"type": "file_search"}] }
77
+ ],
78
+ }
79
+ ]
80
+ )
81
+ run = client.beta.threads.runs.create_and_poll(
82
+ thread_id=thread.id, assistant_id=assistant_id
83
+ )
84
+ messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
85
+ message_content = messages[0].content[0].text.value
86
+ return message_content, thread.id
87
+
88
+ with gr.Blocks() as demo:
89
+ with gr.Row():
90
+ file_upload = gr.File(label="Upload your File")
91
+ user_message_input = gr.Textbox(label="Enter your message")
92
+ with gr.Row():
93
+ output_message = gr.Textbox(label="Response", interactive=False)
94
+ output_thread_id = gr.Textbox(label="Thread ID", interactive=False)
95
+ upload_button = gr.Button("Upload File and Send Message")
96
+
97
+ upload_button.click(
98
+ file_update_for_threads,
99
+ inputs=[file_upload, user_message_input],
100
+ outputs=[output_message, output_thread_id]
101
+ )
102
+ # get_thread = gr.Textbox(label="Get Thread ID")
103
+ # get_thread_d = gr.Button("Get Thread ID")
104
+ # get_thread_d.click(fn=get_thread_id, outputs=get_thread, api_name="get_thread_id")
105
+ message_to_assistant = gr.Textbox(label="Send Message to the the current thread")
106
+ thread_id = gr.Textbox(label="Thread ID")
107
+ output = gr.Textbox(label="Output response from the Assistant")
108
+ greet_btn = gr.Button("Send Message")
109
+ greet_btn.click(fn=send_message, inputs=[message_to_assistant, thread_id], outputs=output, api_name="send_message")
110
+
111
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.31.5
2
+ openai==1.30.1
3
+ python-dotenv==1.0.0
4
+