mail2kandan commited on
Commit
5baaad5
·
1 Parent(s): 7d28034

fixing model download path from remote

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -3,32 +3,38 @@ from PIL import Image
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
 
6
 
7
- # Get model path once at startup
8
  def initialize_model_path():
9
  is_huggingface = os.environ.get("SPACE_ID") is not None
10
  if is_huggingface:
11
  REPO_ID = "mail2kandan/guitar_watch_openvino_model"
12
  print("Downloading model from Hugging Face...")
13
 
14
- # Create a model directory
15
- model_dir = 'openvino_model'
16
  os.makedirs(model_dir, exist_ok=True)
17
 
18
- # Download both files
19
- snapshot_download(
20
- repo_id=REPO_ID,
21
- allow_patterns=["*.xml", "*.bin"],
22
- local_dir=model_dir
23
- )
 
 
 
 
 
 
24
 
25
- # Verify files exist
26
  if os.path.exists(os.path.join(model_dir, 'best.xml')) and \
27
  os.path.exists(os.path.join(model_dir, 'best.bin')):
28
- print(f"Model files downloaded to: {model_dir}")
29
  return model_dir
30
  else:
31
- raise Exception("Model files not found after download")
32
  else:
33
  local_path = r'C:\Manikandan\NYP\ITI107\Assignment_draft\output_models\best_openvino_model'
34
  print(f"Using local model from: {local_path}")
@@ -41,12 +47,21 @@ def predict(pilimg, conf_threshold, iou_threshold):
41
  try:
42
  if pilimg is None:
43
  return None
 
 
 
 
 
 
44
  model = YOLO(MODEL_PATH, task='detect')
45
  result = model.predict(pilimg, conf=conf_threshold, iou=iou_threshold)
46
  img_bgr = result[0].plot()
47
  return Image.fromarray(img_bgr[..., ::-1])
48
  except Exception as e:
49
  print(f"Error during prediction: {str(e)}")
 
 
 
50
  return None
51
 
52
  with gr.Blocks() as demo:
@@ -54,7 +69,7 @@ with gr.Blocks() as demo:
54
  gr.HTML("""
55
  <div style="text-align: center; margin-bottom: 20px; padding: 20px; background-color: #f7f7f7;">
56
  <h1 style="font-size: 2.5em; color: #2196F3; margin-bottom: 10px;">Guitar & Watch Detection</h1>
57
- <p style="font-size: 1.2em; color: #666;">Powered by YOLOv8 </p>
58
  </div>
59
  """)
60
 
@@ -142,10 +157,11 @@ with gr.Blocks() as demo:
142
  # Add custom CSS to resize the hero image
143
  demo.css = """
144
  #hero-image img {
145
- width: 560px; /* Adjust the width to make the image smaller */
146
  height: auto;
147
  }
148
  """
149
 
150
  if __name__ == "__main__":
151
- demo.launch(share=True)
 
 
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
+ import shutil
7
 
 
8
  def initialize_model_path():
9
  is_huggingface = os.environ.get("SPACE_ID") is not None
10
  if is_huggingface:
11
  REPO_ID = "mail2kandan/guitar_watch_openvino_model"
12
  print("Downloading model from Hugging Face...")
13
 
14
+ # Create a temp directory for the model
15
+ model_dir = os.path.join(os.getcwd(), 'guitar_openvino_model')
16
  os.makedirs(model_dir, exist_ok=True)
17
 
18
+ # Download complete model directory
19
+ downloaded_path = snapshot_download(repo_id=REPO_ID)
20
+
21
+ # Copy files to our model directory
22
+ for ext in ['.xml', '.bin']:
23
+ src = os.path.join(downloaded_path, f'best{ext}')
24
+ dst = os.path.join(model_dir, f'best{ext}')
25
+ if os.path.exists(src):
26
+ shutil.copy2(src, dst)
27
+ print(f"Copied {src} to {dst}")
28
+ else:
29
+ raise Exception(f"Required model file best{ext} not found in repository")
30
 
31
+ # Verify both files exist in the new directory
32
  if os.path.exists(os.path.join(model_dir, 'best.xml')) and \
33
  os.path.exists(os.path.join(model_dir, 'best.bin')):
34
+ print(f"Using model directory: {model_dir}")
35
  return model_dir
36
  else:
37
+ raise Exception("Model files not found in target directory after copying")
38
  else:
39
  local_path = r'C:\Manikandan\NYP\ITI107\Assignment_draft\output_models\best_openvino_model'
40
  print(f"Using local model from: {local_path}")
 
47
  try:
48
  if pilimg is None:
49
  return None
50
+
51
+ print(f"Loading model from: {MODEL_PATH}")
52
+ print(f"Model path exists: {os.path.exists(MODEL_PATH)}")
53
+ if os.path.isfile(MODEL_PATH):
54
+ print(f"Found these files in directory: {os.listdir(os.path.dirname(MODEL_PATH))}")
55
+
56
  model = YOLO(MODEL_PATH, task='detect')
57
  result = model.predict(pilimg, conf=conf_threshold, iou=iou_threshold)
58
  img_bgr = result[0].plot()
59
  return Image.fromarray(img_bgr[..., ::-1])
60
  except Exception as e:
61
  print(f"Error during prediction: {str(e)}")
62
+ # Print full error traceback for debugging
63
+ import traceback
64
+ print(traceback.format_exc())
65
  return None
66
 
67
  with gr.Blocks() as demo:
 
69
  gr.HTML("""
70
  <div style="text-align: center; margin-bottom: 20px; padding: 20px; background-color: #f7f7f7;">
71
  <h1 style="font-size: 2.5em; color: #2196F3; margin-bottom: 10px;">Guitar & Watch Detection</h1>
72
+ <p style="font-size: 1.2em; color: #666;">Powered by YOLOv8</p>
73
  </div>
74
  """)
75
 
 
157
  # Add custom CSS to resize the hero image
158
  demo.css = """
159
  #hero-image img {
160
+ width: 560px;
161
  height: auto;
162
  }
163
  """
164
 
165
  if __name__ == "__main__":
166
+ is_huggingface = os.environ.get("SPACE_ID") is not None
167
+ demo.launch(share=not is_huggingface)