Update app.py
Browse files
app.py
CHANGED
@@ -82,37 +82,50 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
82 |
|
83 |
def respond(
|
84 |
message,
|
85 |
-
|
86 |
system_message,
|
87 |
max_tokens,
|
88 |
temperature,
|
89 |
top_p,
|
90 |
):
|
|
|
|
|
|
|
91 |
messages = [{"role": "system", "content": system_message}]
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
97 |
messages.append({"role": "user", "content": message})
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
with gr.Blocks(css=custom_css) as demo:
|
111 |
with gr.Column(elem_id="chat-container"):
|
112 |
# Header
|
113 |
with gr.Row(elem_id="header"):
|
114 |
with gr.Column(elem_id="logo"):
|
115 |
-
gr.Markdown("
|
116 |
with gr.Row(elem_id="nav-links"):
|
117 |
gr.Markdown("[AI Chat](#) [AI Image Generator](#) [AI Video](#) [AI Music Generator](#) [Login](#)")
|
118 |
|
@@ -164,9 +177,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
164 |
# Set up chat functionality
|
165 |
msg.submit(
|
166 |
respond,
|
167 |
-
[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
168 |
-
[msg, chatbot]
|
169 |
)
|
170 |
|
|
|
|
|
|
|
|
|
171 |
if __name__ == "__main__":
|
172 |
demo.launch()
|
|
|
82 |
|
83 |
def respond(
|
84 |
message,
|
85 |
+
chat_history,
|
86 |
system_message,
|
87 |
max_tokens,
|
88 |
temperature,
|
89 |
top_p,
|
90 |
):
|
91 |
+
if message.strip() == "":
|
92 |
+
return "", chat_history
|
93 |
+
|
94 |
messages = [{"role": "system", "content": system_message}]
|
95 |
+
|
96 |
+
# Convert chat history to the format expected by the model
|
97 |
+
for human, assistant in chat_history:
|
98 |
+
messages.append({"role": "user", "content": human})
|
99 |
+
if assistant: # Only add assistant message if it exists
|
100 |
+
messages.append({"role": "assistant", "content": assistant})
|
101 |
+
|
102 |
messages.append({"role": "user", "content": message})
|
103 |
+
|
104 |
+
# Get the response from the model
|
105 |
+
try:
|
106 |
+
response = ""
|
107 |
+
for chunk in client.chat_completion(
|
108 |
+
messages,
|
109 |
+
max_tokens=max_tokens,
|
110 |
+
stream=True,
|
111 |
+
temperature=temperature,
|
112 |
+
top_p=top_p,
|
113 |
+
):
|
114 |
+
if hasattr(chunk.choices[0].delta, 'content'):
|
115 |
+
if chunk.choices[0].delta.content is not None:
|
116 |
+
response += chunk.choices[0].delta.content
|
117 |
+
|
118 |
+
chat_history.append((message, response))
|
119 |
+
return "", chat_history
|
120 |
+
except Exception as e:
|
121 |
+
return "", chat_history + [(message, f"Error: {str(e)}")]
|
122 |
|
123 |
with gr.Blocks(css=custom_css) as demo:
|
124 |
with gr.Column(elem_id="chat-container"):
|
125 |
# Header
|
126 |
with gr.Row(elem_id="header"):
|
127 |
with gr.Column(elem_id="logo"):
|
128 |
+
gr.Markdown("🌎 ZiF-V0")
|
129 |
with gr.Row(elem_id="nav-links"):
|
130 |
gr.Markdown("[AI Chat](#) [AI Image Generator](#) [AI Video](#) [AI Music Generator](#) [Login](#)")
|
131 |
|
|
|
177 |
# Set up chat functionality
|
178 |
msg.submit(
|
179 |
respond,
|
180 |
+
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
181 |
+
outputs=[msg, chatbot]
|
182 |
)
|
183 |
|
184 |
+
# Add a clear button
|
185 |
+
clear = gr.Button("Clear")
|
186 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
187 |
+
|
188 |
if __name__ == "__main__":
|
189 |
demo.launch()
|