Preeti Dave
commited on
Commit
·
7cbd5ba
1
Parent(s):
7e12507
app.py
CHANGED
|
@@ -1,7 +1,30 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the model and tokenizer from the Hugging Face Hub
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained("preetidav/my_model")
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("preetidav/my_model")
|
| 8 |
|
| 9 |
+
# Function to predict sentiment
|
| 10 |
+
def predict_sentiment(text):
|
| 11 |
+
# Tokenize the input text
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 13 |
+
# Forward pass through the model
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
# Get the prediction (0 or 1 for binary classification)
|
| 16 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 17 |
+
# Map prediction to sentiment labels
|
| 18 |
+
return "positive" if prediction == 1 else "negative"
|
| 19 |
+
|
| 20 |
+
# Set up the Gradio interface
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=predict_sentiment, # Function to call
|
| 23 |
+
inputs=gr.Textbox(label="Input Text"), # Input field for the text
|
| 24 |
+
outputs=gr.Textbox(label="Sentiment"), # Output field for the sentiment
|
| 25 |
+
title="Sentiment Analysis Model", # Title of the app
|
| 26 |
+
description="This model predicts whether a given text has positive or negative sentiment.", # Description of the app
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Launch the app
|
| 30 |
+
iface.launch()
|