natfil's picture
Create app.py
1d9876d verified
import torch
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
# Use a pipeline as a high-level helper
from transformers import pipeline
#analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
analyzer = pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student")
def sentiment_analyzer(review):
sentiment = analyzer(review)
return sentiment[0]['label']
def sentiment_bar_chart(df):
sentiment_counts = df['Sentiment'].value_counts()
# Create a bar chart
fig, ax = plt.subplots()
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
ax.set_title('Reviews')
ax.set_xlabel('Stimmung')
ax.set_ylabel('Anzahl')
# ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
# Return the figure object
return fig
def read_reviews_and_analyze_sentiment(file_object):
# Load the Excel file into a DataFrame
df = pd.read_excel(file_object)
print(df.columns)
# Check if 'Review' column is in the DataFrame
#for col in df.columns:
#print(f"col={col}")
if 'Reviews' not in df.columns:
raise ValueError("Die Excel-Datei muss eine Spalte 'Reviews' enthalten.")
# Apply the get_sentiment function to each review in the DataFrame
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
chart_object = sentiment_bar_chart(df)
return chart_object, df
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
inputs=[gr.File(file_types=["xlsx"], label="Laden Sie Ihre xls-Review-Datei hoch")],
outputs=[ gr.Plot(label="Stimmungsanalyse"), gr.Dataframe(label="Stimmungen")],
title="Project 3: Stimmung-Analysator",
description="DIESE ANWENDUNG WIRD VERWENDET, UM DIE STIMMUNG AUF DER GRUNDLAGE DER HOCHGELADENEN DATEI ZU ANALYSIEREN.",
allow_flagging="never",
submit_btn="Übermitteln",
clear_btn="Bereinigen",)
demo.launch()