Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ MAX_GPU_MEMORY = "60GiB"
|
|
14 |
# --- Model Loading ---
|
15 |
@GPU(memory=60)
|
16 |
def load_model():
|
17 |
-
"""Load the
|
18 |
print(f"Attempting to load model: {MODEL_ID} without quantization")
|
19 |
try:
|
20 |
pipe = pipeline(
|
@@ -142,7 +142,7 @@ def parse_llm_output(full_output, input_prompt_list):
|
|
142 |
cleaned_text = re.sub(r'^<\/?s?>', '', cleaned_text).strip()
|
143 |
cleaned_text = re.sub(r'^(assistant|ASSISTANT|System|SYSTEM)[:\s]*', '', cleaned_text).strip()
|
144 |
if not cleaned_text and generated_text:
|
145 |
-
print("
|
146 |
return generated_text
|
147 |
if last_input_content and last_occurrence_index == -1:
|
148 |
print("Warning: Could not find last input prompt in LLM output. Returning cleaned full output.")
|
@@ -266,7 +266,8 @@ class PersonaChat:
|
|
266 |
self.messages = []
|
267 |
self.enhanced_profile = ""
|
268 |
status = f"Searching for information about {name}..."
|
269 |
-
|
|
|
270 |
search_results = search_person(name, context)
|
271 |
if isinstance(search_results, str) and search_results.startswith("Error"):
|
272 |
error_msg = f"Failed to set persona: {search_results}"
|
@@ -276,14 +277,14 @@ class PersonaChat:
|
|
276 |
if bio_text.startswith("Could not extract text"):
|
277 |
yield f"Warning: {bio_text}", "", "", [{"role": "system", "content": bio_text}]
|
278 |
status = f"Creating enhanced profile for {name}..."
|
279 |
-
yield status, "", bio_text, [
|
280 |
self.enhanced_profile = generate_enhanced_persona(name, bio_text, context)
|
281 |
profile_for_prompt = self.enhanced_profile
|
282 |
if self.enhanced_profile.startswith("Error enhancing profile"):
|
283 |
yield f"Warning: Could not enhance profile. Using basic info.", "", self.enhanced_profile, [{"role": "system", "content": self.enhanced_profile}]
|
284 |
profile_for_prompt = bio_text
|
285 |
status = f"Generating optimal system prompt for {name}..."
|
286 |
-
yield status, self.enhanced_profile, self.enhanced_profile, [
|
287 |
self.system_prompt = generate_system_prompt_with_llm(name, profile_for_prompt, context)
|
288 |
self.messages = [{"role": "system", "content": self.system_prompt}]
|
289 |
yield f"Persona set to '{name}'. Ready to chat!", self.system_prompt, self.enhanced_profile, self.messages
|
@@ -326,7 +327,7 @@ def create_interface():
|
|
326 |
.message-input { margin-top: 10px; }
|
327 |
.send-button { background-color: #2c3e50 !important; color: white !important; }
|
328 |
.persona-button { background-color: #4ca1af !important; color: white !important; }
|
329 |
-
.system-prompt-display { background-color: #f5f5f5; border-radius: 8px; padding: 15px; margin-top: 15px; border: 1px solid
|
330 |
.footer { text-align: center; margin-top: 20px; font-size: 0.9em; color: #666; }
|
331 |
.typing-indicator { color: #aaa; font-style: italic; }
|
332 |
"""
|
@@ -372,13 +373,13 @@ def create_interface():
|
|
372 |
initial_profile = "Enhanced profile will appear here..."
|
373 |
initial_history = []
|
374 |
yield initial_status, initial_prompt, initial_profile, initial_character_display, initial_history
|
375 |
-
final_status, final_prompt, final_profile = "Error", "", ""
|
376 |
-
final_history = initial_history
|
377 |
try:
|
378 |
for status_update, prompt_update, profile_update, history_update in persona_chat.set_persona(name, context):
|
379 |
-
final_status
|
380 |
-
|
381 |
-
|
|
|
382 |
character_display = f"### Preparing chat with {name}..."
|
383 |
if "Ready to chat" in status_update:
|
384 |
character_display = f"### Chatting with {name}"
|
|
|
14 |
# --- Model Loading ---
|
15 |
@GPU(memory=60)
|
16 |
def load_model():
|
17 |
+
"""Load the Q-year-old model without quantization for full precision."""
|
18 |
print(f"Attempting to load model: {MODEL_ID} without quantization")
|
19 |
try:
|
20 |
pipe = pipeline(
|
|
|
142 |
cleaned_text = re.sub(r'^<\/?s?>', '', cleaned_text).strip()
|
143 |
cleaned_text = re.sub(r'^(assistant|ASSISTANT|System|SYSTEM)[:\s]*', '', cleaned_text).strip()
|
144 |
if not cleaned_text and generated_text:
|
145 |
+
print("Warning: Parsing resulted in empty string, returning original generation.")
|
146 |
return generated_text
|
147 |
if last_input_content and last_occurrence_index == -1:
|
148 |
print("Warning: Could not find last input prompt in LLM output. Returning cleaned full output.")
|
|
|
266 |
self.messages = []
|
267 |
self.enhanced_profile = ""
|
268 |
status = f"Searching for information about {name}..."
|
269 |
+
# Yield empty history for intermediate steps to avoid invalid messages
|
270 |
+
yield status, "", "", []
|
271 |
search_results = search_person(name, context)
|
272 |
if isinstance(search_results, str) and search_results.startswith("Error"):
|
273 |
error_msg = f"Failed to set persona: {search_results}"
|
|
|
277 |
if bio_text.startswith("Could not extract text"):
|
278 |
yield f"Warning: {bio_text}", "", "", [{"role": "system", "content": bio_text}]
|
279 |
status = f"Creating enhanced profile for {name}..."
|
280 |
+
yield status, "", bio_text, []
|
281 |
self.enhanced_profile = generate_enhanced_persona(name, bio_text, context)
|
282 |
profile_for_prompt = self.enhanced_profile
|
283 |
if self.enhanced_profile.startswith("Error enhancing profile"):
|
284 |
yield f"Warning: Could not enhance profile. Using basic info.", "", self.enhanced_profile, [{"role": "system", "content": self.enhanced_profile}]
|
285 |
profile_for_prompt = bio_text
|
286 |
status = f"Generating optimal system prompt for {name}..."
|
287 |
+
yield status, self.enhanced_profile, self.enhanced_profile, []
|
288 |
self.system_prompt = generate_system_prompt_with_llm(name, profile_for_prompt, context)
|
289 |
self.messages = [{"role": "system", "content": self.system_prompt}]
|
290 |
yield f"Persona set to '{name}'. Ready to chat!", self.system_prompt, self.enhanced_profile, self.messages
|
|
|
327 |
.message-input { margin-top: 10px; }
|
328 |
.send-button { background-color: #2c3e50 !important; color: white !important; }
|
329 |
.persona-button { background-color: #4ca1af !important; color: white !important; }
|
330 |
+
.system-prompt-display { background-color: #f5f5f5; border-radius: 8px; padding: 15px; margin-top: 15px; border: 1px solid F#e0e0e0; font-family: monospace; white-space: pre-wrap; word-wrap: break-word; }
|
331 |
.footer { text-align: center; margin-top: 20px; font-size: 0.9em; color: #666; }
|
332 |
.typing-indicator { color: #aaa; font-style: italic; }
|
333 |
"""
|
|
|
373 |
initial_profile = "Enhanced profile will appear here..."
|
374 |
initial_history = []
|
375 |
yield initial_status, initial_prompt, initial_profile, initial_character_display, initial_history
|
376 |
+
final_status, final_prompt, final_profile, final_character_display, final_history = "Error", "", "", f"### Error creating {name}", []
|
|
|
377 |
try:
|
378 |
for status_update, prompt_update, profile_update, history_update in persona_chat.set_persona(name, context):
|
379 |
+
final_status = status_update
|
380 |
+
final_prompt = prompt_update
|
381 |
+
final_profile = profile_update
|
382 |
+
final_history = history_update if history_update else []
|
383 |
character_display = f"### Preparing chat with {name}..."
|
384 |
if "Ready to chat" in status_update:
|
385 |
character_display = f"### Chatting with {name}"
|