tayyab-077 commited on
Commit
22487cb
Β·
1 Parent(s): a36268e

updatation

Browse files
Files changed (1) hide show
  1. app.py +14 -28
app.py CHANGED
@@ -34,45 +34,32 @@ def now_ts():
34
  # ----------------------
35
  # Export TXT/PDF
36
  # ----------------------
 
 
 
37
  def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]:
38
  tmpdir = tempfile.gettempdir()
39
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
40
  txt_path = os.path.join(tmpdir, f"chat_history_{timestamp}.txt")
41
  pdf_path = os.path.join(tmpdir, f"chat_history_{timestamp}.pdf")
42
 
43
- def remove_last_closing_line(lines):
44
- closing_keywords = [
45
- "let me know", "is there anything else", "anything else i can help",
46
- "feel free to ask", "hope this helps", "need further assistance",
47
- "feel free", "happy to help", "hello! how can i assist you today?",
48
- "how can i help you better?", "would you like to explore", "need clarification"
49
- ]
50
- if not lines:
51
- return lines
52
- last = lines[-1].lower().strip()
53
- if any(k in last for k in closing_keywords):
54
- return lines[:-1]
55
- return lines
56
-
57
- # TXT
58
  with open(txt_path, "w", encoding="utf-8") as f:
59
  for msg in history:
 
60
  content = msg.get("content", "")
61
  if isinstance(content, dict):
62
  content = content.get("text", "")
63
- lines = [l.strip() for l in str(content).splitlines() if l.strip() and not l.strip().startswith("πŸ•’")]
64
- lines = remove_last_closing_line(lines)
65
- f.write("\n".join(lines) + "\n\n")
66
- f.write("-" * 60 + "\n\n")
67
 
68
- # PDF
69
  try:
70
  c = canvas.Canvas(pdf_path, pagesize=A4)
71
  page_width, page_height = A4
72
  margin = 40
73
  y = page_height - margin
74
- line_height = 14
75
- font_size = 10
76
  c.setFont("Helvetica", font_size)
77
 
78
  for msg in history:
@@ -81,19 +68,18 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
81
  if isinstance(content, dict):
82
  content = content.get("text", "")
83
 
84
- lines = [l.strip() for l in str(content).splitlines() if l.strip() and not l.strip().startswith("πŸ•’")]
85
- lines = remove_last_closing_line(lines)
86
-
87
  for line in lines:
88
- wrapped = textwrap.wrap(line, width=95)
89
  for wline in wrapped:
90
  if y < margin + line_height:
91
  c.showPage()
92
  c.setFont("Helvetica", font_size)
93
  y = page_height - margin
94
- c.drawString(margin, y, f"{role}: {wline}")
95
  y -= line_height
96
- y -= line_height
97
 
98
  c.showPage()
99
  c.save()
 
34
  # ----------------------
35
  # Export TXT/PDF
36
  # ----------------------
37
+ # ----------------------
38
+ # Clean PDF Export
39
+ # ----------------------
40
  def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]:
41
  tmpdir = tempfile.gettempdir()
42
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
43
  txt_path = os.path.join(tmpdir, f"chat_history_{timestamp}.txt")
44
  pdf_path = os.path.join(tmpdir, f"chat_history_{timestamp}.pdf")
45
 
46
+ # TXT file (simple, clean)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  with open(txt_path, "w", encoding="utf-8") as f:
48
  for msg in history:
49
+ role = msg.get("role", "user").capitalize()
50
  content = msg.get("content", "")
51
  if isinstance(content, dict):
52
  content = content.get("text", "")
53
+ f.write(f"{role}:\n{str(content).strip()}\n\n")
 
 
 
54
 
55
+ # PDF file
56
  try:
57
  c = canvas.Canvas(pdf_path, pagesize=A4)
58
  page_width, page_height = A4
59
  margin = 40
60
  y = page_height - margin
61
+ line_height = 16
62
+ font_size = 11
63
  c.setFont("Helvetica", font_size)
64
 
65
  for msg in history:
 
68
  if isinstance(content, dict):
69
  content = content.get("text", "")
70
 
71
+ # Include timestamp if present
72
+ lines = str(content).splitlines()
 
73
  for line in lines:
74
+ wrapped = textwrap.wrap(line.strip(), width=90)
75
  for wline in wrapped:
76
  if y < margin + line_height:
77
  c.showPage()
78
  c.setFont("Helvetica", font_size)
79
  y = page_height - margin
80
+ c.drawString(margin, y, f"{role}: {wline}" if role == "User" else f"{wline}")
81
  y -= line_height
82
+ y -= line_height // 2 # small spacing between messages
83
 
84
  c.showPage()
85
  c.save()