|
|
import gradio as gr |
|
|
import matplotlib.pyplot as plt |
|
|
import json |
|
|
import tempfile |
|
|
import os |
|
|
|
|
|
from backend_app import analyzer |
|
|
|
|
|
def process_single_report(file): |
|
|
result = analyzer.process_user_report(file.name) |
|
|
if result['status'] != 'success': |
|
|
return result['message'], None, None, None, None, None, None |
|
|
|
|
|
analysis = analyzer.generate_single_report_analysis(result['report_id']) |
|
|
|
|
|
suggestions = analysis['health_suggestions'] |
|
|
parameters = analysis['categorized_parameters'] |
|
|
abnormal_parameters = analysis['abnormal_parameters'] |
|
|
|
|
|
vis = analysis['visualizations'] |
|
|
|
|
|
status_chart_img = analyzer.generate_visualization_image(vis['status_chart']) |
|
|
abnormal_chart_img = analyzer.generate_visualization_image(vis['abnormal_chart']) if vis['abnormal_chart'] else None |
|
|
category_chart_img = analyzer.generate_visualization_image(vis['category_chart']) |
|
|
|
|
|
return ( |
|
|
f"β
Report Processed Successfully!\nAbnormal Parameters Detected: {analysis['abnormal_count']}", |
|
|
suggestions, |
|
|
status_chart_img, |
|
|
abnormal_chart_img, |
|
|
category_chart_img, |
|
|
parameters, |
|
|
abnormal_parameters |
|
|
) |
|
|
|
|
|
|
|
|
def compare_multiple_reports(files): |
|
|
if not files or len(files) < 2: |
|
|
return "Please upload at least 2 reports.", None, None, None |
|
|
|
|
|
paths = [file.name for file in files] |
|
|
result = analyzer.process_multiple_reports(paths) |
|
|
|
|
|
if result['status'] != 'success': |
|
|
return result['message'], None, None, None |
|
|
|
|
|
comparison = analyzer.compare_reports(result['report_ids']) |
|
|
|
|
|
if comparison['status'] != 'success': |
|
|
return comparison['message'], None, None, None |
|
|
|
|
|
health_insights = comparison['health_insights'] |
|
|
|
|
|
summary_chart_img = analyzer.generate_visualization_image(comparison['summary_chart']) |
|
|
|
|
|
trends_imgs = [] |
|
|
for category, charts in comparison['categorized_charts'].items(): |
|
|
for chart in charts: |
|
|
img = analyzer.generate_visualization_image(chart) |
|
|
trends_imgs.append((chart['title'], img)) |
|
|
|
|
|
return ( |
|
|
f"Reports Compared Successfully!\nCommon Parameters: {comparison['common_parameters_count']}", |
|
|
health_insights, |
|
|
summary_chart_img, |
|
|
trends_imgs |
|
|
) |
|
|
|
|
|
def ask_question(question): |
|
|
if not question.strip(): |
|
|
return "Please type a question." |
|
|
|
|
|
answer = analyzer.answer_question(question) |
|
|
return answer |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown(""" |
|
|
# MediVeda Medical Report Analyzer |
|
|
|
|
|
Upload your medical reports to analyze health parameters, detect abnormalities, track changes, and get health advice! |
|
|
""") |
|
|
|
|
|
with gr.Tab("Single Report Analysis"): |
|
|
with gr.Row(): |
|
|
file_input = gr.File(label="Upload Medical Report (PDF)") |
|
|
analyze_btn = gr.Button("Analyze Report") |
|
|
output_text = gr.Markdown() |
|
|
health_suggestions = gr.Markdown(label="Health Suggestions") |
|
|
with gr.Row(): |
|
|
status_chart = gr.Image() |
|
|
abnormal_chart = gr.Image() |
|
|
category_chart = gr.Image() |
|
|
with gr.Row(): |
|
|
extracted_parameters = gr.JSON(label="Extracted Parameters") |
|
|
abnormalities = gr.JSON(label="Abnormal Parameters") |
|
|
|
|
|
analyze_btn.click( |
|
|
fn=process_single_report, |
|
|
inputs=[file_input], |
|
|
outputs=[output_text, health_suggestions, status_chart, abnormal_chart, category_chart, extracted_parameters, abnormalities] |
|
|
) |
|
|
|
|
|
with gr.Tab("Multiple Reports Comparison"): |
|
|
with gr.Row(): |
|
|
files_input = gr.Files(label="Upload 2-3 Reports (PDF)") |
|
|
compare_btn = gr.Button("Compare Reports") |
|
|
comparison_text = gr.Markdown() |
|
|
health_insights = gr.Markdown(label="Health Insights") |
|
|
summary_chart = gr.Image() |
|
|
trend_charts_gallery = gr.Gallery(label="Parameter Trends", columns=2) |
|
|
|
|
|
compare_btn.click( |
|
|
fn=compare_multiple_reports, |
|
|
inputs=[files_input], |
|
|
outputs=[comparison_text, health_insights, summary_chart, trend_charts_gallery] |
|
|
) |
|
|
|
|
|
with gr.Tab("Ask Medical Questions"): |
|
|
question_input = gr.Textbox(label="Ask a Question", placeholder="e.g., What does a high creatinine level mean?") |
|
|
ask_btn = gr.Button("Get Answer") |
|
|
answer_output = gr.Markdown() |
|
|
|
|
|
ask_btn.click( |
|
|
fn=ask_question, |
|
|
inputs=[question_input], |
|
|
outputs=[answer_output] |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|