Spaces:
Running
Running
Commit
Β·
84fe593
1
Parent(s):
0ce5e49
updatation
Browse files
app.py
CHANGED
|
@@ -42,13 +42,9 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 42 |
"let me know", "is there anything else",
|
| 43 |
"anything else i can help", "feel free to ask",
|
| 44 |
"hope this helps", "need further assistance",
|
| 45 |
-
"feel free", "happy to help",
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"What did you like about our interaction?",
|
| 49 |
-
"Do you have any feedback on your experience?" ,
|
| 50 |
-
|
| 51 |
-
"would you like to explore", "need clarification"
|
| 52 |
]
|
| 53 |
if not lines:
|
| 54 |
return lines
|
|
@@ -59,19 +55,28 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 59 |
|
| 60 |
with open(txt_path, "w", encoding="utf-8") as f:
|
| 61 |
for msg in history:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
content = msg.get("content", "")
|
|
|
|
|
|
|
|
|
|
| 63 |
lines = content.splitlines()
|
| 64 |
|
|
|
|
| 65 |
clean = [l for l in lines if not l.strip().startswith("π")]
|
| 66 |
-
|
| 67 |
-
clean = clean[1:]
|
| 68 |
-
clean = [l for l in clean if not set(l) == {"-"}]
|
| 69 |
clean = [l.replace("USER:", "").replace("ASSISTANT:", "").strip() for l in clean]
|
| 70 |
|
| 71 |
clean = remove_last_closing_line(clean)
|
| 72 |
-
f.write("\n".join(clean).strip() + "\n")
|
| 73 |
-
f.write("-" * 60 + "\n")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
pdf_path = None
|
| 76 |
try:
|
| 77 |
from reportlab.lib.pagesizes import A4
|
|
@@ -81,12 +86,15 @@ def export_chat_files(history: List[Dict[str, Any]]) -> Dict[str, Optional[str]]
|
|
| 81 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 82 |
width, height = A4
|
| 83 |
margin = 40
|
|
|
|
| 84 |
textobject = c.beginText(margin, height - margin)
|
| 85 |
textobject.setFont("Helvetica", 10)
|
|
|
|
| 86 |
with open(txt_path, "r", encoding="utf-8") as fh:
|
| 87 |
for line in fh:
|
| 88 |
for wrapped in textwrap.wrap(line.rstrip(), 100):
|
| 89 |
textobject.textLine(wrapped)
|
|
|
|
| 90 |
c.drawText(textobject)
|
| 91 |
c.showPage()
|
| 92 |
c.save()
|
|
@@ -287,31 +295,17 @@ with gr.Blocks(title="Tayyab β Chatbot") as demo:
|
|
| 287 |
|
| 288 |
new_chat_btn.click(new_chat, outputs=[chatbot])
|
| 289 |
|
| 290 |
-
# def export_handler(history):
|
| 291 |
-
# files = export_chat_files(history or [])
|
| 292 |
-
# return (
|
| 293 |
-
# gr.update(value=files.get("txt"), visible=True),
|
| 294 |
-
# gr.update(value=files.get("pdf"), visible=bool(files.get("pdf")))
|
| 295 |
-
# )
|
| 296 |
-
|
| 297 |
-
# export_btn.click(export_handler, inputs=[chatbot], outputs=[file_txt, file_pdf])
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
def export_handler(history):
|
| 302 |
-
|
|
|
|
| 303 |
|
| 304 |
-
for user_msg, bot_msg in history:
|
| 305 |
-
fixed_history.append({"role": "user", "content": user_msg})
|
| 306 |
-
fixed_history.append({"role": "assistant", "content": bot_msg})
|
| 307 |
-
|
| 308 |
-
files = export_chat_files(fixed_history)
|
| 309 |
-
|
| 310 |
return (
|
| 311 |
gr.update(value=files.get("txt"), visible=True),
|
| 312 |
gr.update(value=files.get("pdf"), visible=bool(files.get("pdf")))
|
| 313 |
)
|
| 314 |
|
|
|
|
| 315 |
export_btn.click(export_handler, inputs=[chatbot], outputs=[file_txt, file_pdf])
|
| 316 |
|
| 317 |
|
|
|
|
| 42 |
"let me know", "is there anything else",
|
| 43 |
"anything else i can help", "feel free to ask",
|
| 44 |
"hope this helps", "need further assistance",
|
| 45 |
+
"feel free", "happy to help",
|
| 46 |
+
"how can i assist", "do you have",
|
| 47 |
+
"would you like to", "need clarification"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
]
|
| 49 |
if not lines:
|
| 50 |
return lines
|
|
|
|
| 55 |
|
| 56 |
with open(txt_path, "w", encoding="utf-8") as f:
|
| 57 |
for msg in history:
|
| 58 |
+
# Ensure always a dict
|
| 59 |
+
if not isinstance(msg, dict):
|
| 60 |
+
continue
|
| 61 |
+
|
| 62 |
content = msg.get("content", "")
|
| 63 |
+
if not isinstance(content, str):
|
| 64 |
+
content = str(content)
|
| 65 |
+
|
| 66 |
lines = content.splitlines()
|
| 67 |
|
| 68 |
+
# CLEANUP
|
| 69 |
clean = [l for l in lines if not l.strip().startswith("π")]
|
| 70 |
+
clean = [l for l in clean if l.strip() != ""]
|
|
|
|
|
|
|
| 71 |
clean = [l.replace("USER:", "").replace("ASSISTANT:", "").strip() for l in clean]
|
| 72 |
|
| 73 |
clean = remove_last_closing_line(clean)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
if clean:
|
| 76 |
+
f.write("\n".join(clean).strip() + "\n")
|
| 77 |
+
f.write("-" * 60 + "\n")
|
| 78 |
+
|
| 79 |
+
# PDF
|
| 80 |
pdf_path = None
|
| 81 |
try:
|
| 82 |
from reportlab.lib.pagesizes import A4
|
|
|
|
| 86 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 87 |
width, height = A4
|
| 88 |
margin = 40
|
| 89 |
+
|
| 90 |
textobject = c.beginText(margin, height - margin)
|
| 91 |
textobject.setFont("Helvetica", 10)
|
| 92 |
+
|
| 93 |
with open(txt_path, "r", encoding="utf-8") as fh:
|
| 94 |
for line in fh:
|
| 95 |
for wrapped in textwrap.wrap(line.rstrip(), 100):
|
| 96 |
textobject.textLine(wrapped)
|
| 97 |
+
|
| 98 |
c.drawText(textobject)
|
| 99 |
c.showPage()
|
| 100 |
c.save()
|
|
|
|
| 295 |
|
| 296 |
new_chat_btn.click(new_chat, outputs=[chatbot])
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
def export_handler(history):
|
| 300 |
+
# history is already list of dicts: [{"role": "...", "content": "..."}]
|
| 301 |
+
files = export_chat_files(history or [])
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
return (
|
| 304 |
gr.update(value=files.get("txt"), visible=True),
|
| 305 |
gr.update(value=files.get("pdf"), visible=bool(files.get("pdf")))
|
| 306 |
)
|
| 307 |
|
| 308 |
+
|
| 309 |
export_btn.click(export_handler, inputs=[chatbot], outputs=[file_txt, file_pdf])
|
| 310 |
|
| 311 |
|