Spaces:
Runtime error
Runtime error
| import transformers | |
| from flask import Flask, request, jsonify | |
| from transformers import RobertaTokenizerFast, TFRobertaForSequenceClassification, pipeline | |
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import io | |
| from io import BytesIO # Import BytesIO for image generation | |
| # Load model and tokenizer | |
| tokenizer = RobertaTokenizerFast.from_pretrained("arpanghoshal/EmoRoBERTa") | |
| model = TFRobertaForSequenceClassification.from_pretrained("arpanghoshal/EmoROBERTa") | |
| emotion = pipeline("sentiment-analysis", model="arpanghoshal/EmoROBERTa") | |
| def analyze_csv(file): | |
| try: | |
| # Print file content for debugging | |
| file_content = file.read() | |
| print("File content:", file_content) | |
| # Reset file position to the beginning | |
| file.seek(0) | |
| # Read the CSV file into a DataFrame | |
| df = pd.read_csv(io.BytesIO(file_content))`` | |
| print("DataFrame shape:", df.shape) # Print DataFrame shape for debugging | |
| print("DataFrame columns:", df.columns) # Print DataFrame columns for debugging | |
| # Check if the DataFrame is empty | |
| if df.empty: | |
| return "Empty file. Please upload a CSV file with data.", None | |
| # Check if the expected column "phrase" is present in the DataFrame | |
| if "phrase" not in df.columns: | |
| return "Column 'phrase' not found in the CSV file. Please check the file format.", None | |
| phrases = df["phrase"] | |
| # Analyze sentiment for each phrase | |
| emotion_labels = emotion(phrases) | |
| # Create summary statistics | |
| summary_df = pd.DataFrame(emotion_labels).describe() | |
| # Create a bar chart of emotion distribution | |
| plt.figure() | |
| emotion_counts = emotion_labels.get("labels").value_counts() | |
| emotion_counts.plot(kind="bar") | |
| plt.title("Emotion Distribution") | |
| plt.xlabel("Emotion") | |
| plt.ylabel("Count") | |
| # Generate PNG image of the chart | |
| chart_img = BytesIO() | |
| plt.savefig(chart_img, format="png") | |
| chart_img.seek(0) | |
| return summary_df.to_json(), chart_img.read() | |
| except Exception as e: | |
| error_message = f"Error processing the CSV file: {str(e)}" | |
| print(error_message) # Print the error message for debugging | |
| return error_message, None | |
| iface = gr.Interface( | |
| fn=analyze_csv, | |
| inputs=[gr.File(label="Upload CSV File")], | |
| outputs=["dataframe", "image"], | |
| title="Emotion Analyzer with CSV", | |
| description="Analyzes sentiment and creates charts/tables from a CSV file.", | |
| ) | |
| iface.launch() |