AyusmanSamasi commited on
Commit
6b8b5fa
Β·
verified Β·
1 Parent(s): b674722

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -1,60 +1,62 @@
1
  import torch
2
- import torch.nn as nn
3
- from torch.nn.functional import softmax
4
  import gradio as gr
 
5
 
6
- # ---------- Load Your Model ----------
7
- class SentimentModel(nn.Module):
 
 
8
  def __init__(self):
9
  super().__init__()
10
- self.model = nn.Linear(768, 2) # example
11
-
12
  def forward(self, x):
13
- return self.model(x)
14
 
15
- model = SentimentModel()
16
- model.load_state_dict(torch.load("model.pth", map_location="cpu"))
17
  model.eval()
18
 
19
- # ---------- Prediction Function ----------
20
- def predict_sentiment(text):
21
- # Convert text β†’ embedding (example)
22
- # replace with your own preprocessing
 
 
 
 
23
  with torch.no_grad():
24
- x = torch.rand(1, 768) # dummy embedding, replace with real one
25
- output = model(x)
26
- probs = softmax(output, dim=1)
27
-
28
- labels = ["Negative", "Positive"]
29
- result = {
30
- labels[i]: float(probs[0][i])
31
- for i in range(2)
32
- }
33
  return result
34
 
35
- # ---------- Gradio UI ----------
36
- with gr.Blocks() as demo:
37
- gr.Markdown("""
38
- ## πŸ” Sentiment Analysis
39
- Paste text on the right β†’ Get prediction on the left.
40
- """)
41
 
42
  with gr.Row():
43
- info_box = gr.Markdown("""
44
- ### About This Model
45
- - Custom PyTorch sentiment classifier
46
- - Trained on your dataset
47
- - Upload text to analyze
 
 
 
48
  """)
49
 
50
- input_text = gr.Textbox(
51
  label="Enter Text",
52
- placeholder="Type your sentence here..."
 
53
  )
54
 
55
- output = gr.Label(label="Prediction")
56
 
57
- submit_btn = gr.Button("Analyze")
58
- submit_btn.click(predict_sentiment, inputs=input_text, outputs=output)
59
 
60
- demo.launch()
 
1
  import torch
 
 
2
  import gradio as gr
3
+ import torch.nn.functional as F
4
 
5
+ # --------------------
6
+ # Load Model
7
+ # --------------------
8
+ class YourModelClass(torch.nn.Module):
9
  def __init__(self):
10
  super().__init__()
11
+ self.linear = torch.nn.Linear(768, 5) # example
 
12
  def forward(self, x):
13
+ return self.linear(x)
14
 
15
+ model = YourModelClass()
16
+ model.load_state_dict(torch.load("model.pt", map_location="cpu"))
17
  model.eval()
18
 
19
+ labels = ["anger", "fear", "joy", "sadness", "surprise"]
20
+
21
+ # --------------------
22
+ # Prediction Function
23
+ # --------------------
24
+ def predict(text):
25
+ # TODO: replace with your real preprocessing:
26
+ x = torch.rand(1, 768) # dummy input
27
  with torch.no_grad():
28
+ logits = model(x)
29
+ probs = F.softmax(logits, dim=1)[0]
30
+
31
+ result = {labels[i]: float(probs[i]) for i in range(len(labels))}
 
 
 
 
 
32
  return result
33
 
34
+ # --------------------
35
+ # Gradio UI
36
+ # --------------------
37
+ with gr.Blocks() as app:
38
+ gr.Markdown("## 🌈 Emotion Classifier β€” Multi-label Text Analysis")
 
39
 
40
  with gr.Row():
41
+ left = gr.Markdown("""
42
+ ### πŸ“„ About
43
+ Upload or type text β†’ get emotional predictions:
44
+ - Anger
45
+ - Fear
46
+ - Joy
47
+ - Sadness
48
+ - Surprise
49
  """)
50
 
51
+ text_input = gr.Textbox(
52
  label="Enter Text",
53
+ placeholder="Type here...",
54
+ lines=6
55
  )
56
 
57
+ output = gr.Label(label="Emotion Probabilities")
58
 
59
+ btn = gr.Button("Analyze")
60
+ btn.click(predict, inputs=text_input, outputs=output)
61
 
62
+ app.launch()