tayyab-077 commited on
Commit
d1b906b
Β·
1 Parent(s): a88dbcc

updatation

Browse files
Files changed (3) hide show
  1. app.py +19 -1
  2. src/chatbot.py +5 -5
  3. src/conversation.py +7 -2
app.py CHANGED
@@ -91,18 +91,36 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
91
  with open(txt_path, "w", encoding="utf-8") as f:
92
  for msg in history:
93
  content = msg.get("content", "")
 
 
 
 
 
 
 
 
94
  lines = content.splitlines()
95
 
96
  clean = [l for l in lines if not l.strip().startswith("πŸ•’")]
 
97
  if clean and clean[0].startswith("**"):
98
  clean = clean[1:]
 
99
  clean = [l for l in clean if not set(l) == {"-"}]
100
- clean = [l.replace("USER:", "").replace("ASSISTANT:", "").strip() for l in clean]
 
 
 
 
 
 
101
 
102
  clean = remove_last_closing_line(clean)
 
103
  f.write("\n".join(clean).strip() + "\n")
104
  f.write("-" * 60 + "\n")
105
 
 
106
  pdf_path = None
107
  try:
108
  from reportlab.lib.pagesizes import A4
 
91
  with open(txt_path, "w", encoding="utf-8") as f:
92
  for msg in history:
93
  content = msg.get("content", "")
94
+
95
+ # --- FIX: prevent splitlines() crash ---
96
+ if isinstance(content, list):
97
+ content = "\n".join([str(x) for x in content])
98
+ else:
99
+ content = str(content)
100
+ # ---------------------------------------
101
+
102
  lines = content.splitlines()
103
 
104
  clean = [l for l in lines if not l.strip().startswith("πŸ•’")]
105
+
106
  if clean and clean[0].startswith("**"):
107
  clean = clean[1:]
108
+
109
  clean = [l for l in clean if not set(l) == {"-"}]
110
+
111
+ clean = [
112
+ l.replace("USER:", "")
113
+ .replace("ASSISTANT:", "")
114
+ .strip()
115
+ for l in clean
116
+ ]
117
 
118
  clean = remove_last_closing_line(clean)
119
+
120
  f.write("\n".join(clean).strip() + "\n")
121
  f.write("-" * 60 + "\n")
122
 
123
+
124
  pdf_path = None
125
  try:
126
  from reportlab.lib.pagesizes import A4
src/chatbot.py CHANGED
@@ -28,13 +28,13 @@ class LocalChatbot:
28
  pass
29
 
30
  system_prompt = self._build_system_prompt(intent)
31
- history_text = self.memory.get_formatted(separator=MSG_SEPARATOR) or ""
32
 
33
  prompt = (
34
- f"System: {system_prompt}\n"
35
  f"{history_text}"
36
- f"User: {user_message}\n"
37
- f"Assistant:"
38
  )
39
  return prompt
40
 
@@ -55,7 +55,7 @@ class LocalChatbot:
55
  max_tokens=gen["max_tokens"],
56
  temperature=gen["temperature"],
57
  top_p=gen["top_p"],
58
- stop=["User:", "System:", "Assistant:"]
59
 
60
  )
61
 
 
28
  pass
29
 
30
  system_prompt = self._build_system_prompt(intent)
31
+ history_text = self.memory.get_formatted(separator=MSG_SEPARATOR)
32
 
33
  prompt = (
34
+ f"<start_of_turn>system\n{system_prompt}<end_of_turn>\n"
35
  f"{history_text}"
36
+ f"<start_of_turn>user\n{user_message}<end_of_turn>\n"
37
+ f"<start_of_turn>model\n"
38
  )
39
  return prompt
40
 
 
55
  max_tokens=gen["max_tokens"],
56
  temperature=gen["temperature"],
57
  top_p=gen["top_p"],
58
+ stop=["User:", "\nUser:", "\nSystem:", "\nAssistant:"]
59
 
60
  )
61
 
src/conversation.py CHANGED
@@ -29,10 +29,15 @@ class ConversationMemory:
29
  def get_formatted(self, separator: str = "\n") -> str:
30
  out_lines = []
31
  for msg in self.history:
32
- role_label = "System" if msg["role"] == "system" else ("User" if msg["role"] == "user" else "Assistant")
33
- out_lines.append(f"{role_label}: {msg['content']}")
 
 
 
 
34
  return separator.join(out_lines) + (separator if out_lines else "")
35
 
 
36
  def clear(self):
37
  self.history.clear()
38
 
 
29
  def get_formatted(self, separator: str = "\n") -> str:
30
  out_lines = []
31
  for msg in self.history:
32
+ if msg["role"] == "user":
33
+ out_lines.append(f"User says: {msg['content']}")
34
+ elif msg["role"] == "assistant":
35
+ out_lines.append(f"Assistant says: {msg['content']}")
36
+ else:
37
+ out_lines.append(f"System says: {msg['content']}")
38
  return separator.join(out_lines) + (separator if out_lines else "")
39
 
40
+
41
  def clear(self):
42
  self.history.clear()
43