Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,30 @@
|
|
1 |
print("first test for hugging face")
|
2 |
-
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
4 |
|
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
|
6 |
-
model = AutoModelForSequenceClassification.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
print("first test for hugging face")
|
2 |
+
import gradio as gr
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the tokenizer and model
|
7 |
tokenizer = AutoTokenizer.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained("Remicm/sentiment-analysis-model-for-socialmedia")
|
9 |
+
|
10 |
+
# Function to predict sentiment
|
11 |
+
def predict_sentiment(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
17 |
+
|
18 |
+
# Define sentiment labels (adjust based on your model's output)
|
19 |
+
sentiments = ["Negative", "Neutral", "Positive"]
|
20 |
+
return sentiments[predicted_class]
|
21 |
+
|
22 |
+
# Create the Gradio interface
|
23 |
+
interface = gr.Interface(fn=predict_sentiment,
|
24 |
+
inputs="text",
|
25 |
+
outputs="label",
|
26 |
+
title="Sentiment Analysis of Instagram Comments",
|
27 |
+
description="Enter a comment to determine its sentiment (Positive, Neutral, Negative).")
|
28 |
+
|
29 |
+
# Launch the interface
|
30 |
+
interface.launch()
|