Update app.py
Browse files
app.py
CHANGED
@@ -1,149 +1,18 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
-
import numpy as np
|
4 |
-
from tensorflow.keras.preprocessing import image
|
5 |
-
from PIL import Image, ImageDraw
|
6 |
-
from reportlab.lib.pagesizes import letter
|
7 |
-
from reportlab.pdfgen import canvas
|
8 |
-
import os
|
9 |
|
10 |
-
|
11 |
-
model = tf.keras.models.load_model("my_keras_model.h5")
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
def
|
18 |
-
|
19 |
-
img_array = image.img_to_array(img) / 255.0
|
20 |
-
img_array = np.expand_dims(img_array, axis=0)
|
21 |
|
22 |
-
prediction = model.predict(img_array)[0][0]
|
23 |
-
bbox = (50, 50, 150, 150) if prediction > 0.5 else None # Placeholder
|
24 |
-
return prediction, bbox, img
|
25 |
-
|
26 |
-
# Function to analyze severity
|
27 |
-
def analyze_injury(prediction):
|
28 |
-
if prediction < 0.3:
|
29 |
-
severity = "Mild"
|
30 |
-
treatment = "Rest, pain relievers, and follow-up X-ray."
|
31 |
-
gov_cost = "₹2,000 - ₹5,000"
|
32 |
-
private_cost = "₹10,000 - ₹20,000"
|
33 |
-
elif 0.3 <= prediction < 0.7:
|
34 |
-
severity = "Moderate"
|
35 |
-
treatment = "Plaster cast or splint; possible minor surgery."
|
36 |
-
gov_cost = "₹8,000 - ₹15,000"
|
37 |
-
private_cost = "₹30,000 - ₹60,000"
|
38 |
-
else:
|
39 |
-
severity = "Severe"
|
40 |
-
treatment = "Major surgery with metal implants, extensive physiotherapy."
|
41 |
-
gov_cost = "₹20,000 - ₹50,000"
|
42 |
-
private_cost = "₹1,00,000+"
|
43 |
-
|
44 |
-
return severity, treatment, gov_cost, private_cost
|
45 |
-
|
46 |
-
# Function to generate report
|
47 |
-
def generate_report(name, age, gender, xray1, xray2):
|
48 |
-
prediction1, bbox1, img1 = detect_fracture(xray1)
|
49 |
-
prediction2, bbox2, img2 = detect_fracture(xray2)
|
50 |
-
|
51 |
-
avg_prediction = (prediction1 + prediction2) / 2
|
52 |
-
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
53 |
-
severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
|
54 |
-
|
55 |
-
# Draw bounding box if fracture detected
|
56 |
-
if bbox1:
|
57 |
-
draw = ImageDraw.Draw(img1)
|
58 |
-
draw.rectangle(bbox1, outline="red", width=5)
|
59 |
-
|
60 |
-
if bbox2:
|
61 |
-
draw = ImageDraw.Draw(img2)
|
62 |
-
draw.rectangle(bbox2, outline="red", width=5)
|
63 |
-
|
64 |
-
# Save images
|
65 |
-
img1_path = f"{name}_xray1.png"
|
66 |
-
img2_path = f"{name}_xray2.png"
|
67 |
-
img1.save(img1_path)
|
68 |
-
img2.save(img2_path)
|
69 |
-
|
70 |
-
# Generate PDF report
|
71 |
-
report_path = f"{name}_fracture_report.pdf"
|
72 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
73 |
-
c.setFont("Helvetica", 12)
|
74 |
-
|
75 |
-
c.drawString(100, 750, f"Patient Name: {name}")
|
76 |
-
c.drawString(100, 730, f"Age: {age}")
|
77 |
-
c.drawString(100, 710, f"Gender: {gender}")
|
78 |
-
c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
|
79 |
-
c.drawString(100, 670, f"Injury Severity: {severity}")
|
80 |
-
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
81 |
-
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
82 |
-
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
83 |
-
|
84 |
-
c.save()
|
85 |
-
|
86 |
-
return report_path, img1_path, img2_path, diagnosed_class, severity, treatment, gov_cost, private_cost
|
87 |
-
|
88 |
-
# UI Components
|
89 |
-
with gr.Blocks() as app:
|
90 |
-
gr.Markdown("# 🏥 Bone Fracture Detection & Medical Report")
|
91 |
-
gr.Markdown(
|
92 |
-
"### A radiologist is a doctor who specializes in reading medical images like X-rays, MRIs, and CT scans to diagnose diseases and injuries."
|
93 |
-
)
|
94 |
-
gr.Image("x.jpg", label="X-Ray Example")
|
95 |
-
gr.Markdown(
|
96 |
-
"""
|
97 |
-
## **Understanding Bone Fractures**
|
98 |
-
- **Closed (Simple):** Bone doesn't pierce skin.
|
99 |
-
- **Open (Compound):** Bone breaks skin.
|
100 |
-
- **Hairline:** Small stress fracture.
|
101 |
-
- **Comminuted:** Bone shatters into pieces.
|
102 |
-
- **Avulsion:** Tendon pulls bone fragment.
|
103 |
-
- **Compression:** Bones forced together.
|
104 |
-
"""
|
105 |
-
)
|
106 |
-
gr.Markdown(
|
107 |
-
"""
|
108 |
-
## **First Aid**
|
109 |
-
- Immobilize the injured area.
|
110 |
-
- Control bleeding, cover wounds.
|
111 |
-
- Don't straighten broken bones.
|
112 |
-
- Use splints, slings for support.
|
113 |
-
- Apply cold packs.
|
114 |
-
- Seek emergency help.
|
115 |
-
"""
|
116 |
-
)
|
117 |
-
|
118 |
-
with gr.Row():
|
119 |
-
gr.Markdown("## 📝 Patient Information Form")
|
120 |
-
|
121 |
-
with gr.Column():
|
122 |
-
name = gr.Textbox(label="Patient Name")
|
123 |
-
age = gr.Number(label="Age")
|
124 |
-
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
125 |
-
|
126 |
-
with gr.Column():
|
127 |
-
xray1 = gr.Image(type="file", label="Upload X-ray Image 1")
|
128 |
-
xray2 = gr.Image(type="file", label="Upload X-ray Image 2")
|
129 |
-
|
130 |
-
submit_button = gr.Button("Generate Report")
|
131 |
-
|
132 |
-
output_file = gr.File(label="Download Report")
|
133 |
-
xray1_output = gr.Image(label="X-ray 1 with Fracture Highlight")
|
134 |
-
xray2_output = gr.Image(label="X-ray 2 with Fracture Highlight")
|
135 |
-
diagnosis_output = gr.Textbox(label="Fracture Detected")
|
136 |
-
severity_output = gr.Textbox(label="Injury Severity")
|
137 |
-
treatment_output = gr.Textbox(label="Recommended Treatment")
|
138 |
-
gov_cost_output = gr.Textbox(label="Estimated Cost (Govt Hospital)")
|
139 |
-
private_cost_output = gr.Textbox(label="Estimated Cost (Private Hospital)")
|
140 |
-
|
141 |
-
submit_button.click(
|
142 |
-
generate_report,
|
143 |
-
inputs=[name, age, gender, xray1, xray2],
|
144 |
-
outputs=[output_file, xray1_output, xray2_output, diagnosis_output, severity_output, treatment_output, gov_cost_output, private_cost_output],
|
145 |
-
)
|
146 |
-
|
147 |
-
# Run app
|
148 |
if __name__ == "__main__":
|
149 |
-
|
|
|
|
|
|
1 |
+
from flask import Flask, send_file
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
app = Flask(__name__)
|
|
|
5 |
|
6 |
+
# Route to serve HTML file
|
7 |
+
@app.route("/")
|
8 |
+
def serve_html():
|
9 |
+
return send_file("re.html")
|
10 |
|
11 |
+
# Run Gradio separately
|
12 |
+
def run_gradio():
|
13 |
+
gr.Interface(lambda: "Gradio App Running!", inputs=[], outputs="text").launch(share=True)
|
|
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if __name__ == "__main__":
|
16 |
+
from threading import Thread
|
17 |
+
Thread(target=run_gradio).start()
|
18 |
+
app.run(host="0.0.0.0", port=7860)
|