Kishan-Ved commited on
Commit
ba16edd
·
verified ·
1 Parent(s): 8e15aac

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ # Load the tokenizer and model
6
+ model_name = "NLPTeamIITGN/finetuned_llama_sentiment_sst2"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ # Define a label map
11
+ label_map = {0: "Negative", 1: "Positive"}
12
+
13
+ # Function to predict sentiment
14
+ def predict_sentiment(review):
15
+ if not review.strip():
16
+ return "No input provided", 0.0
17
+ inputs = tokenizer(review, return_tensors="pt", truncation=True, padding=True, max_length=512)
18
+ outputs = model(**inputs)
19
+ logits = outputs.logits.detach().numpy()
20
+ predicted_label = np.argmax(logits, axis=-1)[0]
21
+ predicted_class = label_map[predicted_label]
22
+ probability = np.max(logits, axis=-1)[0]
23
+ return predicted_class, round(float(probability), 2)
24
+
25
+ # Gradio Interface
26
+ interface = gr.Interface(
27
+ fn=predict_sentiment,
28
+ inputs=gr.Textbox(lines=3, placeholder="Enter a movie review here...", label="Movie Review"),
29
+ outputs=[
30
+ gr.Label(label="Sentiment"),
31
+ gr.Number(label="Confidence Score")
32
+ ],
33
+ title="Sentiment Analysis for Movie Reviews",
34
+ description="Enter a movie review, and the model will classify it as Positive or Negative with a confidence score."
35
+ )
36
+
37
+ # Launch the Gradio app
38
+ interface.launch()