Spaces:
Sleeping
Sleeping
Commit
·
43c6927
1
Parent(s):
f0d08f0
fixing the app.py script
Browse files- scripts/app.py +18 -15
scripts/app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import torch
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
| 6 |
|
|
@@ -23,10 +24,9 @@ except ImportError:
|
|
| 23 |
class FineTunedSentimentClassifier: pass
|
| 24 |
|
| 25 |
# --- Configuration ---
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
SENTIMENT_CHECKPOINT_PATH = "checkpoints/sentiment-binary-best-checkpoint.ckpt" # <-- CHANGE THIS
|
| 30 |
|
| 31 |
# --- Pre-defined Aspect Dictionaries for Different Product Categories ---
|
| 32 |
ASPECT_DICTIONARIES = {
|
|
@@ -37,7 +37,7 @@ ASPECT_DICTIONARIES = {
|
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
-
# ---
|
| 41 |
print("--- Initializing all models for the Gradio App ---")
|
| 42 |
sentiment_classifier, summarizer, aspect_analyzer, aspect_extractor = None, None, None, None
|
| 43 |
try:
|
|
@@ -45,21 +45,22 @@ try:
|
|
| 45 |
aspect_analyzer = AspectAnalyzer(force_cpu=True)
|
| 46 |
aspect_extractor = AspectExtractor(force_cpu=True)
|
| 47 |
|
| 48 |
-
if
|
| 49 |
-
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 50 |
-
print("!!! WARNING: Sentiment checkpoint path not found or not set. !!!")
|
| 51 |
-
print(f"!!! Please update the 'SENTIMENT_CHECKPOINT_PATH' variable in app.py")
|
| 52 |
-
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 53 |
-
else:
|
| 54 |
sentiment_classifier = FineTunedSentimentClassifier(
|
| 55 |
checkpoint_path=SENTIMENT_CHECKPOINT_PATH, force_cpu=True
|
| 56 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
print("\n--- All models loaded successfully ---\n")
|
| 58 |
except Exception as e:
|
| 59 |
print(f"An error occurred during model initialization: {e}")
|
| 60 |
|
| 61 |
|
| 62 |
-
# ---
|
| 63 |
def analyze_review(review_text, product_category):
|
| 64 |
if not review_text:
|
| 65 |
return {"ERROR": "Please enter a review."}, "", None
|
|
@@ -71,7 +72,9 @@ def analyze_review(review_text, product_category):
|
|
| 71 |
sentiment_result['label']: f"{sentiment_result['score']:.2f}"
|
| 72 |
}
|
| 73 |
else:
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# --- b. Review Summarization ---
|
| 77 |
if summarizer:
|
|
@@ -95,7 +98,7 @@ def analyze_review(review_text, product_category):
|
|
| 95 |
return sentiment_output, summary_output, aspect_df
|
| 96 |
|
| 97 |
|
| 98 |
-
# ---
|
| 99 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 100 |
gr.Markdown("# 🛍️ ReviewSense: Product Review Analysis Engine")
|
| 101 |
gr.Markdown(
|
|
@@ -154,7 +157,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 154 |
)
|
| 155 |
|
| 156 |
|
| 157 |
-
# ---
|
| 158 |
if __name__ == "__main__":
|
| 159 |
print("Launching Gradio App...")
|
| 160 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
import pandas as pd
|
| 6 |
import re
|
| 7 |
|
|
|
|
| 24 |
class FineTunedSentimentClassifier: pass
|
| 25 |
|
| 26 |
# --- Configuration ---
|
| 27 |
+
# This should be the relative path to your checkpoint file within the repository.
|
| 28 |
+
SENTIMENT_CHECKPOINT_PATH = "checkpoints/sentiment-binary-best-checkpoint.ckpt"
|
| 29 |
+
|
|
|
|
| 30 |
|
| 31 |
# --- Pre-defined Aspect Dictionaries for Different Product Categories ---
|
| 32 |
ASPECT_DICTIONARIES = {
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
+
# --- Load All Models (Global Objects) ---
|
| 41 |
print("--- Initializing all models for the Gradio App ---")
|
| 42 |
sentiment_classifier, summarizer, aspect_analyzer, aspect_extractor = None, None, None, None
|
| 43 |
try:
|
|
|
|
| 45 |
aspect_analyzer = AspectAnalyzer(force_cpu=True)
|
| 46 |
aspect_extractor = AspectExtractor(force_cpu=True)
|
| 47 |
|
| 48 |
+
if os.path.exists(SENTIMENT_CHECKPOINT_PATH):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
sentiment_classifier = FineTunedSentimentClassifier(
|
| 50 |
checkpoint_path=SENTIMENT_CHECKPOINT_PATH, force_cpu=True
|
| 51 |
)
|
| 52 |
+
else:
|
| 53 |
+
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 54 |
+
print("!!! WARNING: Sentiment checkpoint path not found. !!!")
|
| 55 |
+
print(f"!!! Path checked: '{SENTIMENT_CHECKPOINT_PATH}'")
|
| 56 |
+
print("!!! The fine-tuned sentiment model will NOT be loaded. !!!")
|
| 57 |
+
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 58 |
print("\n--- All models loaded successfully ---\n")
|
| 59 |
except Exception as e:
|
| 60 |
print(f"An error occurred during model initialization: {e}")
|
| 61 |
|
| 62 |
|
| 63 |
+
# --- Define the Core Analysis Function ---
|
| 64 |
def analyze_review(review_text, product_category):
|
| 65 |
if not review_text:
|
| 66 |
return {"ERROR": "Please enter a review."}, "", None
|
|
|
|
| 72 |
sentiment_result['label']: f"{sentiment_result['score']:.2f}"
|
| 73 |
}
|
| 74 |
else:
|
| 75 |
+
# **ROBUST ERROR HANDLING:** This prevents the app from crashing.
|
| 76 |
+
# It returns a dictionary that the Gradio Label component can display.
|
| 77 |
+
sentiment_output = {"Error: Model Not Loaded": 1.0}
|
| 78 |
|
| 79 |
# --- b. Review Summarization ---
|
| 80 |
if summarizer:
|
|
|
|
| 98 |
return sentiment_output, summary_output, aspect_df
|
| 99 |
|
| 100 |
|
| 101 |
+
# --- Build the Gradio Interface ---
|
| 102 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 103 |
gr.Markdown("# 🛍️ ReviewSense: Product Review Analysis Engine")
|
| 104 |
gr.Markdown(
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
|
| 160 |
+
# --- Launch the App ---
|
| 161 |
if __name__ == "__main__":
|
| 162 |
print("Launching Gradio App...")
|
| 163 |
demo.launch()
|