Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
from sklearn.metrics import f1_score
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# ----------------------------
|
| 10 |
+
# Load Hugging Face Model
|
| 11 |
+
# ----------------------------
|
| 12 |
+
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 16 |
+
|
| 17 |
+
labels = ["negative", "neutral", "positive"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ----------------------------
|
| 21 |
+
# Sentiment Prediction Function
|
| 22 |
+
# ----------------------------
|
| 23 |
+
def predict_sentiment(text):
|
| 24 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 27 |
+
prediction = torch.argmax(probs).item()
|
| 28 |
+
return labels[prediction]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ----------------------------
|
| 32 |
+
# Main Processing Function
|
| 33 |
+
# ----------------------------
|
| 34 |
+
def process_file(file):
|
| 35 |
+
df = pd.read_csv(file.name)
|
| 36 |
+
|
| 37 |
+
if "text" not in df.columns:
|
| 38 |
+
return "CSV must contain a 'text' column.", None
|
| 39 |
+
|
| 40 |
+
df["predicted_sentiment"] = df["text"].astype(str).apply(predict_sentiment)
|
| 41 |
+
|
| 42 |
+
# Optional evaluation if ground truth exists
|
| 43 |
+
f1 = None
|
| 44 |
+
if "sentiment" in df.columns:
|
| 45 |
+
f1 = f1_score(
|
| 46 |
+
df["sentiment"],
|
| 47 |
+
df["predicted_sentiment"],
|
| 48 |
+
average="weighted"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
output_path = "results.csv"
|
| 52 |
+
df.to_csv(output_path, index=False)
|
| 53 |
+
|
| 54 |
+
if f1:
|
| 55 |
+
return f"Processing complete! F1 Score: {round(f1,4)}", output_path
|
| 56 |
+
else:
|
| 57 |
+
return "Processing complete! No ground truth column found.", output_path
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# ----------------------------
|
| 61 |
+
# Gradio Interface
|
| 62 |
+
# ----------------------------
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=process_file,
|
| 65 |
+
inputs=gr.File(label="Upload CSV File"),
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.Textbox(label="Status"),
|
| 68 |
+
gr.File(label="Download Results")
|
| 69 |
+
],
|
| 70 |
+
title="Aspect-Based Sentiment Analysis App",
|
| 71 |
+
description="Upload a CSV file with a 'text' column. Optionally include a 'sentiment' column for evaluation."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
interface.launch()
|