natfil commited on
Commit
1d9876d
1 Parent(s): 053e1e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ # Use a pipeline as a high-level helper
7
+ from transformers import pipeline
8
+
9
+ #analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
10
+ analyzer = pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student")
11
+
12
+ def sentiment_analyzer(review):
13
+ sentiment = analyzer(review)
14
+ return sentiment[0]['label']
15
+
16
+ def sentiment_bar_chart(df):
17
+ sentiment_counts = df['Sentiment'].value_counts()
18
+
19
+ # Create a bar chart
20
+ fig, ax = plt.subplots()
21
+ sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
22
+ ax.set_title('Reviews')
23
+ ax.set_xlabel('Stimmung')
24
+ ax.set_ylabel('Anzahl')
25
+ # ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
26
+
27
+ # Return the figure object
28
+ return fig
29
+
30
+
31
+ def read_reviews_and_analyze_sentiment(file_object):
32
+ # Load the Excel file into a DataFrame
33
+ df = pd.read_excel(file_object)
34
+ print(df.columns)
35
+ # Check if 'Review' column is in the DataFrame
36
+ #for col in df.columns:
37
+ #print(f"col={col}")
38
+ if 'Reviews' not in df.columns:
39
+ raise ValueError("Die Excel-Datei muss eine Spalte 'Reviews' enthalten.")
40
+
41
+ # Apply the get_sentiment function to each review in the DataFrame
42
+ df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
43
+ chart_object = sentiment_bar_chart(df)
44
+ return chart_object, df
45
+
46
+ demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
47
+ inputs=[gr.File(file_types=["xlsx"], label="Laden Sie Ihre xls-Review-Datei hoch")],
48
+ outputs=[ gr.Plot(label="Stimmungsanalyse"), gr.Dataframe(label="Stimmungen")],
49
+ title="Project 3: Stimmung-Analysator",
50
+ description="DIESE ANWENDUNG WIRD VERWENDET, UM DIE STIMMUNG AUF DER GRUNDLAGE DER HOCHGELADENEN DATEI ZU ANALYSIEREN.",
51
+ allow_flagging="never",
52
+ submit_btn="Übermitteln",
53
+ clear_btn="Bereinigen",)
54
+ demo.launch()