from flask import Flask, render_template, request, send_file, jsonify, Response, redirect, url_for import os import json import logging from utils.callbackmanager import CallbackManager from utils.generators import (generate_pdf_from_form, generate_pdf_from_meldrx, analyze_dicom_file_with_ai, analyze_hl7_file_with_ai, analyze_cda_xml_file_with_ai, analyze_pdf_file_with_ai, analyze_csv_file_with_ai) from utils.oneclick import generate_discharge_paper_one_click from utils.meldrx import MeldRxAPI # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = Flask(__name__) # Set SPACE_URL from environment variable or default to the correct Space URL SPACE_URL = os.getenv("SPACE_URL", "https://multitransformer-tonic-discharge-guard.hf.space") REDIRECT_URI = f"{SPACE_URL}/auth/callback" CALLBACK_MANAGER = CallbackManager( redirect_uri=REDIRECT_URI, client_secret=None, ) # Mock display_form function for HTML rendering def display_form(first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code, doctor_first_name, doctor_last_name, doctor_middle_initial, hospital_name, doctor_address, doctor_city, doctor_state, doctor_zip, admission_date, referral_source, admission_method, discharge_date, discharge_reason, date_of_death, diagnosis, procedures, medications, preparer_name, preparer_job_title): return f"""
Patient Discharge Form
- Name: {first_name} {middle_initial} {last_name}
- Date of Birth: {dob}, Age: {age}, Sex: {sex}
- Address: {address}, {city}, {state}, {zip_code}
- Doctor: {doctor_first_name} {doctor_middle_initial} {doctor_last_name}
- Hospital/Clinic: {hospital_name}
- Doctor Address: {doctor_address}, {doctor_city}, {doctor_state}, {doctor_zip}
- Admission Date: {admission_date}, Source: {referral_source}, Method: {admission_method}
- Discharge Date: {discharge_date}, Reason: {discharge_reason}
- Date of Death: {date_of_death}
- Diagnosis: {diagnosis}
- Procedures: {procedures}
- Medications: {medications}
- Prepared By: {preparer_name}, {preparer_job_title}
""" @app.route('/') def index(): return render_template('index.html') @app.route('/auth', methods=['GET', 'POST']) def auth(): auth_url = CALLBACK_MANAGER.get_auth_url() if request.method == 'POST': redirected_url = request.form.get('redirected_url') if redirected_url: auth_code, error = CALLBACK_MANAGER.handle_callback(redirected_url) result = CALLBACK_MANAGER.set_auth_code(auth_code) if auth_code else f"{error}" return render_template('auth.html', auth_url=auth_url, auth_result=result) auth_code = request.form.get('auth_code') if auth_code: result = CALLBACK_MANAGER.set_auth_code(auth_code) return render_template('auth.html', auth_url=auth_url, auth_result=result) return render_template('auth.html', auth_url=auth_url) @app.route('/auth/callback', methods=['GET']) def auth_callback(): redirected_url = request.url result = CALLBACK_MANAGER.handle_callback(redirected_url) if "Authentication successful" in result: return redirect(url_for('dashboard')) return render_template('auth.html', auth_url=CALLBACK_MANAGER.get_auth_url(), auth_result=result) @app.route('/dashboard', methods=['GET']) def dashboard(): if not CALLBACK_MANAGER.access_token: return render_template('auth.html', auth_url=CALLBACK_MANAGER.get_auth_url(), auth_result="Please authenticate first.") data = CALLBACK_MANAGER.get_patient_data() if data.startswith('