Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,172 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
"""
|
5 |
-
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
|
6 |
-
"""
|
7 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
8 |
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
-
token =
|
|
|
|
|
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 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=200000, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
|
|
|
|
4 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
5 |
|
6 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
messages = [{"role": "system", "content": system_message}]
|
8 |
+
|
9 |
+
for user_msg, assistant_msg in history:
|
10 |
+
messages.extend([
|
11 |
+
{"role": "user", "content": user_msg},
|
12 |
+
{"role": "assistant", "content": assistant_msg}
|
13 |
+
])
|
14 |
+
|
15 |
messages.append({"role": "user", "content": message})
|
16 |
|
17 |
+
full_response = ""
|
18 |
+
for chunk in client.chat_completion(
|
|
|
19 |
messages,
|
20 |
max_tokens=max_tokens,
|
21 |
stream=True,
|
22 |
temperature=temperature,
|
23 |
top_p=top_p,
|
24 |
):
|
25 |
+
token = chunk.choices[0].delta.content or ""
|
26 |
+
full_response += token
|
27 |
+
yield full_response
|
28 |
|
29 |
+
custom_css = """
|
30 |
+
:root {
|
31 |
+
--primary: #2563eb;
|
32 |
+
--primary-dark: #1d4ed8;
|
33 |
+
--dark: #1e293b;
|
34 |
+
--light: #f8fafc;
|
35 |
+
--gray: #94a3b8;
|
36 |
+
--success: #10b981;
|
37 |
+
}
|
38 |
|
39 |
+
body {
|
40 |
+
font-family: 'Inter', sans-serif;
|
41 |
+
background-color: #f1f5f9 !important;
|
42 |
+
}
|
43 |
|
44 |
+
.message-user {
|
45 |
+
background: white !important;
|
46 |
+
border-left: 4px solid var(--primary) !important;
|
47 |
+
}
|
48 |
+
|
49 |
+
.message-assistant {
|
50 |
+
background: white !important;
|
51 |
+
border-left: 4px solid var(--success) !important;
|
52 |
+
}
|
53 |
+
|
54 |
+
.code-block {
|
55 |
+
background-color: #0f172a !important;
|
56 |
+
color: #e2e8f0 !important;
|
57 |
+
font-family: 'Fira Code', monospace !important;
|
58 |
+
padding: 1rem !important;
|
59 |
+
border-radius: 0.5rem !important;
|
60 |
+
margin: 0.5rem 0 !important;
|
61 |
+
}
|
62 |
+
|
63 |
+
.settings-panel {
|
64 |
+
background: #f8fafc !important;
|
65 |
+
padding: 1rem !important;
|
66 |
+
border-radius: 0.5rem !important;
|
67 |
+
margin-bottom: 1rem !important;
|
68 |
+
}
|
69 |
+
|
70 |
+
.avatar {
|
71 |
+
width: 32px !important;
|
72 |
+
height: 32px !important;
|
73 |
+
border-radius: 50% !important;
|
74 |
+
display: flex !important;
|
75 |
+
align-items: center !important;
|
76 |
+
justify-content: center !important;
|
77 |
+
}
|
78 |
+
|
79 |
+
.avatar-user { background: var(--primary) !important; }
|
80 |
+
.avatar-bot { background: var(--success) !important; }
|
81 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
with gr.Blocks(css=custom_css, title="Qwen2.5 Coder - AI Assistant") as demo:
|
84 |
+
# Header
|
85 |
+
gr.Markdown("""
|
86 |
+
<div class="flex items-center justify-between p-4 border-b">
|
87 |
+
<h1 class="text-xl font-semibold">Chat with Qwen2.5 Coder</h1>
|
88 |
+
<div class="flex gap-2">
|
89 |
+
<button class="p-2 rounded-full hover:bg-gray-100">
|
90 |
+
<i class="fas fa-cog text-gray-500"></i>
|
91 |
+
</button>
|
92 |
+
<button class="p-2 rounded-full hover:bg-gray-100">
|
93 |
+
<i class="fas fa-question-circle text-gray-500"></i>
|
94 |
+
</button>
|
95 |
+
</div>
|
96 |
+
</div>
|
97 |
+
""")
|
98 |
+
|
99 |
+
# Main Content
|
100 |
+
with gr.Row():
|
101 |
+
# Sidebar
|
102 |
+
with gr.Column(scale=1, min_width=256):
|
103 |
+
with gr.Blocks():
|
104 |
+
gr.Markdown("""
|
105 |
+
<div class="p-4 border-b flex items-center gap-3">
|
106 |
+
<div class="avatar avatar-bot text-white">Q</div>
|
107 |
+
<div>
|
108 |
+
<h2 class="font-semibold">Qwen2.5 Coder</h2>
|
109 |
+
<p class="text-xs text-gray-500">32B Instruct</p>
|
110 |
+
</div>
|
111 |
+
</div>
|
112 |
+
""")
|
113 |
+
|
114 |
+
gr.Button("New Chat", variant="primary", size="sm", icon="fa-plus")
|
115 |
+
gr.Markdown("""<div class="p-2 text-sm font-medium text-gray-500">Recent Chats</div>""")
|
116 |
+
gr.Chatbot(label="Chat History", elem_classes="overflow-y-auto h-[calc(100vh-240px)]")
|
117 |
+
|
118 |
+
# Main Chat Area
|
119 |
+
with gr.Column(scale=3):
|
120 |
+
chatbot = gr.Chatbot(
|
121 |
+
elem_classes="h-[calc(100vh-240px)] overflow-y-auto",
|
122 |
+
bubble_full_width=False,
|
123 |
+
avatar_images=(
|
124 |
+
None,
|
125 |
+
"https://i.ibb.co/4sYt3yd/robot.png" # Replace with your bot avatar
|
126 |
+
)
|
127 |
+
)
|
128 |
+
|
129 |
+
# Settings Panel
|
130 |
+
with gr.Accordion("Settings", open=False, elem_classes="settings-panel"):
|
131 |
+
system_msg = gr.Textbox(
|
132 |
+
"You are a helpful AI assistant that specializes in coding and technical questions.",
|
133 |
+
label="System Message"
|
134 |
+
)
|
135 |
+
max_tokens = gr.Slider(1, 200000, 512, label="Max Tokens")
|
136 |
+
temperature = gr.Slider(0.1, 4.0, 0.7, label="Temperature")
|
137 |
+
top_p = gr.Slider(0.1, 1.0, 0.95, label="Top-p")
|
138 |
+
|
139 |
+
# Input Area
|
140 |
+
with gr.Row():
|
141 |
+
msg = gr.Textbox(
|
142 |
+
placeholder="Type your message here...",
|
143 |
+
lines=2,
|
144 |
+
max_lines=8,
|
145 |
+
autofocus=True,
|
146 |
+
container=False,
|
147 |
+
)
|
148 |
+
submit_btn = gr.Button("Send", variant="primary", size="sm", icon="fa-paper-plane")
|
149 |
+
|
150 |
+
gr.Markdown("""
|
151 |
+
<p class="text-xs text-gray-500 mt-2">
|
152 |
+
Qwen2.5 Coder may produce inaccurate information about people, places, or facts.
|
153 |
+
</p>
|
154 |
+
""")
|
155 |
+
|
156 |
+
# Event Handlers
|
157 |
+
msg.submit(
|
158 |
+
respond,
|
159 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
160 |
+
[chatbot, msg],
|
161 |
+
api_name="respond"
|
162 |
+
).then(lambda: None, None, msg, queue=False)
|
163 |
+
|
164 |
+
submit_btn.click(
|
165 |
+
respond,
|
166 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
167 |
+
[chatbot, msg],
|
168 |
+
api_name="respond"
|
169 |
+
)
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
+
demo.launch()
|