Spaces:
Sleeping
Sleeping
feat: Add error handling to display runtime exceptions in UI
Browse files
app.py
CHANGED
@@ -1,23 +1,21 @@
|
|
1 |
# Import the necessary libraries
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
|
|
4 |
|
5 |
# --- Configuration ---
|
6 |
# Define the models we want to offer in our application.
|
7 |
-
# We are now using a confirmed, publicly available Persian model.
|
8 |
MODELS = {
|
9 |
"English": "distilbert-base-uncased-finetuned-sst-2-english",
|
10 |
"Persian (Farsi)": "m3hrdadfi/albert-fa-base-v2-sentiment-binary",
|
11 |
}
|
12 |
|
13 |
# --- State and Caching ---
|
14 |
-
# Create a dictionary to cache the loaded pipelines.
|
15 |
pipeline_cache = {}
|
16 |
|
17 |
def get_pipeline(model_name):
|
18 |
"""
|
19 |
Loads a sentiment-analysis pipeline for the given model name.
|
20 |
-
Uses a cache to avoid reloading models that are already in memory.
|
21 |
"""
|
22 |
if model_name not in pipeline_cache:
|
23 |
print(f"Loading pipeline for model: {model_name}...")
|
@@ -25,22 +23,30 @@ def get_pipeline(model_name):
|
|
25 |
print("Pipeline loaded successfully.")
|
26 |
return pipeline_cache[model_name]
|
27 |
|
28 |
-
# --- Core Logic ---
|
29 |
def analyze_sentiment(text, model_choice):
|
30 |
"""
|
31 |
Analyzes the sentiment of a given text using the selected model.
|
|
|
32 |
"""
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# --- Gradio Interface ---
|
46 |
with gr.Blocks() as iface:
|
@@ -53,7 +59,7 @@ with gr.Blocks() as iface:
|
|
53 |
value="English",
|
54 |
label="Select Language Model"
|
55 |
)
|
56 |
-
output_text = gr.Textbox(label="Result", interactive=False)
|
57 |
|
58 |
input_text = gr.Textbox(lines=5, placeholder="Enter text here...")
|
59 |
submit_button = gr.Button("Analyze Sentiment")
|
|
|
1 |
# Import the necessary libraries
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
+
import traceback # Import the traceback module to get detailed errors
|
5 |
|
6 |
# --- Configuration ---
|
7 |
# Define the models we want to offer in our application.
|
|
|
8 |
MODELS = {
|
9 |
"English": "distilbert-base-uncased-finetuned-sst-2-english",
|
10 |
"Persian (Farsi)": "m3hrdadfi/albert-fa-base-v2-sentiment-binary",
|
11 |
}
|
12 |
|
13 |
# --- State and Caching ---
|
|
|
14 |
pipeline_cache = {}
|
15 |
|
16 |
def get_pipeline(model_name):
|
17 |
"""
|
18 |
Loads a sentiment-analysis pipeline for the given model name.
|
|
|
19 |
"""
|
20 |
if model_name not in pipeline_cache:
|
21 |
print(f"Loading pipeline for model: {model_name}...")
|
|
|
23 |
print("Pipeline loaded successfully.")
|
24 |
return pipeline_cache[model_name]
|
25 |
|
26 |
+
# --- Core Logic with Error Handling ---
|
27 |
def analyze_sentiment(text, model_choice):
|
28 |
"""
|
29 |
Analyzes the sentiment of a given text using the selected model.
|
30 |
+
Includes a try-except block to catch and display any errors.
|
31 |
"""
|
32 |
+
try:
|
33 |
+
if not text:
|
34 |
+
return "Please enter some text to analyze."
|
35 |
+
|
36 |
+
model_name = MODELS[model_choice]
|
37 |
+
sentiment_pipeline = get_pipeline(model_name)
|
38 |
+
|
39 |
+
result = sentiment_pipeline(text)[0]
|
40 |
+
label = result['label']
|
41 |
+
score = result['score']
|
42 |
+
|
43 |
+
return f"Sentiment: {label} (Score: {score:.4f})"
|
44 |
+
except Exception as e:
|
45 |
+
# If any error occurs, format it and return it to the UI.
|
46 |
+
# This is our debugging tool!
|
47 |
+
error_details = traceback.format_exc()
|
48 |
+
print(error_details) # Also print to server logs if we can see them
|
49 |
+
return f"An error occurred:\n\n{error_details}"
|
50 |
|
51 |
# --- Gradio Interface ---
|
52 |
with gr.Blocks() as iface:
|
|
|
59 |
value="English",
|
60 |
label="Select Language Model"
|
61 |
)
|
62 |
+
output_text = gr.Textbox(label="Result", interactive=False, lines=10) # Made the box bigger for errors
|
63 |
|
64 |
input_text = gr.Textbox(lines=5, placeholder="Enter text here...")
|
65 |
submit_button = gr.Button("Analyze Sentiment")
|