ftx7go commited on
Commit
434ed8b
·
verified ·
1 Parent(s): e1eb8aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -3,7 +3,8 @@ import smtplib
3
  import ssl
4
  from email.message import EmailMessage
5
 
6
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force TensorFlow to use CPU
 
7
 
8
  import gradio as gr
9
  import tensorflow as tf
@@ -12,16 +13,14 @@ from tensorflow.keras.preprocessing import image
12
  from PIL import Image
13
  from reportlab.lib.pagesizes import letter
14
  from reportlab.pdfgen import canvas
15
- from reportlab.lib import colors
16
- from reportlab.platypus import Table, TableStyle
17
 
18
  # Load the trained model
19
  model = tf.keras.models.load_model("my_keras_model.h5")
20
 
21
- # Store generated report file path
22
  report_paths = {}
23
 
24
- # Function to send email
25
  def send_email(patient_email, patient_name):
26
  if patient_name not in report_paths:
27
  return "Error: Generate the report first before sending."
@@ -77,15 +76,7 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
77
 
78
  # Predict fracture
79
  prediction = predict_fracture(xray)
80
- diagnosed_class = "normal" if prediction > 0.5 else "Fractured"
81
-
82
- # Injury severity classification
83
- severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
84
-
85
- # Save X-ray image for report
86
- img = Image.open(xray).resize((300, 300))
87
- img_path = f"{name}_xray.png"
88
- img.save(img_path)
89
 
90
  # Generate PDF report
91
  report_path = f"{name}_fracture_report.pdf"
@@ -96,11 +87,16 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
96
 
97
  c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
98
 
 
 
 
 
 
99
  c.drawInlineImage(img_path, 50, 320, width=250, height=250)
100
 
101
  c.save()
102
 
103
- # Store the file path
104
  report_paths[name] = report_path
105
 
106
  return report_path # Return file path
@@ -118,24 +114,29 @@ with gr.Blocks() as app:
118
  weight = gr.Number(label="Weight (kg)")
119
  height = gr.Number(label="Height (cm)")
120
 
 
 
 
 
121
  with gr.Row():
122
  email = gr.Textbox(label="Patient Email", type="email")
123
 
 
 
124
  with gr.Row():
125
- xray = gr.Image(type="filepath", label="Upload X-ray Image")
 
126
 
127
- submit_button = gr.Button("Generate Report")
128
- send_email_button = gr.Button("Send Report via Email")
129
  output_file = gr.File(label="Download Report")
130
 
131
- # When clicking "Generate Report", save the file path and allow downloading
132
  submit_button.click(
133
  generate_report,
134
  inputs=[name, age, gender, weight, height, allergies, cause, xray],
135
  outputs=[output_file]
136
  )
137
 
138
- # When clicking "Send Report via Email", send the stored report file
139
  send_email_button.click(
140
  send_email,
141
  inputs=[email, name],
 
3
  import ssl
4
  from email.message import EmailMessage
5
 
6
+ # Force TensorFlow to use CPU to prevent CUDA errors
7
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
8
 
9
  import gradio as gr
10
  import tensorflow as tf
 
13
  from PIL import Image
14
  from reportlab.lib.pagesizes import letter
15
  from reportlab.pdfgen import canvas
 
 
16
 
17
  # Load the trained model
18
  model = tf.keras.models.load_model("my_keras_model.h5")
19
 
20
+ # Store generated report file paths
21
  report_paths = {}
22
 
23
+ # Function to send email with the report
24
  def send_email(patient_email, patient_name):
25
  if patient_name not in report_paths:
26
  return "Error: Generate the report first before sending."
 
76
 
77
  # Predict fracture
78
  prediction = predict_fracture(xray)
79
+ diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
 
 
 
 
 
 
 
 
80
 
81
  # Generate PDF report
82
  report_path = f"{name}_fracture_report.pdf"
 
87
 
88
  c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
89
 
90
+ # Save X-ray image for report
91
+ img = Image.open(xray).resize((300, 300))
92
+ img_path = f"{name}_xray.png"
93
+ img.save(img_path)
94
+
95
  c.drawInlineImage(img_path, 50, 320, width=250, height=250)
96
 
97
  c.save()
98
 
99
+ # Store file path for later use
100
  report_paths[name] = report_path
101
 
102
  return report_path # Return file path
 
114
  weight = gr.Number(label="Weight (kg)")
115
  height = gr.Number(label="Height (cm)")
116
 
117
+ with gr.Row():
118
+ allergies = gr.Textbox(label="Allergies")
119
+ cause = gr.Textbox(label="Cause of Injury")
120
+
121
  with gr.Row():
122
  email = gr.Textbox(label="Patient Email", type="email")
123
 
124
+ xray = gr.Image(type="filepath", label="Upload X-ray Image")
125
+
126
  with gr.Row():
127
+ submit_button = gr.Button("Generate Report")
128
+ send_email_button = gr.Button("Send Report via Email")
129
 
 
 
130
  output_file = gr.File(label="Download Report")
131
 
132
+ # Generate Report Button
133
  submit_button.click(
134
  generate_report,
135
  inputs=[name, age, gender, weight, height, allergies, cause, xray],
136
  outputs=[output_file]
137
  )
138
 
139
+ # Send Email Button
140
  send_email_button.click(
141
  send_email,
142
  inputs=[email, name],