Spaces:
Runtime error
Runtime error
Commit
·
c85385e
1
Parent(s):
d4a2e62
Implemented facility chat
Browse files- app.py +115 -50
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,65 +1,130 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
"""
|
6 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
7 |
-
"""
|
8 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
def respond(
|
12 |
-
message,
|
13 |
-
history: list[tuple[str, str]],
|
14 |
-
system_message,
|
15 |
-
max_tokens,
|
16 |
-
temperature,
|
17 |
-
top_p,
|
18 |
-
):
|
19 |
-
messages = [{"role": "system", "content": system_message}]
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
messages.append({"role": "user", "content": val[0]})
|
24 |
-
if val[1]:
|
25 |
-
messages.append({"role": "assistant", "content": val[1]})
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
secrets = os.environ['openAIToken']
|
47 |
-
demo = gr.ChatInterface(
|
48 |
-
respond,
|
49 |
-
additional_inputs=[
|
50 |
-
gr.Textbox(value=secrets, label="System message"),
|
51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
-
],
|
61 |
-
)
|
62 |
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
-
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
import gradio as gr
|
|
|
3 |
import os
|
4 |
+
|
5 |
+
client = OpenAI(api_key=os.environ['openAIToken'])
|
6 |
+
assistantId = os.environ['assistantId']
|
7 |
|
8 |
+
def createThread():
|
9 |
+
return client.beta.threads.create()
|
10 |
+
|
11 |
+
def addMessage(text, threadId):
|
12 |
+
return client.beta.threads.messages.create(
|
13 |
+
thread_id=threadId,
|
14 |
+
role="user",
|
15 |
+
content=text
|
16 |
+
)
|
17 |
+
|
18 |
+
list_of_suggestions = []
|
19 |
+
latest_suggestions = ""
|
20 |
+
|
21 |
+
def handle_suggestions(string_of_suggestions):
|
22 |
+
local_message = None
|
23 |
+
parts = string_of_suggestions.split('#s#')
|
24 |
+
list_of_suggestions = [x.strip('"') for x in parts[0].strip('][').split('", ') if x.strip('"')]
|
25 |
+
print(list_of_suggestions)
|
26 |
+
if len(parts) > 1:
|
27 |
+
local_message = parts[1]
|
28 |
+
return list_of_suggestions, local_message
|
29 |
+
|
30 |
+
def create_suggestions_list(suggestions):
|
31 |
+
update_show = [gr.update(visible=True, value=w) for w in suggestions]
|
32 |
+
update_hide = [gr.update(visible=False, value="") for _ in range(6-len(suggestions))]
|
33 |
+
return update_show + update_hide
|
34 |
+
|
35 |
+
btn_list = []
|
36 |
+
list_of_suggestions = ""
|
37 |
+
|
38 |
+
CSS ="""
|
39 |
+
.contain { display: flex; flex-direction: column; }
|
40 |
+
.gradio-container { height: 100vh !important; }
|
41 |
+
#chatbot { flex-grow: 1; overflow: auto;}
|
42 |
"""
|
|
|
|
|
|
|
43 |
|
44 |
+
with gr.Blocks(css=CSS, fill_height=True) as demo:
|
45 |
+
streaming_thread = createThread()
|
46 |
+
chatbot = gr.Chatbot(label="Facility managment bot", elem_id="chatbot") #just to fit the notebook
|
47 |
+
with gr.Row():
|
48 |
+
for i in range(6):
|
49 |
+
btn = gr.Button(visible=False)
|
50 |
+
btn_list.append(btn)
|
51 |
+
msg = gr.Textbox(label="Answer")
|
52 |
+
with gr.Row():
|
53 |
+
clear_btn = gr.ClearButton(chatbot)
|
54 |
+
btn = gr.Button("Submit")
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
def user(user_message, history):
|
58 |
+
return "", history + [[user_message, None]]
|
|
|
|
|
|
|
59 |
|
60 |
+
def respond(chat_history):
|
61 |
+
print("Responding")
|
62 |
+
global btn_list
|
63 |
+
message = chat_history[-1][0]
|
64 |
+
threadId = streaming_thread.id
|
65 |
+
chat_history[-1][1] = ""
|
66 |
+
addMessage(message, threadId)
|
67 |
+
global list_of_suggestions
|
68 |
+
list_of_suggestions = []
|
69 |
+
string_of_suggestions = ""
|
70 |
+
is_loading_suggestions = False
|
71 |
+
is_it_first_response = True
|
72 |
+
with client.beta.threads.runs.stream(
|
73 |
+
thread_id=threadId,
|
74 |
+
assistant_id=assistantId,
|
75 |
+
) as stream:
|
76 |
+
for text in stream.text_deltas:
|
77 |
+
print(text, end="")
|
78 |
+
local_message = None
|
79 |
+
if "[" in text and is_it_first_response:
|
80 |
+
is_loading_suggestions = True
|
81 |
|
82 |
+
is_it_first_response = False
|
83 |
|
84 |
+
if is_loading_suggestions != True:
|
85 |
+
local_message = text
|
86 |
+
else:
|
87 |
+
string_of_suggestions = string_of_suggestions + text
|
88 |
+
if "#s#" in string_of_suggestions:
|
89 |
+
is_loading_suggestions = False
|
90 |
+
list_of_suggestions, local_message = handle_suggestions(string_of_suggestions)
|
91 |
+
if local_message is not None:
|
92 |
+
chat_history[-1][1] += local_message
|
93 |
+
yield {chatbot: chat_history}
|
94 |
|
95 |
+
stream.until_done()
|
96 |
+
|
97 |
+
def update_suggestions():
|
98 |
+
global list_of_suggestions
|
99 |
+
btn_list = create_suggestions_list(list_of_suggestions)
|
100 |
+
return btn_list
|
101 |
+
|
102 |
+
def hide_suggestions():
|
103 |
+
return [gr.update(visible=False, value="") for _ in range(6)]
|
104 |
+
|
105 |
+
def disable_msg():
|
106 |
+
message_box = gr.Textbox(value=None, interactive=False)
|
107 |
+
return message_box
|
108 |
+
|
109 |
+
def enable_msg():
|
110 |
+
message_box = gr.Textbox(value=None, interactive=True)
|
111 |
+
return message_box
|
112 |
+
|
113 |
+
add_user_message_flow = [user, [msg,chatbot], [msg,chatbot]]
|
114 |
+
chat_response_flow = [respond, [chatbot], [chatbot]]
|
115 |
+
update_suggestions_flow = [update_suggestions, None, btn_list]
|
116 |
+
hide_suggestions_flow = [hide_suggestions, None, btn_list]
|
117 |
+
disable_msg_flow = [disable_msg, None, msg]
|
118 |
+
enable_msg_flow = [enable_msg, None, msg]
|
119 |
+
|
120 |
+
btn.click(*add_user_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
|
121 |
+
msg.submit(*add_user_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
|
122 |
+
for sug_btn in btn_list:
|
123 |
+
add_suggestion_message_flow = [user, [sug_btn, chatbot], [msg, chatbot]]
|
124 |
+
sug_btn.click(*add_suggestion_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
+
demo.launch()
|
130 |
+
gr.close_all()
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
huggingface_hub==0.22.2
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
openai==1.28.0
|