manthilaffs commited on
Commit
e8fd1ed
·
verified ·
1 Parent(s): 9ae1d9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
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[0]}\nGamunu: {h[1]}" for h in history if h[1] is not None]
 
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
- partial_text = ""
72
- response_started = False
73
 
74
  for new_text in streamer:
75
- partial_text += new_text
76
 
77
- # Check if we've reached the response section
78
- if "### ප්‍රතිචාරය:" in partial_text and not response_started:
79
- partial_text = partial_text.split("### ප්‍රතිචාරය:")[-1].strip()
80
- response_started = True
81
-
82
- if response_started:
83
- yield partial_text
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 = """