Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Ruta del archivo de texto plano
|
| 5 |
+
file_path = "/texto_plano.txt" # Asegúrate de cargar este archivo en Colab
|
| 6 |
+
|
| 7 |
+
# Cargar el texto plano
|
| 8 |
+
try:
|
| 9 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 10 |
+
context_data = file.read()
|
| 11 |
+
print("Texto cargado correctamente.")
|
| 12 |
+
except FileNotFoundError:
|
| 13 |
+
print(f"El archivo {file_path} no se encuentra. Verifica la ruta y vuelve a intentarlo.")
|
| 14 |
+
context_data = "" # Evitar errores si el archivo no se encuentra
|
| 15 |
+
|
| 16 |
+
if context_data:
|
| 17 |
+
print(f"Context data cargado: {context_data[:100]}...") # Mostrar una parte del texto cargado
|
| 18 |
+
|
| 19 |
+
# Cargar el modelo bigscience/bloomz-560m
|
| 20 |
+
print("Cargando el modelo bigscience/bloomz-560m...")
|
| 21 |
+
model_name = "bigscience/bloomz-560m"
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(model_name) # Cambiamos a AutoModelForCausalLM
|
| 24 |
+
print("Modelo bigscience/bloomz-560m cargado correctamente.")
|
| 25 |
+
|
| 26 |
+
# Función para responder preguntas
|
| 27 |
+
def answer_question(question):
|
| 28 |
+
input_text = f"Pregunta: {question}\nContexto: {context_data}"
|
| 29 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 30 |
+
outputs = model.generate(inputs, max_length=200)
|
| 31 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
+
return f"Pregunta: {question}\nRespuesta: {response}"
|
| 33 |
+
|
| 34 |
+
# Interfaz con Gradio
|
| 35 |
+
def answer_question_interface(question):
|
| 36 |
+
return answer_question(question)
|
| 37 |
+
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=answer_question_interface,
|
| 40 |
+
inputs="text",
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="QA - bigscience/bloomz-560m",
|
| 43 |
+
description="Haz preguntas abiertas sobre el contenido narrativo."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch()
|