Spaces:
Sleeping
Sleeping
| import re | |
| import datetime | |
| from pathlib import Path | |
| import gradio as gr | |
| import os | |
| import sys | |
| import inspect | |
| from typing import Optional | |
| def make_txt(title: str, content: str) -> str: | |
| """ | |
| Crea un archivo .txt con un título y contenido y devuelve la ruta al archivo. | |
| Args: | |
| title (str): Título del documento. | |
| content (str): Contenido del documento. | |
| Returns: | |
| str: Ruta al archivo creado. | |
| """ | |
| if title is None: | |
| title = "" | |
| # Sanitizar el título para usarlo como nombre de archivo | |
| safe = re.sub(r'[^A-Za-z0-9 _-]', '', title).strip() | |
| safe = safe.replace(' ', '_') | |
| timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
| filename = f"{safe}_{timestamp}.txt" if safe else f"file_{timestamp}.txt" | |
| outdir = Path(__file__).parent / "outputs" | |
| outdir.mkdir(exist_ok=True) | |
| filepath = outdir / filename | |
| header = title if title else "(Sin título)" | |
| underline = "=" * len(header) | |
| with filepath.open("w", encoding="utf-8") as f: | |
| f.write(header + "\n") | |
| f.write(underline + "\n\n") | |
| f.write(content if content else "") | |
| return str(filepath) | |
| demo = gr.Interface( | |
| fn=make_txt, | |
| inputs=[ | |
| gr.Textbox(label="Título", placeholder="Escribe el título aquí"), | |
| gr.Textbox(label="Contenido", lines=10, placeholder="Escribe el contenido del .txt aquí"), | |
| ], | |
| outputs=gr.File(label="Descargar .txt"), | |
| title="Generador de .txt", | |
| description="Genera un archivo .txt con un título y contenido." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) |