arpitk commited on
Commit
aaca144
·
verified ·
1 Parent(s): 7dd5335

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+
5
+ # Load model and tokenizer
6
+ model_path = "yourusername/product-review-sentiment-analyzer"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
9
+
10
+ # Define sentiment labels
11
+ labels = ["Negative", "Positive", "Neutral"]
12
+
13
+ # Define prediction function
14
+ def predict(text):
15
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+
19
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
20
+ prediction = torch.argmax(probabilities, dim=-1).item()
21
+
22
+ return {labels[i]: float(probabilities[0][i]) for i in range(len(labels))}
23
+
24
+ # Create Gradio interface
25
+ demo = gr.Interface(
26
+ fn=predict,
27
+ inputs=gr.Textbox(placeholder="Enter a product review..."),
28
+ outputs=gr.Label(num_top_classes=3),
29
+ title="Product Review Sentiment Analyzer",
30
+ description="Analyze the sentiment of product reviews as Positive, Negative, or Neutral."
31
+ )
32
+
33
+ # Launch app
34
+ demo.launch()