Spaces:
Running
Running
File size: 849 Bytes
5b76d85 |
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 |
import os
import chainlit as cl
import uuid
from datetime import datetime
# Ensure public/jsons directory exists
os.makedirs("public/jsons", exist_ok=True)
def save_response(content: str) -> tuple[str, str]:
"""
Save response content to a file and return filename and filepath
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
file_uuid = str(uuid.uuid4())
filename = f"layer_{file_uuid}_{timestamp}.json"
filepath = os.path.join("public/jsons", filename)
with open(filepath, "w", encoding="utf-8") as f:
f.write(content)
return filename, filepath
def create_file_element(filename: str, filepath: str) -> cl.File:
"""
Create a Chainlit File element for the saved response
"""
return cl.File(
name=filename,
path=filepath,
display="inline",
) |