import gradio as gr import pandas as pd from transformers import pipeline # Configurar el clasificador de sentimientos multilingüe classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli") # Función para analizar los sentimientos de una lista de textos def analyze_sentiments(texts): if not texts: return 0.0, 0.0, 0.0 # Manejar el caso donde no hay textos para analizar positive, negative, neutral = 0, 0, 0 for text in texts: results = classifier(text, ["positive", "negative", "neutral"], multi_label=True) mx = max(results['scores']) ind = results['scores'].index(mx) result = results['labels'][ind] if result == "positive": positive += 1 elif result == "negative": negative += 1 else: neutral += 1 total = len(texts) positive_percent = (positive / total) * 100 negative_percent = (negative / total) * 100 neutral_percent = (neutral / total) * 100 return positive_percent, negative_percent, neutral_percent # Función para cargar el archivo CSV y analizar los primeros 100 comentarios def analyze_sentiment_from_csv(): try: # Intentar leer el archivo CSV con diferentes opciones para manejar errores de formato df = pd.read_csv('SHAREit_BD.csv', delimiter=',') print(df.head()) # Imprimir las primeras filas del DataFrame para verificar su contenido if 'content' not in df.columns: raise ValueError("El archivo CSV no contiene una columna 'content'") texts = df['content'].head(100).tolist() # Tomar solo los primeros 100 comentarios return analyze_sentiments(texts) except pd.errors.ParserError as e: print(f"Error al analizar el archivo CSV: {e}") return "Error al analizar el archivo CSV", "", "" except Exception as e: print(f"Error inesperado: {e}") return str(e), "", "" # Configurar la interfaz de Gradio demo = gr.Interface( fn=analyze_sentiment_from_csv, inputs=[], # No hay necesidad de entradas porque los datos están incrustados outputs=[gr.Textbox(label="Positive Percentage"), gr.Textbox(label="Negative Percentage"), gr.Textbox(label="Neutral Percentage")], # Mostrar los porcentajes como salidas title="Analizador de Sentimientos Multilingüe", description="Porcentaje de comentarios positivos, negativos y neutrales" ) demo.launch()