Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,102 @@
|
|
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("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
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 |
response += token
|
40 |
-
yield response
|
41 |
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly
|
50 |
-
gr.Slider(
|
51 |
-
gr.Slider(
|
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 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
|
|
|
|
|
|
|
6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
|
8 |
+
# Base64 encode image
|
9 |
+
def image_to_base64(image_path):
|
10 |
+
try:
|
11 |
+
with open(image_path, "rb") as image_file:
|
12 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
13 |
+
except:
|
14 |
+
return ""
|
15 |
|
16 |
+
# Header HTML
|
17 |
+
def get_header_html():
|
18 |
+
if os.path.exists("static/aivn_logo.png"):
|
19 |
+
logo_base64 = image_to_base64("static/aivn_logo.png")
|
20 |
+
logo_html = f"""
|
21 |
+
<img src="data:image/png;base64,{logo_base64}"
|
22 |
+
alt="Logo"
|
23 |
+
style="height: 120px; width: auto; margin-right: 20px; margin-bottom: 20px;">
|
24 |
+
"""
|
25 |
+
else:
|
26 |
+
logo_html = "<h2>AI VIETNAM</h2>"
|
27 |
+
|
28 |
+
return f"""
|
29 |
+
<div style="display: flex; align-items: center; padding: 20px;">
|
30 |
+
<div>{logo_html}</div>
|
31 |
+
<div style="margin-left: 20px;">
|
32 |
+
<h1>🧠 Zephyr Chatbot</h1>
|
33 |
+
<p>Conversational Assistant powered by HuggingFace Zephyr-7B</p>
|
34 |
+
<p style="color: #555;">🚀 Customize system prompt, temperature, top-p, etc.</p>
|
35 |
+
</div>
|
36 |
+
</div>
|
37 |
+
"""
|
38 |
|
39 |
+
# Footer HTML
|
40 |
+
def get_footer_html():
|
41 |
+
return """
|
42 |
+
<div style="
|
43 |
+
position: fixed;
|
44 |
+
bottom: 0;
|
45 |
+
left: 0;
|
46 |
+
width: 100%;
|
47 |
+
background: white;
|
48 |
+
padding: 10px;
|
49 |
+
text-align: center;
|
50 |
+
box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
|
51 |
+
z-index: 1000;
|
52 |
+
font-size: 14px;
|
53 |
+
">
|
54 |
+
Created by <a href="https://vlai.work" target="_blank" style="color: #007BFF;">VLAI</a> • AI VIETNAM
|
55 |
+
</div>
|
56 |
+
"""
|
57 |
+
|
58 |
+
# Chat function
|
59 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
60 |
+
messages = [{"role": "system", "content": system_message}]
|
61 |
+
for msg in history:
|
62 |
+
messages.append(msg)
|
63 |
|
64 |
messages.append({"role": "user", "content": message})
|
65 |
|
66 |
response = ""
|
67 |
+
for chunk in client.chat_completion(
|
|
|
68 |
messages,
|
69 |
max_tokens=max_tokens,
|
70 |
stream=True,
|
71 |
temperature=temperature,
|
72 |
top_p=top_p,
|
73 |
):
|
74 |
+
token = chunk.choices[0].delta.content
|
|
|
75 |
response += token
|
76 |
+
yield {"role": "assistant", "content": response}
|
77 |
|
78 |
|
79 |
+
# UI
|
80 |
+
chat = gr.ChatInterface(
|
81 |
+
fn=respond,
|
|
|
|
|
82 |
additional_inputs=[
|
83 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System message"),
|
84 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
85 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
86 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
],
|
88 |
+
title="Zephyr Chatbot",
|
89 |
+
description="A customizable chat assistant powered by HuggingFace H4 Zephyr-7B.",
|
90 |
+
type="messages",
|
91 |
)
|
92 |
|
93 |
|
94 |
+
# Combine with layout elements
|
95 |
+
with gr.Blocks(css=".gradio-container {min-height: 100vh;}") as demo:
|
96 |
+
gr.HTML(get_header_html())
|
97 |
+
chat.render()
|
98 |
+
gr.HTML(get_footer_html())
|
99 |
+
|
100 |
if __name__ == "__main__":
|
101 |
+
os.makedirs("static", exist_ok=True)
|
102 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|