Spaces:
Sleeping
Sleeping
| 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", | |
| ) |