gsar78's picture
Update app.py
197a176 verified
raw
history blame
1.42 kB
import gradio as gr
def predict(text):
# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")
# Get the model outputs
outputs = model(**inputs)
# Apply softmax to the logits to get probabilities
scores = torch.nn.functional.softmax(outputs.logits, dim=1)
# Get the predicted label
predicted_label_idx = scores.argmax(dim=1).item()
labels = ["negative", "neutral", "positive"]
predicted_label = labels[predicted_label_idx]
confidence_score = scores[0, predicted_label_idx].item()
# Create a dictionary with the prediction and scores
result = {
"text": text,
"label": predicted_label,
"score": confidence_score,
"scores": {
"positive": scores[0, 2].item(),
"neutral": scores[0, 1].item(),
"negative": scores[0, 0].item()
}
}
return result
iface = gr.Interface(
fn=predict,
inputs="text",
outputs=[
gr.Textbox(label="Prediction"),
gr.Label(label="Label Confidence")
],
title="Hellenic Sentiment AI",
description=None,
article=None,
theme="default",
flagging_dir=None,
favicon_path=None,
css=None,
analytics_script=None,
allow_flagging="never",
allow_screenshot=True,
enable_queue=True,
show_input=True,
show_output=True,
footer="Development by Geo Sar"
)
iface.launch(share=True)