sabitizen commited on
Commit
79a28f8
·
verified ·
1 Parent(s): cec867f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+ MODEL_PATH = "distilbert-imdb-chatbot"
7
+
8
+ # Load model once
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
10
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
11
+ model.eval()
12
+
13
+ def analyze_review(review):
14
+ if review.strip() == "":
15
+ return "Please enter a movie review."
16
+
17
+ inputs = tokenizer(
18
+ review,
19
+ return_tensors="pt",
20
+ truncation=True,
21
+ padding=True,
22
+ max_length=256
23
+ )
24
+
25
+ with torch.no_grad():
26
+ outputs = model(**inputs)
27
+
28
+ probs = F.softmax(outputs.logits, dim=1)
29
+ confidence, prediction = torch.max(probs, dim=1)
30
+
31
+ sentiment = "Positive 😊" if prediction.item() == 1 else "Negative 😞"
32
+
33
+ return f"""
34
+ 🎬 **Sentiment:** {sentiment}
35
+ 📊 **Confidence:** {confidence.item():.2f}
36
+ """
37
+
38
+ # Gradio UI
39
+ interface = gr.Interface(
40
+ fn=analyze_review,
41
+ inputs=gr.Textbox(
42
+ lines=4,
43
+ placeholder="Write a movie review here..."
44
+ ),
45
+ outputs="markdown",
46
+ title="🎬 Movie Review Chatbot",
47
+ description="DistilBERT fine-tuned on IMDB movie reviews"
48
+ )
49
+
50
+ if __name__ == "__main__":
51
+ interface.launch()