Samanta Das commited on
Commit
67ea11c
·
verified ·
1 Parent(s): bc62e6b

Update app.py

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