Spaces:
Sleeping
Sleeping
| from transformers import pipeline | |
| import pandas as pd | |
| import gradio as gr | |
| # Initialize the sentiment analysis pipeline | |
| sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", framework="pt") | |
| def analyze_csv(file_path): | |
| # Read the CSV file | |
| df = pd.read_csv(file_path) | |
| # Ensure the CSV has a 'text' column | |
| if 'text' not in df.columns: | |
| return "Error: CSV must contain a 'text' column." | |
| # Apply sentiment analysis on each text entry | |
| results = df['text'].apply(lambda x: sentiment_pipeline(x)[0]) | |
| df['sentiment'] = results.apply(lambda r: r['label']) | |
| df['score'] = results.apply(lambda r: r['score']) | |
| # Save output to a new CSV file | |
| output_csv_path = "output.csv" | |
| df.to_csv(output_csv_path, index=False) | |
| return output_csv_path # Return path to the new CSV | |
| # Define the Gradio interface | |
| iface = gr.Interface( | |
| fn=analyze_csv, | |
| inputs=gr.File(label="Upload CSV File", file_count="single", type="filepath"), | |
| outputs=gr.File(label="Download CSV File"), | |
| title="CSV Sentiment Analysis App", | |
| description="Upload a CSV file with a 'text' column. The app will run sentiment analysis on each row and return a downloadable CSV with sentiment labels and scores." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |