Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,36 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
generador = pipeline("text-generation", model="IIC/MEL")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
archivos = st.file_uploader("Sube documentos (.txt, .md, .pdf)", accept_multiple_files=True)
|
| 14 |
-
if st.button("Generar respuesta"):
|
| 15 |
-
texts = []
|
| 16 |
-
for fichero in archivos:
|
| 17 |
try:
|
| 18 |
-
texto
|
| 19 |
except:
|
| 20 |
-
texto
|
| 21 |
-
|
| 22 |
-
contenido = "\n\n".join(texts)
|
| 23 |
-
entrada = prompt + "\n\n" + contenido
|
| 24 |
-
# Generar con límite de tokens razonable
|
| 25 |
salida = generador(entrada, max_new_tokens=512, do_sample=False)
|
| 26 |
-
respuesta = salida[0]
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Pipeline de generación genérica que acepta prompts
|
| 5 |
+
generador = pipeline("text-generation", model="IIC/MEL", trust_remote_code=True)
|
| 6 |
|
| 7 |
+
def procesar_tarea(archivos, prompt):
|
| 8 |
+
if not prompt:
|
| 9 |
+
return "", "Debes escribir un prompt"
|
| 10 |
+
texto = ""
|
| 11 |
+
for f in archivos:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
texto += f.read().decode("utf‑8") + "\n\n"
|
| 14 |
except:
|
| 15 |
+
texto += ""
|
| 16 |
+
entrada = prompt + "\n\n" + texto
|
|
|
|
|
|
|
|
|
|
| 17 |
salida = generador(entrada, max_new_tokens=512, do_sample=False)
|
| 18 |
+
respuesta = salida[0].get("generated_text", "")
|
| 19 |
+
return respuesta, respuesta
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
fn=procesar_tarea,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.File(label="Sube documentos (.txt, .md, .pdf)", file_count="multiple", type="file"),
|
| 25 |
+
gr.Textbox(label="Prompt (instrucción)", lines=3, placeholder="Ej. Resume este texto, explica esto...")
|
| 26 |
+
],
|
| 27 |
+
outputs=[
|
| 28 |
+
gr.Textbox(label="Resultado (para copiar)", lines=10),
|
| 29 |
+
gr.File(label="Descargar resultado", file_name="resultado.txt")
|
| 30 |
+
],
|
| 31 |
+
title="Interfaz MEL con Gradio",
|
| 32 |
+
description="Sube tus documentos, escribe un prompt y recibe la salida generada. Puedes copiar el texto o descargarlo."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|