Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
def __init__(self):
|
| 9 |
super().__init__()
|
| 10 |
-
self.
|
| 11 |
-
|
| 12 |
def forward(self, x):
|
| 13 |
-
return self.
|
| 14 |
|
| 15 |
-
model =
|
| 16 |
-
model.load_state_dict(torch.load("model.
|
| 17 |
model.eval()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with torch.no_grad():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
""")
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
-
|
| 44 |
-
### About
|
| 45 |
-
|
| 46 |
-
-
|
| 47 |
-
-
|
|
|
|
|
|
|
|
|
|
| 48 |
""")
|
| 49 |
|
| 50 |
-
|
| 51 |
label="Enter Text",
|
| 52 |
-
placeholder="Type
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
-
output = gr.Label(label="
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
|
|
|
| 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()
|