Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,11 @@
|
|
1 |
-
from flask import Flask, render_template, request, send_file
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import tensorflow as tf
|
5 |
-
import numpy as np
|
6 |
-
from tensorflow.keras.preprocessing import image
|
7 |
-
from PIL import Image
|
8 |
-
from reportlab.lib.pagesizes import letter
|
9 |
-
from reportlab.pdfgen import canvas
|
10 |
-
import os
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
image_size = (224, 224)
|
20 |
-
|
21 |
-
def predict_fracture(xray):
|
22 |
-
img = Image.open(xray).resize(image_size)
|
23 |
-
img_array = image.img_to_array(img) / 255.0
|
24 |
-
img_array = np.expand_dims(img_array, axis=0)
|
25 |
-
prediction = model.predict(img_array)[0][0]
|
26 |
-
return prediction
|
27 |
-
|
28 |
-
# Predict on both X-rays
|
29 |
-
prediction1 = predict_fracture(xray1)
|
30 |
-
prediction2 = predict_fracture(xray2)
|
31 |
-
avg_prediction = (prediction1 + prediction2) / 2
|
32 |
-
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
33 |
-
|
34 |
-
# Injury severity classification
|
35 |
-
severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
|
36 |
-
treatment = {
|
37 |
-
"Mild": "Rest, pain relievers, follow-up X-ray.",
|
38 |
-
"Moderate": "Plaster cast, possible minor surgery.",
|
39 |
-
"Severe": "Major surgery, metal implants, physiotherapy."
|
40 |
-
}[severity]
|
41 |
-
gov_cost = {"Mild": "₹2,000 - ₹5,000", "Moderate": "₹8,000 - ₹15,000", "Severe": "₹20,000 - ₹50,000"}[severity]
|
42 |
-
private_cost = {"Mild": "₹10,000 - ₹20,000", "Moderate": "₹30,000 - ₹60,000", "Severe": "₹1,00,000+"}[severity]
|
43 |
-
|
44 |
-
# Generate PDF report
|
45 |
-
report_path = f"{name}_fracture_report.pdf"
|
46 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
47 |
-
c.setFont("Helvetica", 12)
|
48 |
-
c.drawString(100, 750, f"Patient Name: {name}")
|
49 |
-
c.drawString(100, 730, f"Age: {age}")
|
50 |
-
c.drawString(100, 710, f"Gender: {gender}")
|
51 |
-
c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
|
52 |
-
c.drawString(100, 670, f"Injury Severity: {severity}")
|
53 |
-
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
54 |
-
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
55 |
-
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
56 |
-
c.save()
|
57 |
-
|
58 |
-
return report_path # Return path for auto-download
|
59 |
-
|
60 |
-
# Flask Route: Serve HTML Page
|
61 |
-
@app.route("/")
|
62 |
-
def home():
|
63 |
-
return render_template("re.html")
|
64 |
-
|
65 |
-
# Flask Route: Handle Form Submission
|
66 |
-
@app.route("/submit_report", methods=["POST"])
|
67 |
-
def submit_report():
|
68 |
-
name = request.form["first_name"] + " " + request.form["surname"]
|
69 |
-
age = request.form["age"]
|
70 |
-
gender = request.form["gender"]
|
71 |
-
xray1 = request.files["xray_side"]
|
72 |
-
xray2 = request.files["xray_top"]
|
73 |
-
|
74 |
-
# Generate PDF report
|
75 |
-
pdf_path = generate_report(name, age, gender, xray1, xray2)
|
76 |
-
|
77 |
-
return send_file(pdf_path, as_attachment=True) # Auto-download report
|
78 |
-
|
79 |
-
# Run Gradio in a separate thread
|
80 |
-
def run_gradio():
|
81 |
-
interface = gr.Interface(
|
82 |
-
fn=generate_report,
|
83 |
-
inputs=[
|
84 |
-
gr.Textbox(label="Patient Name"),
|
85 |
-
gr.Number(label="Age"),
|
86 |
-
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
87 |
-
gr.Image(type="file", label="Upload X-ray Image 1"),
|
88 |
-
gr.Image(type="file", label="Upload X-ray Image 2"),
|
89 |
-
],
|
90 |
-
outputs=gr.File(label="Download Report"),
|
91 |
-
title="Bone Fracture Detection & Medical Report",
|
92 |
-
description="Enter patient details, upload two X-ray images, and generate a detailed medical report."
|
93 |
-
)
|
94 |
-
interface.launch(share=True)
|
95 |
-
|
96 |
-
if __name__ == "__main__":
|
97 |
-
threading.Thread(target=run_gradio).start()
|
98 |
-
app.run(host="0.0.0.0", port=7861, debug=True) # Flask runs separately
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Start Flask in the background
|
5 |
+
subprocess.Popen(["python3", "flask_app.py"])
|
6 |
|
7 |
+
def generate_report():
|
8 |
+
return "Gradio App Running!"
|
9 |
|
10 |
+
interface = gr.Interface(fn=generate_report, inputs=[], outputs="text")
|
11 |
+
interface.launch(server_name="0.0.0.0", server_port=7861) # Run Gradio on 7861
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|