Spaces:
Sleeping
Sleeping
File size: 2,166 Bytes
49b5633 eb43fb7 49b5633 eb43fb7 49b5633 | 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 | import gradio as gr
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from sklearn.metrics import f1_score
import numpy as np
import os
# ----------------------------
# Load Hugging Face Model
# ----------------------------
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
labels = ["negative", "neutral", "positive"]
# ----------------------------
# Sentiment Prediction Function
# ----------------------------
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True)
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
prediction = torch.argmax(probs).item()
return labels[prediction]
# ----------------------------
# Main Processing Function
# ----------------------------
def process_file(file):
df = pd.read_csv(file.name)
if "Review" not in df.columns:
return "CSV must contain a 'text' column.", None
df["predicted_sentiment"] = df["Review"].astype(str).apply(predict_sentiment)
# Optional evaluation if ground truth exists
f1 = None
if "sentiment" in df.columns:
f1 = f1_score(
df["sentiment"],
df["predicted_sentiment"],
average="weighted"
)
output_path = "results.csv"
df.to_csv(output_path, index=False)
if f1:
return f"Processing complete! F1 Score: {round(f1,4)}", output_path
else:
return "Processing complete! No ground truth column found.", output_path
# ----------------------------
# Gradio Interface
# ----------------------------
interface = gr.Interface(
fn=process_file,
inputs=gr.File(label="Upload CSV File"),
outputs=[
gr.Textbox(label="Status"),
gr.File(label="Download Results")
],
title="Aspect-Based Sentiment Analysis App",
description="Upload a CSV file with a 'text' column. Optionally include a 'sentiment' column for evaluation."
)
if __name__ == "__main__":
interface.launch() |