Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,10 @@ class ChatMessage:
|
|
11 |
role: str
|
12 |
content: str
|
13 |
|
|
|
|
|
|
|
|
|
14 |
class XylariaChat:
|
15 |
def __init__(self):
|
16 |
# Securely load HuggingFace token
|
@@ -125,7 +129,7 @@ class XylariaChat:
|
|
125 |
messages.append(ChatMessage(
|
126 |
role="system",
|
127 |
content=self.system_prompt
|
128 |
-
))
|
129 |
|
130 |
# Add persistent memory context if available
|
131 |
if self.persistent_memory:
|
@@ -135,14 +139,14 @@ class XylariaChat:
|
|
135 |
messages.append(ChatMessage(
|
136 |
role="system",
|
137 |
content=memory_context
|
138 |
-
))
|
139 |
|
140 |
-
# Convert existing conversation history to ChatMessage objects
|
141 |
for msg in self.conversation_history:
|
142 |
messages.append(ChatMessage(
|
143 |
role=msg['role'],
|
144 |
content=msg['content']
|
145 |
-
))
|
146 |
|
147 |
# Process image if uploaded
|
148 |
if image:
|
@@ -153,12 +157,12 @@ class XylariaChat:
|
|
153 |
messages.append(ChatMessage(
|
154 |
role="user",
|
155 |
content=user_input
|
156 |
-
))
|
157 |
|
158 |
# Generate response with streaming
|
159 |
stream = self.client.chat.completions.create(
|
160 |
model="Qwen/QwQ-32B-Preview",
|
161 |
-
messages=messages,
|
162 |
temperature=0.5,
|
163 |
max_tokens=10240,
|
164 |
top_p=0.7,
|
@@ -173,30 +177,35 @@ class XylariaChat:
|
|
173 |
|
174 |
def create_interface(self):
|
175 |
def streaming_response(message, chat_history, image):
|
176 |
-
# Clear input textbox
|
177 |
response_stream = self.get_response(message, image)
|
178 |
-
|
179 |
-
#
|
180 |
if isinstance(response_stream, str):
|
181 |
-
|
|
|
|
|
|
|
182 |
|
183 |
# Prepare for streaming response
|
184 |
full_response = ""
|
185 |
updated_history = chat_history + [[message, ""]]
|
186 |
-
|
187 |
# Streaming output
|
188 |
try:
|
189 |
for chunk in response_stream:
|
190 |
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
191 |
chunk_content = chunk.choices[0].delta.content
|
192 |
full_response += chunk_content
|
193 |
-
|
194 |
# Update the last message in chat history with partial response
|
195 |
updated_history[-1][1] = full_response
|
196 |
-
yield "", updated_history, None
|
197 |
except Exception as e:
|
198 |
print(f"Streaming error: {e}")
|
199 |
-
|
|
|
|
|
|
|
200 |
|
201 |
# Update conversation history
|
202 |
self.conversation_history.append(
|
@@ -206,7 +215,7 @@ class XylariaChat:
|
|
206 |
{"role": "assistant", "content": full_response}
|
207 |
)
|
208 |
|
209 |
-
# Limit conversation history
|
210 |
if len(self.conversation_history) > 10:
|
211 |
self.conversation_history = self.conversation_history[-10:]
|
212 |
|
@@ -236,7 +245,7 @@ class XylariaChat:
|
|
236 |
label="Xylaria 1.4 Senoa (Qwen Model)",
|
237 |
height=500,
|
238 |
show_copy_button=True,
|
239 |
-
|
240 |
)
|
241 |
|
242 |
# Input row with improved layout and image upload
|
|
|
11 |
role: str
|
12 |
content: str
|
13 |
|
14 |
+
def to_dict(self):
|
15 |
+
"""Converts ChatMessage to a dictionary for JSON serialization."""
|
16 |
+
return {"role": self.role, "content": self.content}
|
17 |
+
|
18 |
class XylariaChat:
|
19 |
def __init__(self):
|
20 |
# Securely load HuggingFace token
|
|
|
129 |
messages.append(ChatMessage(
|
130 |
role="system",
|
131 |
content=self.system_prompt
|
132 |
+
).to_dict()) # Convert to dictionary
|
133 |
|
134 |
# Add persistent memory context if available
|
135 |
if self.persistent_memory:
|
|
|
139 |
messages.append(ChatMessage(
|
140 |
role="system",
|
141 |
content=memory_context
|
142 |
+
).to_dict()) # Convert to dictionary
|
143 |
|
144 |
+
# Convert existing conversation history to ChatMessage objects and then to dictionaries
|
145 |
for msg in self.conversation_history:
|
146 |
messages.append(ChatMessage(
|
147 |
role=msg['role'],
|
148 |
content=msg['content']
|
149 |
+
).to_dict()) # Convert to dictionary
|
150 |
|
151 |
# Process image if uploaded
|
152 |
if image:
|
|
|
157 |
messages.append(ChatMessage(
|
158 |
role="user",
|
159 |
content=user_input
|
160 |
+
).to_dict()) # Convert to dictionary
|
161 |
|
162 |
# Generate response with streaming
|
163 |
stream = self.client.chat.completions.create(
|
164 |
model="Qwen/QwQ-32B-Preview",
|
165 |
+
messages=messages, # Send dictionaries
|
166 |
temperature=0.5,
|
167 |
max_tokens=10240,
|
168 |
top_p=0.7,
|
|
|
177 |
|
178 |
def create_interface(self):
|
179 |
def streaming_response(message, chat_history, image):
|
|
|
180 |
response_stream = self.get_response(message, image)
|
181 |
+
|
182 |
+
# Handle errors in get_response
|
183 |
if isinstance(response_stream, str):
|
184 |
+
# Return immediately with the error message
|
185 |
+
updated_history = chat_history + [[message, response_stream]]
|
186 |
+
yield "", updated_history, None
|
187 |
+
return
|
188 |
|
189 |
# Prepare for streaming response
|
190 |
full_response = ""
|
191 |
updated_history = chat_history + [[message, ""]]
|
192 |
+
|
193 |
# Streaming output
|
194 |
try:
|
195 |
for chunk in response_stream:
|
196 |
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
197 |
chunk_content = chunk.choices[0].delta.content
|
198 |
full_response += chunk_content
|
199 |
+
|
200 |
# Update the last message in chat history with partial response
|
201 |
updated_history[-1][1] = full_response
|
202 |
+
yield "", updated_history, None
|
203 |
except Exception as e:
|
204 |
print(f"Streaming error: {e}")
|
205 |
+
# Display error in the chat interface
|
206 |
+
updated_history[-1][1] = f"Error during response: {e}"
|
207 |
+
yield "", updated_history, None
|
208 |
+
return
|
209 |
|
210 |
# Update conversation history
|
211 |
self.conversation_history.append(
|
|
|
215 |
{"role": "assistant", "content": full_response}
|
216 |
)
|
217 |
|
218 |
+
# Limit conversation history
|
219 |
if len(self.conversation_history) > 10:
|
220 |
self.conversation_history = self.conversation_history[-10:]
|
221 |
|
|
|
245 |
label="Xylaria 1.4 Senoa (Qwen Model)",
|
246 |
height=500,
|
247 |
show_copy_button=True,
|
248 |
+
|
249 |
)
|
250 |
|
251 |
# Input row with improved layout and image upload
|