Spaces:
Runtime error
Runtime error
OminduAnjana
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,43 @@
|
|
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 |
def respond(
|
11 |
message,
|
@@ -14,51 +46,109 @@ def respond(
|
|
14 |
max_tokens,
|
15 |
temperature,
|
16 |
top_p,
|
|
|
17 |
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
|
|
|
|
|
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
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=2048, 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
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import time
|
4 |
|
5 |
+
# Initialize the client
|
|
|
|
|
6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
|
8 |
+
# Custom CSS for better styling
|
9 |
+
custom_css = """
|
10 |
+
.container {
|
11 |
+
max-width: 800px;
|
12 |
+
margin: auto;
|
13 |
+
padding: 20px;
|
14 |
+
}
|
15 |
+
|
16 |
+
.chat-message {
|
17 |
+
padding: 15px;
|
18 |
+
border-radius: 10px;
|
19 |
+
margin-bottom: 10px;
|
20 |
+
}
|
21 |
+
|
22 |
+
.user-message {
|
23 |
+
background-color: #e3f2fd;
|
24 |
+
}
|
25 |
+
|
26 |
+
.bot-message {
|
27 |
+
background-color: #f5f5f5;
|
28 |
+
}
|
29 |
+
|
30 |
+
.controls-container {
|
31 |
+
background-color: #ffffff;
|
32 |
+
padding: 20px;
|
33 |
+
border-radius: 10px;
|
34 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
35 |
+
margin-top: 20px;
|
36 |
+
}
|
37 |
+
"""
|
38 |
+
|
39 |
+
def format_timestamp():
|
40 |
+
return time.strftime("%H:%M:%S")
|
41 |
|
42 |
def respond(
|
43 |
message,
|
|
|
46 |
max_tokens,
|
47 |
temperature,
|
48 |
top_p,
|
49 |
+
model_name,
|
50 |
):
|
51 |
messages = [{"role": "system", "content": system_message}]
|
52 |
+
|
53 |
+
# Add timestamp to messages
|
54 |
+
timestamp = format_timestamp()
|
55 |
+
|
56 |
for val in history:
|
57 |
if val[0]:
|
58 |
messages.append({"role": "user", "content": val[0]})
|
59 |
if val[1]:
|
60 |
messages.append({"role": "assistant", "content": val[1]})
|
61 |
+
|
62 |
messages.append({"role": "user", "content": message})
|
63 |
+
|
64 |
response = ""
|
65 |
+
try:
|
66 |
+
for message in client.chat_completion(
|
67 |
+
messages,
|
68 |
+
max_tokens=max_tokens,
|
69 |
+
stream=True,
|
70 |
+
temperature=temperature,
|
71 |
+
top_p=top_p,
|
72 |
+
):
|
73 |
+
token = message.choices[0].delta.content
|
74 |
+
response += token
|
75 |
+
yield response
|
76 |
+
except Exception as e:
|
77 |
+
yield f"Error: {str(e)}"
|
78 |
|
79 |
+
def create_demo():
|
80 |
+
# Theme configuration
|
81 |
+
theme = gr.themes.Default().set(
|
82 |
+
font=["Inter", "ui-sans-serif", "system-ui"],
|
83 |
+
radius_size=gr.themes.sizes.radius_sm,
|
84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
87 |
+
with gr.Row():
|
88 |
+
with gr.Column(scale=3):
|
89 |
+
gr.Markdown("# 🤖 Advanced Chat Interface")
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
with gr.Column(scale=3):
|
93 |
+
chatbot = gr.ChatInterface(
|
94 |
+
respond,
|
95 |
+
additional_inputs=[
|
96 |
+
gr.Textbox(
|
97 |
+
value="You are a friendly and helpful AI assistant.",
|
98 |
+
label="System Message",
|
99 |
+
lines=2
|
100 |
+
),
|
101 |
+
gr.Slider(
|
102 |
+
minimum=1,
|
103 |
+
maximum=2048,
|
104 |
+
value=512,
|
105 |
+
step=1,
|
106 |
+
label="Max Tokens",
|
107 |
+
info="Maximum number of tokens to generate"
|
108 |
+
),
|
109 |
+
gr.Slider(
|
110 |
+
minimum=0.1,
|
111 |
+
maximum=4.0,
|
112 |
+
value=0.7,
|
113 |
+
step=0.1,
|
114 |
+
label="Temperature",
|
115 |
+
info="Higher values make output more random"
|
116 |
+
),
|
117 |
+
gr.Slider(
|
118 |
+
minimum=0.1,
|
119 |
+
maximum=1.0,
|
120 |
+
value=0.95,
|
121 |
+
step=0.05,
|
122 |
+
label="Top-p (Nucleus Sampling)",
|
123 |
+
info="Controls diversity of generated text"
|
124 |
+
),
|
125 |
+
gr.Dropdown(
|
126 |
+
choices=["HuggingFaceH4/zephyr-7b-beta", "other-model-1", "other-model-2"],
|
127 |
+
value="HuggingFaceH4/zephyr-7b-beta",
|
128 |
+
label="Model",
|
129 |
+
info="Select the model to use"
|
130 |
+
),
|
131 |
+
],
|
132 |
+
title="Advanced Chat Interface",
|
133 |
+
description="Chat with an AI assistant powered by Hugging Face models.",
|
134 |
+
)
|
135 |
+
|
136 |
+
with gr.Column(scale=1):
|
137 |
+
with gr.Accordion("Chat Information", open=True):
|
138 |
+
total_messages = gr.Number(value=0, label="Total Messages", interactive=False)
|
139 |
+
chat_duration = gr.Number(value=0, label="Chat Duration (min)", interactive=False)
|
140 |
+
|
141 |
+
with gr.Accordion("Advanced Settings", open=False):
|
142 |
+
gr.Checkbox(label="Enable Debug Mode", value=False)
|
143 |
+
gr.Checkbox(label="Save Chat History", value=True)
|
144 |
+
gr.Radio(
|
145 |
+
choices=["Simple", "Detailed"],
|
146 |
+
value="Simple",
|
147 |
+
label="Response Mode"
|
148 |
+
)
|
149 |
|
150 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
+
demo = create_demo()
|
154 |
+
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|