Spaces:
Sleeping
Sleeping
File size: 1,443 Bytes
ee384f0 9e224eb ee384f0 388d058 9e224eb 388d058 9e224eb 388d058 9e224eb 388d058 9e224eb 388d058 9e224eb ee384f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import gradio as gr
import torch
from transformers import BertTokenizer, BertForSequenceClassification
model_path = "my_model"
tokenizer = BertTokenizer.from_pretrained(model_path)
model = BertForSequenceClassification.from_pretrained(model_path)
device = torch.device("cpu")
model.to(device)
model.eval()
def predict(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probs = torch.softmax(logits, dim=1)
predicted_class = torch.argmax(probs, dim=1).item()
confidence = probs[0][predicted_class].item()
label = "π’ Positive" if predicted_class == 1 else "π΄ Negative"
return label, f"{confidence:.2f}"
# π¨ CUSTOM UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π¬ AI Sentiment Analyzer
### Analyze emotions in text using BERT π€
""")
with gr.Row():
text_input = gr.Textbox(
placeholder="Type your sentence here...",
lines=3,
label="Input Text"
)
analyze_btn = gr.Button("Analyze Sentiment π")
with gr.Row():
result_label = gr.Textbox(label="Prediction")
confidence_score = gr.Textbox(label="Confidence")
analyze_btn.click(
fn=predict,
inputs=text_input,
outputs=[result_label, confidence_score]
)
demo.launch() |