Samanta Das commited on
Commit
bc62e6b
·
verified ·
1 Parent(s): 4e3b63a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -1,10 +1,14 @@
1
  import gradio as gr
2
  from PIL import Image, ImageDraw
3
  import os
 
4
  import time
5
  import threading
6
  import logging
7
 
 
 
 
8
  # Set up logging configuration
9
  logging.basicConfig(level=logging.INFO)
10
 
@@ -14,13 +18,16 @@ from llama import generate_response_based_on_yolo
14
 
15
  # Define model path and output folder relative to the app's directory
16
  MODEL_PATH = 'yolov8n_custom.pkl' # Update this to the relative path of your model on Hugging Face Spaces
17
- OUTPUT_FOLDER = './output_images' # Use a relative path to avoid permission issues
18
 
19
  # Ensure the output folder exists
20
  os.makedirs(OUTPUT_FOLDER, exist_ok=True)
21
 
22
- # Initialize the fracture detector with both model path and output folder
23
- detector = FractureDetector(MODEL_PATH, OUTPUT_FOLDER)
 
 
 
24
 
25
  def delete_file_after_delay(file_path, delay):
26
  """Delete the specified file after a given delay."""
@@ -45,28 +52,18 @@ def mark_fracture_area(image, detections):
45
 
46
  def analyze_image(input_image):
47
  """Analyze the uploaded image for fractures and generate a report."""
48
- # Save the uploaded image to a temporary location
49
  temp_image_path = os.path.join(OUTPUT_FOLDER, 'temp_uploaded_image.jpg')
50
  input_image.save(temp_image_path)
51
 
52
  try:
53
- # Perform fracture detection
54
  detections = detector.detect_fractures(temp_image_path, conf_threshold=0.25)
55
-
56
- # Mark the fracture areas on the image
57
  marked_image = mark_fracture_area(input_image.copy(), detections)
58
-
59
- # Generate analysis report
60
  analysis_report = generate_response_based_on_yolo(detections)
61
-
62
- # Schedule deletion of the temporary file after 2 minutes
63
  delete_file_after_delay(temp_image_path, 120)
64
-
65
  return marked_image, analysis_report
66
-
67
  except Exception as e:
68
  logging.error(f"An error occurred during analysis: {str(e)}")
69
- return input_image, f"An error occurred during analysis: {str(e)}"
70
 
71
  # Define the Gradio interface
72
  iface = gr.Interface(
 
1
  import gradio as gr
2
  from PIL import Image, ImageDraw
3
  import os
4
+ import sys
5
  import time
6
  import threading
7
  import logging
8
 
9
+ # Add the backend app to the system path for imports
10
+ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
11
+
12
  # Set up logging configuration
13
  logging.basicConfig(level=logging.INFO)
14
 
 
18
 
19
  # Define model path and output folder relative to the app's directory
20
  MODEL_PATH = 'yolov8n_custom.pkl' # Update this to the relative path of your model on Hugging Face Spaces
21
+ OUTPUT_FOLDER = './output_images' # Use a relative path
22
 
23
  # Ensure the output folder exists
24
  os.makedirs(OUTPUT_FOLDER, exist_ok=True)
25
 
26
+ # Initialize the fracture detector
27
+ try:
28
+ detector = FractureDetector(MODEL_PATH, OUTPUT_FOLDER)
29
+ except Exception as e:
30
+ logging.error(f"Failed to initialize FractureDetector: {str(e)}")
31
 
32
  def delete_file_after_delay(file_path, delay):
33
  """Delete the specified file after a given delay."""
 
52
 
53
  def analyze_image(input_image):
54
  """Analyze the uploaded image for fractures and generate a report."""
 
55
  temp_image_path = os.path.join(OUTPUT_FOLDER, 'temp_uploaded_image.jpg')
56
  input_image.save(temp_image_path)
57
 
58
  try:
 
59
  detections = detector.detect_fractures(temp_image_path, conf_threshold=0.25)
 
 
60
  marked_image = mark_fracture_area(input_image.copy(), detections)
 
 
61
  analysis_report = generate_response_based_on_yolo(detections)
 
 
62
  delete_file_after_delay(temp_image_path, 120)
 
63
  return marked_image, analysis_report
 
64
  except Exception as e:
65
  logging.error(f"An error occurred during analysis: {str(e)}")
66
+ return input_image, f"An error occurred: {str(e)}"
67
 
68
  # Define the Gradio interface
69
  iface = gr.Interface(