Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
from safetensors.torch import load_file
|
5 |
+
import torch.nn as nn
|
6 |
+
|
7 |
+
# Define the model class (same as in the training script)
|
8 |
+
class Magical1Sun(nn.Module):
|
9 |
+
def __init__(self, num_classes, dropout_rate=0.1):
|
10 |
+
super(Magical1Sun, self).__init__()
|
11 |
+
self.sentence_transformer = SentenceTransformer('all-MiniLM-L12-v2')
|
12 |
+
self.dropout = nn.Dropout(dropout_rate)
|
13 |
+
self.classifier = nn.Sequential(
|
14 |
+
nn.Linear(384, 256),
|
15 |
+
nn.ReLU(),
|
16 |
+
nn.Dropout(dropout_rate),
|
17 |
+
nn.Linear(256, num_classes)
|
18 |
+
)
|
19 |
+
|
20 |
+
def forward(self, text):
|
21 |
+
embeddings = self.sentence_transformer.encode(text, convert_to_tensor=True)
|
22 |
+
embeddings = self.dropout(embeddings)
|
23 |
+
return self.classifier(embeddings)
|
24 |
+
|
25 |
+
# Load the trained model
|
26 |
+
def load_model(model_path):
|
27 |
+
model = Magical1Sun(num_classes=2)
|
28 |
+
state_dict = load_file(model_path)
|
29 |
+
model.load_state_dict(state_dict)
|
30 |
+
model.eval()
|
31 |
+
return model
|
32 |
+
|
33 |
+
# Prediction function
|
34 |
+
def predict(text):
|
35 |
+
with torch.no_grad():
|
36 |
+
output = model(text)
|
37 |
+
probabilities = torch.softmax(output, dim=0)
|
38 |
+
positive_prob = probabilities[1].item()
|
39 |
+
negative_prob = probabilities[0].item()
|
40 |
+
prediction = "Positive" if positive_prob > negative_prob else "Negative"
|
41 |
+
confidence = max(positive_prob, negative_prob)
|
42 |
+
return {
|
43 |
+
"Prediction": prediction,
|
44 |
+
"Confidence": f"{confidence:.2%}",
|
45 |
+
"Positive Probability": f"{positive_prob:.2%}",
|
46 |
+
"Negative Probability": f"{negative_prob:.2%}"
|
47 |
+
}
|
48 |
+
|
49 |
+
# Load the model (make sure to replace 'path_to_your_model.safetensors' with the actual path)
|
50 |
+
model = load_model('magical_1_sun.safetensors')
|
51 |
+
|
52 |
+
# Create the Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=predict,
|
55 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter text to classify..."),
|
56 |
+
outputs=[
|
57 |
+
gr.Label(num_top_classes=1, label="Prediction"),
|
58 |
+
gr.Label(label="Confidence"),
|
59 |
+
gr.Label(label="Positive Probability"),
|
60 |
+
gr.Label(label="Negative Probability")
|
61 |
+
],
|
62 |
+
title="Magical-1 Sun Text Classification",
|
63 |
+
description="Enter a text to classify it as positive or negative.",
|
64 |
+
examples=[
|
65 |
+
["I love this product! It's amazing!"],
|
66 |
+
["This is terrible. Worst purchase ever."],
|
67 |
+
["Great experience overall. Would buy again."],
|
68 |
+
["Never buying again. Complete waste of money."],
|
69 |
+
["Highly recommended! You won't regret it."]
|
70 |
+
]
|
71 |
+
)
|
72 |
+
|
73 |
+
# Launch the app
|
74 |
+
if __name__ == "__main__":
|
75 |
+
iface.launch()
|