Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,11 +43,10 @@ class ChatApp:
|
|
43 |
max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
|
44 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
45 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
46 |
-
) ->
|
47 |
-
"""Generate
|
48 |
if not message.strip():
|
49 |
-
|
50 |
-
return
|
51 |
|
52 |
messages = [{"role": "system", "content": system_message}]
|
53 |
|
@@ -72,10 +71,10 @@ class ChatApp:
|
|
72 |
):
|
73 |
token = chunk.choices[0].delta.content or ""
|
74 |
response += token
|
75 |
-
|
76 |
except Exception as e:
|
77 |
logger.error(f"Error generating response: {e}")
|
78 |
-
|
79 |
|
80 |
def create_interface(self) -> gr.Blocks:
|
81 |
"""Create and configure the chat interface."""
|
@@ -191,37 +190,61 @@ class ChatApp:
|
|
191 |
- **描述**: Gemma 3是Google推出的先进开源大语言模型。
|
192 |
""")
|
193 |
|
194 |
-
#
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
|
|
197 |
submit_click = submit_btn.click(
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
202 |
)
|
203 |
|
204 |
-
# Submit when pressing Enter
|
205 |
msg.submit(
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
210 |
)
|
211 |
|
212 |
-
# Clear the textbox after sending
|
213 |
-
submit_click.then(lambda: "", None, msg)
|
214 |
-
msg.submit(lambda: "", None, msg)
|
215 |
-
|
216 |
# Clear chat button
|
217 |
-
clear_btn.click(lambda:
|
218 |
|
219 |
# Example button
|
220 |
-
example_btn.click(
|
221 |
-
lambda: ("介绍一下人工智能研究中最有趣的发展", []),
|
222 |
-
None,
|
223 |
-
[msg, chatbot]
|
224 |
-
)
|
225 |
|
226 |
return interface
|
227 |
|
|
|
43 |
max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
|
44 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
45 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
46 |
+
) -> str:
|
47 |
+
"""Generate responses from the model."""
|
48 |
if not message.strip():
|
49 |
+
return "请输入消息。"
|
|
|
50 |
|
51 |
messages = [{"role": "system", "content": system_message}]
|
52 |
|
|
|
71 |
):
|
72 |
token = chunk.choices[0].delta.content or ""
|
73 |
response += token
|
74 |
+
return response
|
75 |
except Exception as e:
|
76 |
logger.error(f"Error generating response: {e}")
|
77 |
+
return f"抱歉,发生了错误: {str(e)}"
|
78 |
|
79 |
def create_interface(self) -> gr.Blocks:
|
80 |
"""Create and configure the chat interface."""
|
|
|
190 |
- **描述**: Gemma 3是Google推出的先进开源大语言模型。
|
191 |
""")
|
192 |
|
193 |
+
# Define chatbot functions with correct input/output format
|
194 |
+
def add_text(history, text):
|
195 |
+
history = history + [(text, None)]
|
196 |
+
return history, ""
|
197 |
+
|
198 |
+
def bot_response(history, system_message, max_tokens, temperature, top_p):
|
199 |
+
if history and history[-1][1] is None:
|
200 |
+
user_message = history[-1][0]
|
201 |
+
# Remove the last incomplete message pair
|
202 |
+
history_for_model = history[:-1]
|
203 |
+
# Generate response
|
204 |
+
bot_message = self.generate_response(
|
205 |
+
user_message,
|
206 |
+
history_for_model,
|
207 |
+
system_message,
|
208 |
+
max_tokens,
|
209 |
+
temperature,
|
210 |
+
top_p
|
211 |
+
)
|
212 |
+
# Update the history with the complete message pair
|
213 |
+
history[-1] = (user_message, bot_message)
|
214 |
+
return history
|
215 |
+
|
216 |
+
def load_example():
|
217 |
+
return [("介绍一下人工智能研究中最有趣的发展", None)]
|
218 |
|
219 |
+
# Set up event handlers
|
220 |
submit_click = submit_btn.click(
|
221 |
+
add_text,
|
222 |
+
[chatbot, msg],
|
223 |
+
[chatbot, msg],
|
224 |
+
queue=False
|
225 |
+
).then(
|
226 |
+
bot_response,
|
227 |
+
[chatbot, system_msg, max_tokens, temperature, top_p],
|
228 |
+
chatbot
|
229 |
)
|
230 |
|
231 |
+
# Submit when pressing Enter
|
232 |
msg.submit(
|
233 |
+
add_text,
|
234 |
+
[chatbot, msg],
|
235 |
+
[chatbot, msg],
|
236 |
+
queue=False
|
237 |
+
).then(
|
238 |
+
bot_response,
|
239 |
+
[chatbot, system_msg, max_tokens, temperature, top_p],
|
240 |
+
chatbot
|
241 |
)
|
242 |
|
|
|
|
|
|
|
|
|
243 |
# Clear chat button
|
244 |
+
clear_btn.click(lambda: [], None, chatbot)
|
245 |
|
246 |
# Example button
|
247 |
+
example_btn.click(load_example, None, chatbot)
|
|
|
|
|
|
|
|
|
248 |
|
249 |
return interface
|
250 |
|