Spaces:
Sleeping
Sleeping
Commit
·
2ddb996
1
Parent(s):
90db298
don't seem to work mmh
Browse files
app.py
CHANGED
@@ -3,10 +3,6 @@ from typing import List, Tuple
|
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
|
6 |
-
# ======================
|
7 |
-
# Chatbot configuration
|
8 |
-
# ======================
|
9 |
-
|
10 |
class ChatConfig:
|
11 |
MODEL = "mistralai/Mistral-7B-Instruct-v0.1"
|
12 |
DEFAULT_SYSTEM_MSG = "You are an extremely smart and useful Chatbot."
|
@@ -15,8 +11,7 @@ class ChatConfig:
|
|
15 |
DEFAULT_TOP_P = 0.95
|
16 |
|
17 |
HF_TOKEN = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
18 |
-
# If needed
|
19 |
-
# HF_TOKEN = "hf_YOUR_TOKEN_HERE"
|
20 |
|
21 |
client = InferenceClient(ChatConfig.MODEL, token=HF_TOKEN)
|
22 |
|
@@ -28,19 +23,12 @@ def generate_response(
|
|
28 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
29 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
30 |
):
|
31 |
-
"""
|
32 |
-
Streams tokens from the model.
|
33 |
-
"""
|
34 |
-
# Prepare messages with system prompt
|
35 |
messages = [{"role": "system", "content": system_message}]
|
36 |
-
|
37 |
-
# Add conversation history
|
38 |
for user_msg, bot_msg in history:
|
39 |
if user_msg:
|
40 |
messages.append({"role": "user", "content": user_msg})
|
41 |
if bot_msg:
|
42 |
messages.append({"role": "assistant", "content": bot_msg})
|
43 |
-
# Add newest user message
|
44 |
messages.append({"role": "user", "content": message})
|
45 |
|
46 |
response = ""
|
@@ -53,15 +41,10 @@ def generate_response(
|
|
53 |
):
|
54 |
token = chunk.choices[0].delta.content or ""
|
55 |
response += token
|
56 |
-
# yield partial response
|
57 |
yield response
|
58 |
|
59 |
-
# ======================
|
60 |
-
# Build the Gradio UI
|
61 |
-
# ======================
|
62 |
-
|
63 |
def create_interface():
|
64 |
-
#
|
65 |
custom_css = """
|
66 |
body {
|
67 |
background: linear-gradient(to right, #a8c0ff, #fafdff);
|
@@ -76,16 +59,16 @@ def create_interface():
|
|
76 |
padding: 10px;
|
77 |
}
|
78 |
.chatbot .user-message {
|
79 |
-
background-color: #b3e5fc;
|
80 |
-
color: #0d47a1;
|
81 |
}
|
82 |
.chatbot .bot-message {
|
83 |
-
background-color: #e3f2fd;
|
84 |
color: #0d47a1;
|
85 |
}
|
86 |
.gr-button {
|
87 |
-
background-color: #2196f3;
|
88 |
-
color: #ffffff;
|
89 |
border: none;
|
90 |
border-radius: 8px;
|
91 |
padding: 8px 16px;
|
@@ -93,31 +76,27 @@ def create_interface():
|
|
93 |
font-weight: bold;
|
94 |
}
|
95 |
.gr-button:hover {
|
96 |
-
background-color: #1976d2;
|
97 |
}
|
98 |
"""
|
99 |
|
100 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
101 |
-
gr.Markdown(
|
102 |
-
"<h1 style='text-align: center;'>Welcome to Happy Gemma Chat!</h1>"
|
103 |
-
)
|
104 |
|
105 |
-
# Chatbot display
|
106 |
chatbot = gr.Chatbot(
|
107 |
label="Gemma Chat",
|
108 |
-
avatar_images=("./user.png", "./gemma.png"),
|
109 |
height=450,
|
110 |
show_copy_button=True
|
111 |
)
|
112 |
|
113 |
-
#
|
114 |
state = gr.State([])
|
115 |
|
116 |
-
# Additional/Advanced settings
|
117 |
with gr.Accordion("Advanced Settings", open=False):
|
118 |
system_msg = gr.Textbox(
|
119 |
value=ChatConfig.DEFAULT_SYSTEM_MSG,
|
120 |
-
label="System Prompt
|
121 |
lines=2
|
122 |
)
|
123 |
max_tokens = gr.Slider(
|
@@ -125,76 +104,64 @@ def create_interface():
|
|
125 |
maximum=8192,
|
126 |
value=ChatConfig.DEFAULT_MAX_TOKENS,
|
127 |
step=1,
|
128 |
-
label="Max Tokens"
|
129 |
-
info="Controls response length"
|
130 |
)
|
131 |
temperature = gr.Slider(
|
132 |
minimum=0.1,
|
133 |
maximum=1.0,
|
134 |
value=ChatConfig.DEFAULT_TEMP,
|
135 |
step=0.1,
|
136 |
-
label="Temperature"
|
137 |
-
info="Controls randomness"
|
138 |
)
|
139 |
top_p = gr.Slider(
|
140 |
minimum=0.1,
|
141 |
maximum=1.0,
|
142 |
value=ChatConfig.DEFAULT_TOP_P,
|
143 |
step=0.05,
|
144 |
-
label="Top-P"
|
145 |
-
info="Controls diversity"
|
146 |
)
|
147 |
|
148 |
-
#
|
149 |
msg_box = gr.Textbox(
|
150 |
-
label="Type your message here
|
151 |
-
placeholder="
|
152 |
)
|
153 |
|
154 |
-
# -- SUBMIT EVENT 1: Collect user's message into state
|
155 |
def user_submit(user_message, chat_history):
|
156 |
-
"""
|
157 |
-
When user hits Enter, we clear the textbox
|
158 |
-
and add a new (user_message, None) to the history.
|
159 |
-
"""
|
160 |
if not user_message.strip():
|
161 |
return "", chat_history
|
162 |
-
# Add
|
163 |
chat_history = chat_history + [[user_message, None]]
|
|
|
164 |
return "", chat_history
|
165 |
|
166 |
-
# -- SUBMIT EVENT 2: Generate the bot's response
|
167 |
def bot_reply(chat_history, system_text, max_toks, temp, top):
|
168 |
-
|
169 |
-
|
170 |
-
"""
|
171 |
-
# The last item in chat_history is the user message we just appended
|
172 |
-
user_msg = chat_history[-1][0]
|
173 |
-
|
174 |
-
# Stream tokens from generate_response
|
175 |
stream_resp = generate_response(
|
176 |
message=user_msg,
|
177 |
-
history=chat_history[:-1],
|
178 |
system_message=system_text,
|
179 |
max_tokens=max_toks,
|
180 |
temperature=temp,
|
181 |
top_p=top
|
182 |
)
|
183 |
-
#
|
184 |
-
for
|
185 |
-
chat_history[-1][1] =
|
186 |
-
yield
|
187 |
-
|
188 |
-
|
189 |
-
#
|
190 |
-
#
|
|
|
191 |
(
|
192 |
msg_box.submit(
|
193 |
fn=user_submit,
|
194 |
inputs=[msg_box, state],
|
195 |
outputs=[msg_box, state],
|
196 |
-
#
|
197 |
-
js="new Audio('submit_sound.mp3').play();"
|
198 |
)
|
199 |
.then(
|
200 |
fn=bot_reply,
|
@@ -207,14 +174,7 @@ def create_interface():
|
|
207 |
|
208 |
def main():
|
209 |
app = create_interface()
|
210 |
-
app.launch(
|
211 |
-
server_name="0.0.0.0",
|
212 |
-
server_port=7860,
|
213 |
-
share=False,
|
214 |
-
show_api=False,
|
215 |
-
show_error=True,
|
216 |
-
debug=True
|
217 |
-
)
|
218 |
|
219 |
if __name__ == "__main__":
|
220 |
main()
|
|
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
|
|
|
|
|
|
|
|
|
6 |
class ChatConfig:
|
7 |
MODEL = "mistralai/Mistral-7B-Instruct-v0.1"
|
8 |
DEFAULT_SYSTEM_MSG = "You are an extremely smart and useful Chatbot."
|
|
|
11 |
DEFAULT_TOP_P = 0.95
|
12 |
|
13 |
HF_TOKEN = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
14 |
+
# HF_TOKEN = "hf_Your_Token_Here" # If needed
|
|
|
15 |
|
16 |
client = InferenceClient(ChatConfig.MODEL, token=HF_TOKEN)
|
17 |
|
|
|
23 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
24 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
25 |
):
|
|
|
|
|
|
|
|
|
26 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
27 |
for user_msg, bot_msg in history:
|
28 |
if user_msg:
|
29 |
messages.append({"role": "user", "content": user_msg})
|
30 |
if bot_msg:
|
31 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
32 |
messages.append({"role": "user", "content": message})
|
33 |
|
34 |
response = ""
|
|
|
41 |
):
|
42 |
token = chunk.choices[0].delta.content or ""
|
43 |
response += token
|
|
|
44 |
yield response
|
45 |
|
|
|
|
|
|
|
|
|
46 |
def create_interface():
|
47 |
+
# Lighter/happier blue color theme
|
48 |
custom_css = """
|
49 |
body {
|
50 |
background: linear-gradient(to right, #a8c0ff, #fafdff);
|
|
|
59 |
padding: 10px;
|
60 |
}
|
61 |
.chatbot .user-message {
|
62 |
+
background-color: #b3e5fc;
|
63 |
+
color: #0d47a1;
|
64 |
}
|
65 |
.chatbot .bot-message {
|
66 |
+
background-color: #e3f2fd;
|
67 |
color: #0d47a1;
|
68 |
}
|
69 |
.gr-button {
|
70 |
+
background-color: #2196f3;
|
71 |
+
color: #ffffff;
|
72 |
border: none;
|
73 |
border-radius: 8px;
|
74 |
padding: 8px 16px;
|
|
|
76 |
font-weight: bold;
|
77 |
}
|
78 |
.gr-button:hover {
|
79 |
+
background-color: #1976d2;
|
80 |
}
|
81 |
"""
|
82 |
|
83 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
84 |
+
gr.Markdown("<h1 style='text-align: center;'>Welcome to Happy Gemma Chat!</h1>")
|
|
|
|
|
85 |
|
|
|
86 |
chatbot = gr.Chatbot(
|
87 |
label="Gemma Chat",
|
88 |
+
avatar_images=("./user.png", "./gemma.png"),
|
89 |
height=450,
|
90 |
show_copy_button=True
|
91 |
)
|
92 |
|
93 |
+
# State: holds entire conversation as list of (user_msg, bot_msg) pairs
|
94 |
state = gr.State([])
|
95 |
|
|
|
96 |
with gr.Accordion("Advanced Settings", open=False):
|
97 |
system_msg = gr.Textbox(
|
98 |
value=ChatConfig.DEFAULT_SYSTEM_MSG,
|
99 |
+
label="System Prompt",
|
100 |
lines=2
|
101 |
)
|
102 |
max_tokens = gr.Slider(
|
|
|
104 |
maximum=8192,
|
105 |
value=ChatConfig.DEFAULT_MAX_TOKENS,
|
106 |
step=1,
|
107 |
+
label="Max Tokens"
|
|
|
108 |
)
|
109 |
temperature = gr.Slider(
|
110 |
minimum=0.1,
|
111 |
maximum=1.0,
|
112 |
value=ChatConfig.DEFAULT_TEMP,
|
113 |
step=0.1,
|
114 |
+
label="Temperature"
|
|
|
115 |
)
|
116 |
top_p = gr.Slider(
|
117 |
minimum=0.1,
|
118 |
maximum=1.0,
|
119 |
value=ChatConfig.DEFAULT_TOP_P,
|
120 |
step=0.05,
|
121 |
+
label="Top-P"
|
|
|
122 |
)
|
123 |
|
124 |
+
# User input box
|
125 |
msg_box = gr.Textbox(
|
126 |
+
label="Type your message here",
|
127 |
+
placeholder="Press enter to send"
|
128 |
)
|
129 |
|
|
|
130 |
def user_submit(user_message, chat_history):
|
|
|
|
|
|
|
|
|
131 |
if not user_message.strip():
|
132 |
return "", chat_history
|
133 |
+
# Add user turn
|
134 |
chat_history = chat_history + [[user_message, None]]
|
135 |
+
# Clear text box
|
136 |
return "", chat_history
|
137 |
|
|
|
138 |
def bot_reply(chat_history, system_text, max_toks, temp, top):
|
139 |
+
user_msg = chat_history[-1][0] # latest user message
|
140 |
+
# stream from generate_response
|
|
|
|
|
|
|
|
|
|
|
141 |
stream_resp = generate_response(
|
142 |
message=user_msg,
|
143 |
+
history=chat_history[:-1],
|
144 |
system_message=system_text,
|
145 |
max_tokens=max_toks,
|
146 |
temperature=temp,
|
147 |
top_p=top
|
148 |
)
|
149 |
+
# each partial chunk
|
150 |
+
for partial in stream_resp:
|
151 |
+
chat_history[-1][1] = partial
|
152 |
+
# **IMPORTANT**: must yield TWO items -> (updated_state, updated_chatbot)
|
153 |
+
yield chat_history, chat_history
|
154 |
+
|
155 |
+
# We have 2 steps chained:
|
156 |
+
# 1) user_submit -> (clear box, update state)
|
157 |
+
# 2) bot_reply -> stream updates
|
158 |
(
|
159 |
msg_box.submit(
|
160 |
fn=user_submit,
|
161 |
inputs=[msg_box, state],
|
162 |
outputs=[msg_box, state],
|
163 |
+
# Must be valid JS function:
|
164 |
+
js="(msg, st) => { new Audio('submit_sound.mp3').play(); return [msg, st]; }"
|
165 |
)
|
166 |
.then(
|
167 |
fn=bot_reply,
|
|
|
174 |
|
175 |
def main():
|
176 |
app = create_interface()
|
177 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
main()
|