-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (104 loc) · 3.18 KB
/
main.py
File metadata and controls
120 lines (104 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import gradio as gr
SAVE_PATH = "./distilbert_multiclass_sentiment/final_model"
LABEL_MAP = {0: "negative", 1: "neutral", 2: "positive"}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
loaded_tokenizer = AutoTokenizer.from_pretrained(SAVE_PATH)
loaded_model = AutoModelForSequenceClassification.from_pretrained(SAVE_PATH)
loaded_model.to(device)
loaded_model.eval()
def predict_sentiment(text, model=loaded_model, tokenizer=loaded_tokenizer, device=device, label_map=LABEL_MAP):
inputs = tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True
)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
logits = model(**inputs).logits
predicted_class_id = logits.argmax(dim=-1).item()
return label_map[predicted_class_id]
custom_css = """
/* General Container Styles */
.gradio-container {
background-color: #fafafa; /* Light background color */
border-radius: 12px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Soft shadow effect */
padding: 20px;
max-width: 500px;
max-height:500px;
margin: auto; /* Center the container */
}
/* Title Styling */
.gradio-title {
font-size: 24px;
font-weight: bold;
color: #333;
text-align: center;
margin-bottom: 20px;
}
/* Input Field Styling */
.gradio-input textarea {
width: 100%;
padding: 15px;
font-size: 16px;
border: 2px solid #007bff;
border-radius: 8px;
background-color: #fff;
box-sizing: border-box;
transition: all 0.3s ease-in-out;
}
/* Focus effect for textarea */
.gradio-input textarea:focus {
border-color: #0056b3;
box-shadow: 0 0 8px rgba(0, 91, 179, 0.3);
outline: none;
}
/* Button Styling */
.gradio-button {
background-color: #007bff;
color: white;
font-size: 16px;
font-weight: bold;
padding: 12px 20px;
border-radius: 8px;
border: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
/* Hover effect for button */
.gradio-button:hover {
background-color: #0056b3;
}
/* Output Text Styling */
.gradio-output {
font-size: 18px;
color: #333;
font-weight: 500;
margin-top: 20px;
text-align: center;
background-color: #f8f8f8;
padding: 15px;
border-radius: 8px;
border: 2px solid #ddd;
}
/* Extra margin for the input and output sections */
.gradio-input, .gradio-output {
margin-bottom: 20px;
}
/* Custom scrollbar */
.gradio-container::-webkit-scrollbar {
width: 8px;
}
.gradio-container::-webkit-scrollbar-thumb {
background-color: #007bff;
border-radius: 8px;
}
.gradio-container::-webkit-scrollbar-track {
background-color: #f1f1f1;
}
"""
demo = gr.Interface(fn=predict_sentiment, inputs="text", outputs="text", css=custom_css)
demo.launch()