Rammohan0504 commited on
Commit
6e4efc6
·
verified ·
1 Parent(s): 0827283

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -85
app.py CHANGED
@@ -6,6 +6,9 @@ from sklearn.linear_model import LinearRegression
6
  import random
7
  import base64
8
  import joblib
 
 
 
9
 
10
  # Initialize the face mesh model
11
  mp_face_mesh = mp.solutions.face_mesh
@@ -114,88 +117,15 @@ def build_table(title, rows):
114
  return html
115
 
116
 
117
- # Build health card layout
118
- def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
119
- from datetime import datetime
120
- current_date = datetime.now().strftime("%B %d, %Y")
121
-
122
- html = f"""
123
- <div id="health-card" style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 700px; margin: 20px auto; border-radius: 16px; background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%); border: 2px solid #ddd; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15); padding: 30px; color: #1a1a1a;">
124
-
125
- <div style="background-color: rgba(255, 255, 255, 0.9); border-radius: 12px; padding: 20px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
126
- <div style="display: flex; align-items: center; margin-bottom: 15px;">
127
- <div style="background: linear-gradient(135deg, #64b5f6, #42a5f5); padding: 8px 16px; border-radius: 8px; margin-right: 20px;">
128
- <h3 style="margin: 0; font-size: 16px; color: white; font-weight: 600;">HEALTH CARD</h3>
129
- </div>
130
- <div style="margin-left: auto; text-align: right; color: #666; font-size: 12px;">
131
- <div>Report Date: {current_date}</div>
132
- {f'<div>Patient ID: {patient_id}</div>' if patient_id else ''}
133
- </div>
134
- </div>
135
- <div style="display: flex; align-items: center;">
136
- <img src="data:image/png;base64,{profile_image}" alt="Profile" style="width: 90px; height: 90px; border-radius: 50%; margin-right: 20px; border: 3px solid #fff; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
137
- <div>
138
- <h2 style="margin: 0; font-size: 28px; color: #2c3e50; font-weight: 700;">{patient_name if patient_name else "Lab Test Results"}</h2>
139
- <p style="margin: 4px 0 0 0; color: #666; font-size: 14px;">{f"Age: {patient_age} | Gender: {patient_gender}" if patient_age and patient_gender else "AI-Generated Health Analysis"}</p>
140
- <p style="margin: 4px 0 0 0; color: #888; font-size: 12px;">Face-Based Health Analysis Report</p>
141
- </div>
142
- </div>
143
- </div>
144
-
145
- <div style="background-color: rgba(255, 255, 255, 0.95); border-radius: 12px; padding: 25px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
146
- {test_results['Hematology']}
147
- {test_results['Iron Panel']}
148
- {test_results['Liver & Kidney']}
149
- {test_results['Electrolytes']}
150
- {test_results['Vitals']}
151
- </div>
152
-
153
- <div style="background-color: rgba(255, 255, 255, 0.95); padding: 20px; border-radius: 12px; border: 1px solid #e0e0e0; margin-bottom: 25px;">
154
- <h4 style="margin: 0 0 15px 0; color: #2c3e50; font-size: 18px; font-weight: 600;">📝 Summary & Recommendations</h4>
155
- <div style="color: #444; line-height: 1.6;">
156
- {summary}
157
- </div>
158
- </div>
159
-
160
- <div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
161
- <button onclick="window.print()" style="padding: 12px 24px; background: linear-gradient(135deg, #4caf50, #45a049); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3); transition: all 0.3s;">
162
- 📥 Download Report
163
- </button>
164
- <button style="padding: 12px 24px; background: linear-gradient(135deg, #2196f3, #1976d2); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);">
165
- 📞 Find Labs Near Me
166
- </button>
167
- </div>
168
- </div>
169
-
170
- <style>
171
- @media print {{
172
- body * {{
173
- visibility: hidden;
174
- }}
175
- #health-card, #health-card * {{
176
- visibility: visible;
177
- }}
178
- #health-card {{
179
- position: absolute;
180
- left: 0;
181
- top: 0;
182
- width: 100% !important;
183
- max-width: none !important;
184
- margin: 0 !important;
185
- box-shadow: none !important;
186
- border: none !important;
187
- }}
188
- button {{
189
- display: none !important;
190
- }}
191
- }}
192
- </style>
193
- """
194
- return html
195
-
196
 
197
- # Initialize global variable for patient details
198
- current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
 
 
199
 
200
  # Modified analyze_face function
201
  def analyze_face(input_data):
@@ -298,7 +228,10 @@ def analyze_face(input_data):
298
  current_patient_details['gender'],
299
  current_patient_details['id']
300
  )
301
- return health_card_html, frame_rgb
 
 
 
302
 
303
 
304
  # Modified route_inputs function
@@ -317,8 +250,9 @@ def route_inputs(mode, image, video, patient_name, patient_age, patient_gender,
317
  'id': patient_id
318
  }
319
 
320
- health_card_html, frame_rgb = analyze_face(image if mode == "Image" else video)
321
- return health_card_html, frame_rgb
 
322
 
323
 
324
  # Create Gradio interface
@@ -343,7 +277,9 @@ with gr.Blocks() as demo:
343
  with gr.Column():
344
  result_html = gr.HTML(label="🧪 Health Report Table")
345
  result_image = gr.Image(label="📷 Key Frame Snapshot")
346
-
 
 
347
  submit_btn.click(fn=route_inputs,
348
  inputs=[mode_selector, image_input, video_input, patient_name, patient_age, patient_gender, patient_id],
349
  outputs=[result_html, result_image])
 
6
  import random
7
  import base64
8
  import joblib
9
+ import pdfkit
10
+ from io import BytesIO
11
+ import json
12
 
13
  # Initialize the face mesh model
14
  mp_face_mesh = mp.solutions.face_mesh
 
117
  return html
118
 
119
 
120
+ # Generate PDF from HTML content
121
+ def generate_pdf(html_content):
122
+ pdf = pdfkit.from_string(html_content, False)
123
+ return BytesIO(pdf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
+ # Find labs using geolocation (for example purposes, replace with actual logic)
126
+ def find_labs_nearby(location):
127
+ # This can use the location to query a database of nearby labs
128
+ return f"Nearby labs for your location {location}: Lab A, Lab B, Lab C."
129
 
130
  # Modified analyze_face function
131
  def analyze_face(input_data):
 
228
  current_patient_details['gender'],
229
  current_patient_details['id']
230
  )
231
+
232
+ # Generate PDF from the HTML content
233
+ pdf_file = generate_pdf(health_card_html)
234
+ return health_card_html, pdf_file
235
 
236
 
237
  # Modified route_inputs function
 
250
  'id': patient_id
251
  }
252
 
253
+ health_card_html, pdf_file = analyze_face(image if mode == "Image" else video)
254
+
255
+ return health_card_html, pdf_file
256
 
257
 
258
  # Create Gradio interface
 
277
  with gr.Column():
278
  result_html = gr.HTML(label="🧪 Health Report Table")
279
  result_image = gr.Image(label="📷 Key Frame Snapshot")
280
+ download_btn = gr.Button("📥 Download PDF")
281
+ download_btn.click(fn=lambda pdf_file: pdf_file, inputs=[result_html], outputs=[download_btn])
282
+
283
  submit_btn.click(fn=route_inputs,
284
  inputs=[mode_selector, image_input, video_input, patient_name, patient_age, patient_gender, patient_id],
285
  outputs=[result_html, result_image])