Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
from embedding_manager import EmbeddingManager
|
| 7 |
+
from summarizer import PatientChartSummarizer
|
| 8 |
+
from hedis_engine import HedisComplianceEngine
|
| 9 |
|
| 10 |
+
APP_TITLE = "ChartWise AI"
|
| 11 |
+
DEFAULT_MEASURE_YEAR = 2024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# --- Handlers ---
|
| 14 |
+
def generate_patient_summary(pdf_file):
|
| 15 |
+
try:
|
| 16 |
+
if pdf_file is None:
|
| 17 |
+
return "⚠️ Please upload a PDF file first."
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 19 |
+
tmp.write(pdf_file)
|
| 20 |
+
pdf_path = tmp.name
|
| 21 |
|
| 22 |
+
manager = EmbeddingManager(pdf_path)
|
| 23 |
+
vectordb = manager.get_or_create_embeddings()
|
| 24 |
|
| 25 |
+
summarizer = PatientChartSummarizer(vectordb)
|
| 26 |
+
result = summarizer.summarize_chart()
|
| 27 |
+
os.unlink(pdf_path)
|
| 28 |
+
return result
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"❌ Error processing chart: {e}"
|
| 31 |
|
| 32 |
+
def generate_hedis_analysis(pdf_file, measure_name, measurement_year):
|
| 33 |
+
try:
|
| 34 |
+
if pdf_file is None:
|
| 35 |
+
return "⚠️ Please upload a PDF file first."
|
| 36 |
+
if not measure_name:
|
| 37 |
+
return "⚠️ Please enter a HEDIS measure code (e.g., COL, BCS, CCS, AAB)."
|
| 38 |
+
if not measurement_year:
|
| 39 |
+
return "⚠️ Please enter a measurement year."
|
| 40 |
|
| 41 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 42 |
+
tmp.write(pdf_file)
|
| 43 |
+
pdf_path = tmp.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
manager = EmbeddingManager(pdf_path)
|
| 46 |
+
vectordb = manager.get_or_create_embeddings()
|
| 47 |
|
| 48 |
+
hedis = HedisComplianceEngine(vectordb, measure_name, int(measurement_year))
|
| 49 |
+
result = hedis.run()
|
| 50 |
+
os.unlink(pdf_path)
|
| 51 |
+
return result
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"❌ Error processing HEDIS analysis: {e}"
|
| 54 |
|
| 55 |
+
# --- Gradio Theme ---
|
| 56 |
+
simple_theme = gr.themes.Soft(
|
| 57 |
+
primary_hue=gr.themes.colors.blue,
|
| 58 |
+
secondary_hue=gr.themes.colors.slate,
|
| 59 |
+
neutral_hue=gr.themes.colors.slate,
|
| 60 |
+
).set(
|
| 61 |
+
button_primary_background_fill="#1e40af",
|
| 62 |
+
button_primary_background_fill_hover="#1d4ed8",
|
| 63 |
+
button_primary_text_color="white",
|
| 64 |
+
background_fill_primary="white",
|
| 65 |
+
background_fill_secondary="#f8fafc",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# --- Interface ---
|
| 69 |
+
with gr.Blocks(theme=simple_theme, title="ChartWise AI") as interface:
|
| 70 |
+
gr.HTML("<h1 style='text-align:center;color:#1e40af;'>🏥 ChartWise AI</h1>")
|
| 71 |
+
gr.HTML("<p style='text-align:center;color:#64748b;'>Patient Chart Analysis & HEDIS Compliance</p>")
|
| 72 |
|
| 73 |
+
with gr.Row(equal_height=True):
|
| 74 |
+
# Left: Patient Summary
|
| 75 |
+
with gr.Column():
|
| 76 |
+
gr.HTML("<h3 style='color:#1e40af;'>📋 Patient Chart Analysis</h3>")
|
| 77 |
+
pdf_upload = gr.File(label="Upload Patient Chart (PDF)", file_types=[".pdf"], type="binary")
|
| 78 |
+
summary_btn = gr.Button("📊 Generate Patient Summary", variant="primary")
|
| 79 |
+
summary_output = gr.Markdown(
|
| 80 |
+
label="Patient Summary Results",
|
| 81 |
+
value="Upload a patient chart and click 'Generate Patient Summary' to see results here.",
|
| 82 |
+
height=400
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# Right: HEDIS
|
| 86 |
+
with gr.Column():
|
| 87 |
+
gr.HTML("<h3 style='color:#1e40af;'>🎯 HEDIS Measure Analysis</h3>")
|
| 88 |
+
hedis_measure = gr.Textbox(
|
| 89 |
+
label="HEDIS Measure Code",
|
| 90 |
+
placeholder="e.g., COL, BCS, CCS, AAB",
|
| 91 |
+
info="Measure code per NCQA HEDIS"
|
| 92 |
+
)
|
| 93 |
+
measurement_year = gr.Number(
|
| 94 |
+
label="Measurement Year",
|
| 95 |
+
value=DEFAULT_MEASURE_YEAR,
|
| 96 |
+
precision=0,
|
| 97 |
+
info="e.g., 2024"
|
| 98 |
+
)
|
| 99 |
+
hedis_btn = gr.Button("🎯 Run HEDIS Analysis", variant="secondary")
|
| 100 |
+
hedis_output = gr.Markdown(
|
| 101 |
+
label="HEDIS Analysis Results",
|
| 102 |
+
value="Enter measure and year, then click 'Run HEDIS Analysis'.",
|
| 103 |
+
height=400
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Wire events
|
| 107 |
+
summary_btn.click(fn=generate_patient_summary, inputs=[pdf_upload], outputs=[summary_output])
|
| 108 |
+
hedis_btn.click(fn=generate_hedis_analysis, inputs=[pdf_upload, hedis_measure, measurement_year], outputs=[hedis_output])
|
| 109 |
+
|
| 110 |
+
# Spaces auto-runs the script; these hints are fine, too:
|
| 111 |
if __name__ == "__main__":
|
| 112 |
+
# On Spaces, port/host are managed, but these envs are also supported by Gradio. [oai_citation:6‡Gradio](https://www.gradio.app/guides/environment-variables?utm_source=chatgpt.com)
|
| 113 |
+
interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")))
|