|
import gradio as gr |
|
from zipfile import ZipFile |
|
import os |
|
|
|
|
|
|
|
def sentiment_analysis(dated_input): |
|
tokenizer = AutoTokenizer.from_pretrained("aliciiavs/sentiment-analysis-whatsapp2") |
|
model = AutoModelForSequenceClassification.from_pretrained("aliciiavs/sentiment-analysis-whatsapp2") |
|
|
|
|
|
inputs = tokenizer(dated_input, padding=True, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
probabilities = torch.softmax(outputs.logits, dim=1) |
|
predicted_label = torch.argmax(probabilities, dim=1) |
|
|
|
|
|
predicted_label = predicted_label.item() |
|
|
|
|
|
label_dic = {0: 'sadness', 1: 'joy', 2: 'love', 3: 'anger', 4: 'fear', 5: 'surprise'} |
|
|
|
|
|
return label_dic[predicted_label] |
|
|
|
|
|
def sentiment_analysis_interface(zip_file): |
|
|
|
with ZipFile(zip_file) as archive: |
|
|
|
text = "" |
|
for filename in archive.namelist(): |
|
with archive.open(filename) as file: |
|
text += file.read().decode("utf-8") |
|
|
|
|
|
predicted_sentiment = sentiment_analysis(text) |
|
|
|
|
|
return f"Predicted sentiment label: {predicted_sentiment}" |
|
|
|
|
|
gr.Interface( |
|
fn=sentiment_analysis_interface, |
|
inputs=gr.File(type="filepath", label="Upload a zip file"), |
|
outputs="text" |
|
).launch() |
|
|