Spaces:
Sleeping
Sleeping
Commit
Β·
674b157
1
Parent(s):
22487cb
updatation
Browse files
app.py
CHANGED
|
@@ -43,16 +43,30 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 52 |
-
content = content.get("text", "")
|
| 53 |
-
f.write(f"{role}:\n{str(content).strip()}\n\n")
|
| 54 |
|
| 55 |
-
# PDF
|
| 56 |
try:
|
| 57 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 58 |
page_width, page_height = A4
|
|
@@ -64,22 +78,19 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 64 |
|
| 65 |
for msg in history:
|
| 66 |
role = msg.get("role", "user").capitalize()
|
| 67 |
-
content = msg.get("content", "")
|
| 68 |
-
if isinstance(content, dict):
|
| 69 |
-
content = content.get("text", "")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
lines = str(content).splitlines()
|
| 73 |
for line in lines:
|
| 74 |
-
wrapped = textwrap.wrap(line
|
| 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
|
| 81 |
y -= line_height
|
| 82 |
-
y -= line_height // 2
|
| 83 |
|
| 84 |
c.showPage()
|
| 85 |
c.save()
|
|
@@ -88,8 +99,6 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 88 |
pdf_path = None
|
| 89 |
|
| 90 |
return {"txt": txt_path, "pdf": pdf_path}
|
| 91 |
-
|
| 92 |
-
|
| 93 |
# ----------------------
|
| 94 |
# Core chat function
|
| 95 |
# ----------------------
|
|
|
|
| 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 |
+
def clean_content(content):
|
| 47 |
+
"""Extract string from Gradio format and remove final timestamp."""
|
| 48 |
+
text = ""
|
| 49 |
+
if isinstance(content, list):
|
| 50 |
+
for item in content:
|
| 51 |
+
if isinstance(item, dict) and "text" in item:
|
| 52 |
+
text += item["text"] + "\n"
|
| 53 |
+
elif isinstance(content, dict) and "text" in content:
|
| 54 |
+
text = content["text"]
|
| 55 |
+
else:
|
| 56 |
+
text = str(content)
|
| 57 |
+
|
| 58 |
+
# Remove timestamp line starting with β or π
|
| 59 |
+
lines = [l for l in text.splitlines() if not l.strip().startswith(("β ", "π"))]
|
| 60 |
+
return "\n".join(lines).strip()
|
| 61 |
+
|
| 62 |
+
# ---------------- TXT FILE ----------------
|
| 63 |
with open(txt_path, "w", encoding="utf-8") as f:
|
| 64 |
for msg in history:
|
| 65 |
role = msg.get("role", "user").capitalize()
|
| 66 |
+
content = clean_content(msg.get("content", ""))
|
| 67 |
+
f.write(f"{role}:\n{content}\n\n")
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# ---------------- PDF FILE ----------------
|
| 70 |
try:
|
| 71 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 72 |
page_width, page_height = A4
|
|
|
|
| 78 |
|
| 79 |
for msg in history:
|
| 80 |
role = msg.get("role", "user").capitalize()
|
| 81 |
+
content = clean_content(msg.get("content", ""))
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
lines = content.splitlines()
|
|
|
|
| 84 |
for line in lines:
|
| 85 |
+
wrapped = textwrap.wrap(line, width=90)
|
| 86 |
for wline in wrapped:
|
| 87 |
if y < margin + line_height:
|
| 88 |
c.showPage()
|
| 89 |
c.setFont("Helvetica", font_size)
|
| 90 |
y = page_height - margin
|
| 91 |
+
c.drawString(margin, y, f"{role}: {wline}" if role=="User" else wline)
|
| 92 |
y -= line_height
|
| 93 |
+
y -= line_height // 2
|
| 94 |
|
| 95 |
c.showPage()
|
| 96 |
c.save()
|
|
|
|
| 99 |
pdf_path = None
|
| 100 |
|
| 101 |
return {"txt": txt_path, "pdf": pdf_path}
|
|
|
|
|
|
|
| 102 |
# ----------------------
|
| 103 |
# Core chat function
|
| 104 |
# ----------------------
|