Shriharsh commited on
Commit
839ec63
ยท
verified ยท
1 Parent(s): 752ca4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -1,10 +1,17 @@
1
- ### 1. Imports and class names setup ###
2
  import gradio as gr
3
  import os
4
  import torch
5
  from model import create_effnetb2_model
6
  from timeit import default_timer as timer
7
  from typing import Tuple, Dict
 
 
 
 
 
 
 
 
8
 
9
  # Load class names
10
  try:
@@ -13,7 +20,7 @@ try:
13
  except FileNotFoundError:
14
  raise FileNotFoundError("class_names.txt not found.")
15
 
16
- ### 2. Model and transforms preparation ###
17
  try:
18
  effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=101)
19
  except Exception as e:
@@ -32,7 +39,7 @@ except FileNotFoundError:
32
  except Exception as e:
33
  raise Exception(f"Error loading weights: {str(e)}")
34
 
35
- ### 3. Predict function ###
36
  def predict(img) -> Tuple[Dict, float]:
37
  try:
38
  start_time = timer()
@@ -48,7 +55,7 @@ def predict(img) -> Tuple[Dict, float]:
48
  except Exception as e:
49
  return {"error": f"Prediction failed: {str(e)}"}, 0.0
50
 
51
- ### 4. Gradio app ###
52
  title = "FoodVision 101 ๐Ÿ”๐Ÿ‘"
53
  description = "An EfficientNetB2 feature extractor to classify 101 food classes."
54
 
@@ -58,6 +65,7 @@ except FileNotFoundError:
58
  example_list = []
59
  print("Warning: 'examples/' directory not found.")
60
 
 
61
  demo = gr.Interface(
62
  fn=predict,
63
  inputs=gr.Image(type="pil"),
@@ -70,5 +78,8 @@ demo = gr.Interface(
70
  description=description,
71
  )
72
 
73
- # Launch without share=True for Hugging Face Spaces
74
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
  import torch
4
  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
+ print(f"Using Gradio version: {gradio_version}")
13
+ except pkg_resources.DistributionNotFound:
14
+ raise ImportError("Gradio is not installed. Please install it using 'pip install gradio'.")
15
 
16
  # Load class names
17
  try:
 
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:
 
39
  except Exception as e:
40
  raise Exception(f"Error loading weights: {str(e)}")
41
 
42
+ # Predict function
43
  def predict(img) -> Tuple[Dict, float]:
44
  try:
45
  start_time = timer()
 
55
  except Exception as e:
56
  return {"error": f"Prediction failed: {str(e)}"}, 0.0
57
 
58
+ # Gradio app
59
  title = "FoodVision 101 ๐Ÿ”๐Ÿ‘"
60
  description = "An EfficientNetB2 feature extractor to classify 101 food classes."
61
 
 
65
  example_list = []
66
  print("Warning: 'examples/' directory not found.")
67
 
68
+ # Simplified Gradio interface
69
  demo = gr.Interface(
70
  fn=predict,
71
  inputs=gr.Image(type="pil"),
 
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)}")