Mthrfkr commited on
Commit
91007b3
verified
1 Parent(s): 5ce0bc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,19 +1,26 @@
1
  import gradio as gr
2
- import tempfile
3
 
4
- # Funci贸n: crea un .txt con la palabra y devuelve la ruta
5
- def crear_archivo(palabra):
6
- with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
7
- f.write(palabra)
8
- return f.name # Gradio tomar谩 esta ruta y ofrecer谩 el fichero
 
9
 
10
- # Interfaz Blocks
11
  with gr.Blocks() as demo:
12
- gr.Markdown("## 馃摜 Generador de archivo de texto\nEscribe una palabra y pulsa Descargar para obtener tu .txt")
13
- palabra = gr.Textbox(label="Escribe una palabra", placeholder="Tu palabra aqu铆...")
14
- download_btn = gr.Button("Descargar archivo", variant="primary")
15
- archivo = gr.File(label="Archivo generado")
16
- download_btn.click(fn=crear_archivo, inputs=[palabra], outputs=[archivo])
 
 
 
 
 
 
17
 
18
  if __name__ == "__main__":
19
  demo.launch()
 
 
1
  import gradio as gr
2
+ import io
3
 
4
+ # Genera un BytesIO con el texto y retorna (BytesIO, nombre)
5
+ def return_bytesio(text):
6
+ file_bytes = io.BytesIO()
7
+ file_bytes.write(text.encode("utf-8"))
8
+ file_bytes.seek(0)
9
+ return (file_bytes, "output.txt")
10
 
 
11
  with gr.Blocks() as demo:
12
+ gr.Markdown("## 馃摜 Simple Download Demo")
13
+
14
+ text_input = gr.Textbox(label="Escribe una palabra", placeholder="Ingresa algo...")
15
+ file_output = gr.File(label="Descargar Archivo")
16
+ download_btn = gr.Button("Generar & Descargar")
17
+
18
+ download_btn.click(
19
+ return_bytesio,
20
+ inputs=text_input,
21
+ outputs=file_output,
22
+ )
23
 
24
  if __name__ == "__main__":
25
  demo.launch()
26
+