Spaces:
Runtime error
Runtime error
Update app.py
#2
by
arssite
- opened
app.py
CHANGED
|
@@ -38,15 +38,36 @@ def sentiment_bar_chart(df):
|
|
| 38 |
|
| 39 |
|
| 40 |
def read_reviews_and_analyze_sentiment(file_object):
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Check if 'Review' column is in the DataFrame
|
| 45 |
-
if 'Reviews' not in df.columns:
|
| 46 |
-
raise ValueError("Excel file must contain a 'Review' column.")
|
| 47 |
-
|
| 48 |
-
# Apply the get_sentiment function to each review in the DataFrame
|
| 49 |
-
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 50 |
chart_object = sentiment_bar_chart(df)
|
| 51 |
return df, chart_object
|
| 52 |
|
|
@@ -58,8 +79,8 @@ def read_reviews_and_analyze_sentiment(file_object):
|
|
| 58 |
|
| 59 |
|
| 60 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 61 |
-
inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
|
| 62 |
-
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
|
| 63 |
title="@GenAILearniverse Project 3: Sentiment Analyzer",
|
| 64 |
description="THIS APPLICATION WILL BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLAODED.")
|
| 65 |
demo.launch()
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def read_reviews_and_analyze_sentiment(file_object):
|
| 41 |
+
if file_object.name.endswith('.xlsx'):
|
| 42 |
+
# Load the Excel file into a DataFrame
|
| 43 |
+
df = pd.read_excel(file_object)
|
| 44 |
+
|
| 45 |
+
# Check if 'Review' column is in the DataFrame
|
| 46 |
+
if 'Reviews' not in df.columns:
|
| 47 |
+
raise ValueError("Excel file must contain a 'Review' column.")
|
| 48 |
+
|
| 49 |
+
# Apply the get_sentiment function to each review in the DataFrame
|
| 50 |
+
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 51 |
+
elif file_object.name.endswith('.docx'):
|
| 52 |
+
# Read the content of the DOCX file
|
| 53 |
+
doc = Document(file_object)
|
| 54 |
+
reviews = [para.text for para in doc.paragraphs if para.text.strip()]
|
| 55 |
+
df = pd.DataFrame({'Reviews': reviews})
|
| 56 |
+
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 57 |
+
|
| 58 |
+
elif file_object.name.endswith('.pdf'):
|
| 59 |
+
# Read the content of the PDF file
|
| 60 |
+
reader = PdfReader(file_object)
|
| 61 |
+
text = ""
|
| 62 |
+
for page in reader.pages:
|
| 63 |
+
text += page.extract_text()
|
| 64 |
+
reviews = text.split('\n') # Assuming reviews are newline separated
|
| 65 |
+
df = pd.DataFrame({'Reviews': reviews})
|
| 66 |
+
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
|
| 67 |
+
|
| 68 |
+
else:
|
| 69 |
+
raise ValueError("Unsupported file format. Please upload .xlsx, .pdf, or .docx files.")
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
chart_object = sentiment_bar_chart(df)
|
| 72 |
return df, chart_object
|
| 73 |
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
|
| 82 |
+
inputs=[gr.File(file_types=["xlsx", "pdf", "docx"], label="Upload your review comment file")],
|
| 83 |
+
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis"),gr.Textbox(label="Single Sentence Sentiment Analysis")],
|
| 84 |
title="@GenAILearniverse Project 3: Sentiment Analyzer",
|
| 85 |
description="THIS APPLICATION WILL BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLAODED.")
|
| 86 |
demo.launch()
|