Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,16 @@ from model import create_effnetb2_model
|
|
5 |
from timeit import default_timer as timer
|
6 |
from typing import Tuple, Dict
|
7 |
import pkg_resources
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Check Gradio version
|
10 |
try:
|
11 |
gradio_version = pkg_resources.get_distribution("gradio").version
|
12 |
-
|
13 |
except pkg_resources.DistributionNotFound:
|
14 |
raise ImportError("Gradio is not installed. Please install it using 'pip install gradio'.")
|
15 |
|
@@ -17,13 +22,17 @@ except pkg_resources.DistributionNotFound:
|
|
17 |
try:
|
18 |
with open("class_names.txt", "r") as f:
|
19 |
class_names = [food_name.strip() for food_name in f.readlines()]
|
|
|
20 |
except FileNotFoundError:
|
|
|
21 |
raise FileNotFoundError("class_names.txt not found.")
|
22 |
|
23 |
# Model and transforms preparation
|
24 |
try:
|
25 |
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=101)
|
|
|
26 |
except Exception as e:
|
|
|
27 |
raise Exception(f"Error creating model: {str(e)}")
|
28 |
|
29 |
# Load weights
|
@@ -34,9 +43,12 @@ try:
|
|
34 |
map_location=torch.device("cpu"),
|
35 |
)
|
36 |
)
|
|
|
37 |
except FileNotFoundError:
|
|
|
38 |
raise FileNotFoundError("Model weights file not found.")
|
39 |
except Exception as e:
|
|
|
40 |
raise Exception(f"Error loading weights: {str(e)}")
|
41 |
|
42 |
# Predict function
|
@@ -51,8 +63,10 @@ def predict(img) -> Tuple[Dict, float]:
|
|
51 |
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
52 |
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
53 |
pred_time = round(timer() - start_time, 5)
|
|
|
54 |
return pred_labels_and_probs, pred_time
|
55 |
except Exception as e:
|
|
|
56 |
return {"error": f"Prediction failed: {str(e)}"}, 0.0
|
57 |
|
58 |
# Gradio app
|
@@ -61,9 +75,10 @@ description = "An EfficientNetB2 feature extractor to classify 101 food classes.
|
|
61 |
|
62 |
try:
|
63 |
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
|
|
64 |
except FileNotFoundError:
|
65 |
example_list = []
|
66 |
-
|
67 |
|
68 |
# Simplified Gradio interface
|
69 |
demo = gr.Interface(
|
@@ -76,10 +91,14 @@ demo = gr.Interface(
|
|
76 |
examples=example_list,
|
77 |
title=title,
|
78 |
description=description,
|
|
|
|
|
79 |
)
|
80 |
|
81 |
# Launch with share=True for Hugging Face Spaces
|
82 |
try:
|
83 |
demo.launch(share=True)
|
|
|
84 |
except Exception as e:
|
|
|
85 |
raise Exception(f"Failed to launch Gradio app: {str(e)}")
|
|
|
5 |
from timeit import default_timer as timer
|
6 |
from typing import Tuple, Dict
|
7 |
import pkg_resources
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
|
14 |
# Check Gradio version
|
15 |
try:
|
16 |
gradio_version = pkg_resources.get_distribution("gradio").version
|
17 |
+
logger.info(f"Using Gradio version: {gradio_version}")
|
18 |
except pkg_resources.DistributionNotFound:
|
19 |
raise ImportError("Gradio is not installed. Please install it using 'pip install gradio'.")
|
20 |
|
|
|
22 |
try:
|
23 |
with open("class_names.txt", "r") as f:
|
24 |
class_names = [food_name.strip() for food_name in f.readlines()]
|
25 |
+
logger.info("Class names loaded successfully")
|
26 |
except FileNotFoundError:
|
27 |
+
logger.error("class_names.txt not found")
|
28 |
raise FileNotFoundError("class_names.txt not found.")
|
29 |
|
30 |
# Model and transforms preparation
|
31 |
try:
|
32 |
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=101)
|
33 |
+
logger.info("EfficientNetB2 model created successfully")
|
34 |
except Exception as e:
|
35 |
+
logger.error(f"Error creating model: {str(e)}")
|
36 |
raise Exception(f"Error creating model: {str(e)}")
|
37 |
|
38 |
# Load weights
|
|
|
43 |
map_location=torch.device("cpu"),
|
44 |
)
|
45 |
)
|
46 |
+
logger.info("Model weights loaded successfully")
|
47 |
except FileNotFoundError:
|
48 |
+
logger.error("Model weights file not found")
|
49 |
raise FileNotFoundError("Model weights file not found.")
|
50 |
except Exception as e:
|
51 |
+
logger.error(f"Error loading weights: {str(e)}")
|
52 |
raise Exception(f"Error loading weights: {str(e)}")
|
53 |
|
54 |
# Predict function
|
|
|
63 |
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
64 |
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
65 |
pred_time = round(timer() - start_time, 5)
|
66 |
+
logger.info(f"Prediction completed: {pred_labels_and_probs}, Time: {pred_time}")
|
67 |
return pred_labels_and_probs, pred_time
|
68 |
except Exception as e:
|
69 |
+
logger.error(f"Prediction failed: {str(e)}")
|
70 |
return {"error": f"Prediction failed: {str(e)}"}, 0.0
|
71 |
|
72 |
# Gradio app
|
|
|
75 |
|
76 |
try:
|
77 |
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
78 |
+
logger.info("Examples loaded successfully")
|
79 |
except FileNotFoundError:
|
80 |
example_list = []
|
81 |
+
logger.warning("'examples/' directory not found")
|
82 |
|
83 |
# Simplified Gradio interface
|
84 |
demo = gr.Interface(
|
|
|
91 |
examples=example_list,
|
92 |
title=title,
|
93 |
description=description,
|
94 |
+
allow_flagging="never", # Disable flagging to simplify API
|
95 |
+
api_mode=False, # Disable API mode to avoid schema generation
|
96 |
)
|
97 |
|
98 |
# Launch with share=True for Hugging Face Spaces
|
99 |
try:
|
100 |
demo.launch(share=True)
|
101 |
+
logger.info("Gradio app launched successfully")
|
102 |
except Exception as e:
|
103 |
+
logger.error(f"Failed to launch Gradio app: {str(e)}")
|
104 |
raise Exception(f"Failed to launch Gradio app: {str(e)}")
|