Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,8 @@ def generate_response(message, history, enable_history=False, max_new_tokens=102
|
|
| 35 |
# Add history only if enabled
|
| 36 |
if enable_history and history:
|
| 37 |
prev = "\n".join(
|
| 38 |
-
[f"User: {h[
|
|
|
|
| 39 |
)
|
| 40 |
context = f"{prev}\n\n{message}"
|
| 41 |
else:
|
|
@@ -68,22 +69,31 @@ def generate_response(message, history, enable_history=False, max_new_tokens=102
|
|
| 68 |
thread.start()
|
| 69 |
|
| 70 |
# Stream the response
|
| 71 |
-
|
| 72 |
-
response_started = False
|
| 73 |
|
| 74 |
for new_text in streamer:
|
| 75 |
-
|
| 76 |
|
| 77 |
-
# Check if we've reached the response section
|
| 78 |
-
if "### ප්රතිචාරය:" in
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
yield
|
| 84 |
|
| 85 |
# Make sure thread completes
|
| 86 |
thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Custom CSS for styling with copy button
|
| 89 |
custom_css = """
|
|
|
|
| 35 |
# Add history only if enabled
|
| 36 |
if enable_history and history:
|
| 37 |
prev = "\n".join(
|
| 38 |
+
[f"User: {h['content']}\nGamunu: {h.get('content', '')}"
|
| 39 |
+
for h in history if h.get('role') == 'assistant']
|
| 40 |
)
|
| 41 |
context = f"{prev}\n\n{message}"
|
| 42 |
else:
|
|
|
|
| 69 |
thread.start()
|
| 70 |
|
| 71 |
# Stream the response
|
| 72 |
+
full_text = ""
|
|
|
|
| 73 |
|
| 74 |
for new_text in streamer:
|
| 75 |
+
full_text += new_text
|
| 76 |
|
| 77 |
+
# Check if we've reached the response section and extract it
|
| 78 |
+
if "### ප්රතිචාරය:" in full_text:
|
| 79 |
+
response_text = full_text.split("### ප්රතිචාරය:")[-1].strip()
|
| 80 |
+
yield response_text
|
| 81 |
+
else:
|
| 82 |
+
# Still building up to the response marker, yield what we have
|
| 83 |
+
yield full_text.strip()
|
| 84 |
|
| 85 |
# Make sure thread completes
|
| 86 |
thread.join()
|
| 87 |
+
|
| 88 |
+
# Final yield with cleaned response
|
| 89 |
+
if "### ප්රතිචාරය:" in full_text:
|
| 90 |
+
final_response = full_text.split("### ප්රතිචාරය:")[-1].strip()
|
| 91 |
+
else:
|
| 92 |
+
final_response = full_text.strip()
|
| 93 |
+
|
| 94 |
+
# Ensure we yield at least once
|
| 95 |
+
if final_response:
|
| 96 |
+
yield final_response
|
| 97 |
|
| 98 |
# Custom CSS for styling with copy button
|
| 99 |
custom_css = """
|