Spaces:
Sleeping
Sleeping
File size: 1,703 Bytes
07d358b 936781a 07d358b 936781a |
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 53 54 55 56 57 |
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) |