Entregable3 / app.py
NatanGarMar's picture
Update app.py
2233766 verified
raw
history blame contribute delete
994 Bytes
from huggingface_hub import from_pretrained_fastai
import gradio as gr
from fastai.text.all import *
# Reemplaza con tu propio repo_id
repo_id = "NatanGarMar/entregable3"
# Cargar el modelo preentrenado de Hugging Face
learner = from_pretrained_fastai(repo_id)
labels = learner.dls.vocab
# Definimos una función que se encarga de llevar a cabo las predicciones
def predict(text):
result = learner.predict(text)
if len(result) == 3:
pred, pred_idx, probs = result
return {labels[i]: float(probs[i]) for i in range(len(labels))}
elif len(result) == 2:
pred, probs = result
return {labels[i]: float(probs[i]) for i in range(len(labels))}
else:
return {"Error": "Unexpected output format from learner"}
# Creamos la interfaz y la lanzamos
gr.Interface(fn=predict, inputs=gr.inputs.Textbox(lines=2, placeholder="Finantial Sentence"), outputs=gr.outputs.Label(num_top_classes=3), examples=['text1.txt', 'text2.txt']).launch(share=False)