Spaces:
Sleeping
Sleeping
File size: 2,055 Bytes
0e8ec0a 68c2148 0e8ec0a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import json
from datetime import datetime
import pickle
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
import requests
def export_txt(conversation, topic, filename="conversation.txt"):
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(f"Topic: {topic}\n\n")
for turn in conversation:
f.write(f"{turn['agent']}: {turn['text']}\n")
return filename
except Exception as e:
logging.error(f"[export_txt] Failed to write file: {e}")
return None
def export_json(conversation: list, topic: str, turns: int) -> str:
filename = f"discussion_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
data = {"topic": topic, "turns": turns, "conversation": conversation}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
return filename
def export_pdf(conversation: list, topic: str, turns: int) -> str:
filename = f"discussion_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
doc = SimpleDocTemplate(filename, pagesize=letter)
styles = getSampleStyleSheet()
story = [Paragraph(f"Discussion: {topic}", styles['Title']), Spacer(1, 12)]
for msg in conversation:
story.append(Paragraph(f"<b>{msg['agent']}</b> (Turn {msg.get('turn')}):", styles['Normal']))
story.append(Paragraph(msg['text'], styles['BodyText']))
story.append(Spacer(1, 12))
doc.build(story)
return filename
def send_webhook(url: str, conversation: list, topic: str, turns: int) -> str:
if not url.startswith('http'):
return '⚠️ Invalid URL'
payload = {"topic": topic, "turns": turns, "conversation": conversation}
try:
resp = requests.post(url, json=payload, timeout=10)
if resp.status_code == 200:
return '✅ Sent successfully'
return f"⚠️ Error {resp.status_code}: {resp.text}"
except Exception as e:
return f"⚠️ Exception: {e}" |