Islam YAHIAOUI
commited on
Commit
β’
3eb54fe
1
Parent(s):
96f677c
Update UI
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
11 |
|
12 |
TOKEN = os.getenv("HF_TOKEN")
|
13 |
|
14 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta" , token=
|
15 |
system_message ="You are a capable and freindly assistant."
|
16 |
history = []
|
17 |
no_change_btn = gr.Button()
|
@@ -19,16 +19,42 @@ enable_btn = gr.Button(interactive=True)
|
|
19 |
disable_btn = gr.Button(interactive=False)
|
20 |
|
21 |
# ================================================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# ================================================================================================================================
|
23 |
|
24 |
def chat(
|
25 |
state,
|
|
|
26 |
message,
|
27 |
# history: list[tuple[str, str]],
|
28 |
max_tokens,
|
29 |
temperature,
|
30 |
top_p,
|
31 |
):
|
|
|
32 |
print("Message: ", message)
|
33 |
print("History: ", history)
|
34 |
print("System Message: ", system_message)
|
@@ -37,13 +63,10 @@ def chat(
|
|
37 |
print("Top P: ", top_p)
|
38 |
|
39 |
messages = [{"role": "system", "content": system_message}]
|
40 |
-
|
41 |
for val in history:
|
42 |
-
|
43 |
-
|
44 |
-
if val[1]:
|
45 |
-
messages.append({"role": "assistant", "content": val[1]})
|
46 |
-
# message =run_rag(message, history)
|
47 |
|
48 |
messages.append({"role": "user", "content": run_rag(message)})
|
49 |
response = ""
|
@@ -55,17 +78,16 @@ def chat(
|
|
55 |
messages,
|
56 |
|
57 |
max_tokens=max_tokens,
|
58 |
-
stream=
|
59 |
temperature=temperature,
|
60 |
top_p=top_p,
|
61 |
):
|
62 |
token = message.choices[0].delta.content
|
63 |
response += str(token)
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
yield (state, state.to_gradio_chatbot(), "", None) + (enable_btn,) * 5
|
68 |
|
|
|
69 |
# ================================================================================================================================
|
70 |
|
71 |
theme = gr.themes.Base(
|
@@ -105,35 +127,10 @@ top_p = gr.Slider(
|
|
105 |
label="Top-p (nucleus sampling)",
|
106 |
info="Higher values is equivalent to sampling more low-probability tokens.",
|
107 |
)
|
108 |
-
textbox = gr.Textbox(show_label=False, placeholder="Enter text and press ENTER", container=False)
|
109 |
# ================================================================================================================================
|
110 |
|
111 |
-
# with gr.Blocks(
|
112 |
-
# fill_height=True,
|
113 |
-
# css=""".gradio-container .avatar-container {height: 40px width: 40px !important;} #duplicate-button {margin: auto; color: white; background: #f1a139; border-radius: 100vh; margin-top: 2px; margin-bottom: 2px;}""",
|
114 |
-
# ) as main:
|
115 |
-
# gr.ChatInterface(
|
116 |
-
# chat,
|
117 |
-
# chatbot=chatbot,
|
118 |
-
# title="Retrieval Augmented Generation (RAG) Chatbot",
|
119 |
-
# examples=EXAMPLES,
|
120 |
-
# theme=theme,
|
121 |
-
# fill_height=True,
|
122 |
-
# additional_inputs=[
|
123 |
-
|
124 |
-
# max_new_tokens,
|
125 |
-
# temperature,
|
126 |
-
# top_p,
|
127 |
-
# ],
|
128 |
-
# )
|
129 |
-
|
130 |
|
131 |
|
132 |
-
# with gr.Blocks(theme=theme, css="footer {visibility: hidden}textbox{resize:none}", title="RAG") as demo:
|
133 |
-
# gr.TabbedInterface([main ] , tab_names=["Chatbot"] )
|
134 |
-
|
135 |
-
# demo.launch()
|
136 |
-
|
137 |
|
138 |
def upvote_last_response(state):
|
139 |
return ("",) + (disable_btn,) * 3
|
@@ -144,17 +141,6 @@ def downvote_last_response(state):
|
|
144 |
def flag_last_response(state):
|
145 |
return ("",) + (disable_btn,) * 3
|
146 |
|
147 |
-
def add_text(state ,textbox ):
|
148 |
-
print("textbox: ", textbox)
|
149 |
-
if state is None:
|
150 |
-
state = gr.State()
|
151 |
-
state.messages = [[("assistant", "")]]
|
152 |
-
state.text = textbox
|
153 |
-
history=""
|
154 |
-
state.append_message(state.roles[0], textbox)#
|
155 |
-
state.append_message(state.roles[1], "")
|
156 |
-
yield (state, None, history) + (disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
|
157 |
-
|
158 |
block_css = """
|
159 |
#buttons button {
|
160 |
min-width: min(120px,100%);
|
@@ -162,8 +148,9 @@ block_css = """
|
|
162 |
"""
|
163 |
# ================================================================================================================================
|
164 |
|
|
|
165 |
with gr.Blocks(title="CuMo", theme=theme, css=block_css) as demo:
|
166 |
-
state =
|
167 |
gr.Markdown("Retrieval Augmented Generation (RAG) Chatbot" )
|
168 |
with gr.Row():
|
169 |
with gr.Column(scale=8):
|
@@ -172,13 +159,19 @@ with gr.Blocks(title="CuMo", theme=theme, css=block_css) as demo:
|
|
172 |
label="Retrieval Augmented Generation (RAG) Chatbot",
|
173 |
height=400,
|
174 |
layout="bubble",
|
175 |
-
|
176 |
)
|
177 |
with gr.Row():
|
178 |
with gr.Column(scale=8):
|
179 |
-
textbox.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
with gr.Column(scale=1, min_width=100):
|
181 |
submit_btn = gr.Button(value="Submit", variant="primary" )
|
|
|
182 |
with gr.Row(elem_id="buttons") as button_row:
|
183 |
upvote_btn = gr.Button(value="π Upvote", interactive=False)
|
184 |
downvote_btn = gr.Button(value="π Downvote", interactive=False)
|
@@ -200,8 +193,8 @@ with gr.Blocks(title="CuMo", theme=theme, css=block_css) as demo:
|
|
200 |
max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True, label="Max output tokens",)
|
201 |
|
202 |
# ================================================================================================================================
|
203 |
-
|
204 |
btn_list = [upvote_btn, downvote_btn, flag_btn, regenerate_btn, clear_btn]
|
|
|
205 |
upvote_btn.click(
|
206 |
upvote_last_response,
|
207 |
[state],
|
@@ -218,26 +211,19 @@ with gr.Blocks(title="CuMo", theme=theme, css=block_css) as demo:
|
|
218 |
[textbox, upvote_btn, downvote_btn, flag_btn]
|
219 |
)
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
add_text,
|
224 |
[state, textbox],
|
225 |
-
[state,
|
226 |
).then(
|
227 |
chat,
|
228 |
-
[state,
|
229 |
-
[state,
|
|
|
230 |
)
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
[state , textbox],
|
235 |
-
[state,chatbot, textbox] + btn_list,
|
236 |
-
).then(
|
237 |
-
chat,
|
238 |
-
[state, textbox, max_output_tokens , temperature, top_p ],
|
239 |
-
[state,chatbot, textbox] + btn_list,
|
240 |
-
)
|
241 |
# ================================================================================================================================
|
242 |
demo.launch()
|
243 |
# ================================================================================================================================
|
|
|
11 |
|
12 |
TOKEN = os.getenv("HF_TOKEN")
|
13 |
|
14 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta" , token="")
|
15 |
system_message ="You are a capable and freindly assistant."
|
16 |
history = []
|
17 |
no_change_btn = gr.Button()
|
|
|
19 |
disable_btn = gr.Button(interactive=False)
|
20 |
|
21 |
# ================================================================================================================================
|
22 |
+
# ================================================================================================================================
|
23 |
+
def initialiate_state():
|
24 |
+
state = gr.State()
|
25 |
+
state.history = list[tuple[str, str]]
|
26 |
+
state.current_query = ""
|
27 |
+
state.current_response = ""
|
28 |
+
state.roles = ["user", "assistant"]
|
29 |
+
return state
|
30 |
+
|
31 |
+
def add_question(state, textbox):
|
32 |
+
print(textbox)
|
33 |
+
state.current_query = textbox.value
|
34 |
+
state.current_response = ""
|
35 |
+
state.history.append({"role": "user", "content": textbox.value})
|
36 |
+
return state , textbox
|
37 |
+
|
38 |
+
def add_response(state,chatbot, assistant_message):
|
39 |
+
current_question = state.current_query
|
40 |
+
state.current_response = assistant_message
|
41 |
+
state.history.append({"role": "system", "content": assistant_message})
|
42 |
+
chatbot.append((current_question , assistant_message))
|
43 |
+
return state , assistant_message
|
44 |
+
|
45 |
+
|
46 |
# ================================================================================================================================
|
47 |
|
48 |
def chat(
|
49 |
state,
|
50 |
+
chatbot,
|
51 |
message,
|
52 |
# history: list[tuple[str, str]],
|
53 |
max_tokens,
|
54 |
temperature,
|
55 |
top_p,
|
56 |
):
|
57 |
+
|
58 |
print("Message: ", message)
|
59 |
print("History: ", history)
|
60 |
print("System Message: ", system_message)
|
|
|
63 |
print("Top P: ", top_p)
|
64 |
|
65 |
messages = [{"role": "system", "content": system_message}]
|
66 |
+
history= state.history
|
67 |
for val in history:
|
68 |
+
messages.append(val)
|
69 |
+
|
|
|
|
|
|
|
70 |
|
71 |
messages.append({"role": "user", "content": run_rag(message)})
|
72 |
response = ""
|
|
|
78 |
messages,
|
79 |
|
80 |
max_tokens=max_tokens,
|
81 |
+
stream=False,
|
82 |
temperature=temperature,
|
83 |
top_p=top_p,
|
84 |
):
|
85 |
token = message.choices[0].delta.content
|
86 |
response += str(token)
|
87 |
+
add_response(state,message , response)
|
88 |
+
yield (state, response) + (enable_btn,) * 5
|
|
|
|
|
89 |
|
90 |
+
|
91 |
# ================================================================================================================================
|
92 |
|
93 |
theme = gr.themes.Base(
|
|
|
127 |
label="Top-p (nucleus sampling)",
|
128 |
info="Higher values is equivalent to sampling more low-probability tokens.",
|
129 |
)
|
|
|
130 |
# ================================================================================================================================
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
def upvote_last_response(state):
|
136 |
return ("",) + (disable_btn,) * 3
|
|
|
141 |
def flag_last_response(state):
|
142 |
return ("",) + (disable_btn,) * 3
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
block_css = """
|
145 |
#buttons button {
|
146 |
min-width: min(120px,100%);
|
|
|
148 |
"""
|
149 |
# ================================================================================================================================
|
150 |
|
151 |
+
|
152 |
with gr.Blocks(title="CuMo", theme=theme, css=block_css) as demo:
|
153 |
+
state = initialiate_state()
|
154 |
gr.Markdown("Retrieval Augmented Generation (RAG) Chatbot" )
|
155 |
with gr.Row():
|
156 |
with gr.Column(scale=8):
|
|
|
159 |
label="Retrieval Augmented Generation (RAG) Chatbot",
|
160 |
height=400,
|
161 |
layout="bubble",
|
|
|
162 |
)
|
163 |
with gr.Row():
|
164 |
with gr.Column(scale=8):
|
165 |
+
# textbox.render()
|
166 |
+
textbox = gr.Textbox(show_label=False,
|
167 |
+
placeholder="Enter text and press ENTER",
|
168 |
+
container=False,
|
169 |
+
show_copy_button=True
|
170 |
+
)
|
171 |
+
|
172 |
with gr.Column(scale=1, min_width=100):
|
173 |
submit_btn = gr.Button(value="Submit", variant="primary" )
|
174 |
+
|
175 |
with gr.Row(elem_id="buttons") as button_row:
|
176 |
upvote_btn = gr.Button(value="π Upvote", interactive=False)
|
177 |
downvote_btn = gr.Button(value="π Downvote", interactive=False)
|
|
|
193 |
max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True, label="Max output tokens",)
|
194 |
|
195 |
# ================================================================================================================================
|
|
|
196 |
btn_list = [upvote_btn, downvote_btn, flag_btn, regenerate_btn, clear_btn]
|
197 |
+
|
198 |
upvote_btn.click(
|
199 |
upvote_last_response,
|
200 |
[state],
|
|
|
211 |
[textbox, upvote_btn, downvote_btn, flag_btn]
|
212 |
)
|
213 |
|
214 |
+
submit_btn.click(
|
215 |
+
add_question,
|
|
|
216 |
[state, textbox],
|
217 |
+
[state, textbox],
|
218 |
).then(
|
219 |
chat,
|
220 |
+
[state, chatbot, textbox , max_new_tokens , temperature ,top_p],
|
221 |
+
[state, textbox] + btn_list,
|
222 |
+
|
223 |
)
|
224 |
|
225 |
+
|
226 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
# ================================================================================================================================
|
228 |
demo.launch()
|
229 |
# ================================================================================================================================
|