Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from zipfile import ZipFile
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Importing the sentiment_analysis function
|
6 |
+
def sentiment_analysis(dated_input):
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("aliciiavs/sentiment-analysis-whatsapp2")
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained("aliciiavs/sentiment-analysis-whatsapp2")
|
9 |
+
|
10 |
+
# Tokenize input
|
11 |
+
inputs = tokenizer(dated_input, padding=True, return_tensors="pt")
|
12 |
+
|
13 |
+
# Forward pass through the model
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = model(**inputs)
|
16 |
+
|
17 |
+
# Get predicted probabilities and predicted label
|
18 |
+
probabilities = torch.softmax(outputs.logits, dim=1)
|
19 |
+
predicted_label = torch.argmax(probabilities, dim=1)
|
20 |
+
|
21 |
+
# Convert the predicted label tensor to a Python integer
|
22 |
+
predicted_label = predicted_label.item()
|
23 |
+
|
24 |
+
# Map predicted label index to sentiment label
|
25 |
+
label_dic = {0: 'sadness', 1: 'joy', 2: 'love', 3: 'anger', 4: 'fear', 5: 'surprise'}
|
26 |
+
|
27 |
+
# Return the predicted sentiment label instead of printing it
|
28 |
+
return label_dic[predicted_label]
|
29 |
+
|
30 |
+
# Define a Gradio interface
|
31 |
+
def sentiment_analysis_interface(zip_file):
|
32 |
+
# Extract text from zip file
|
33 |
+
with ZipFile(zip_file) as archive:
|
34 |
+
# Assuming each file in the zip contains text
|
35 |
+
text = ""
|
36 |
+
for filename in archive.namelist():
|
37 |
+
with archive.open(filename) as file:
|
38 |
+
text += file.read().decode("utf-8")
|
39 |
+
|
40 |
+
# Perform sentiment analysis
|
41 |
+
predicted_sentiment = sentiment_analysis(text)
|
42 |
+
|
43 |
+
# Return the predicted sentiment label
|
44 |
+
return f"Predicted sentiment label: {predicted_sentiment}"
|
45 |
+
|
46 |
+
# Create a Gradio interface
|
47 |
+
gr.Interface(
|
48 |
+
fn=sentiment_analysis_interface,
|
49 |
+
inputs=gr.inputs.File(type="zip", label="Upload a zip file"),
|
50 |
+
outputs="text"
|
51 |
+
).launch()
|