# app.py from flask import Flask, render_template, request, send_file, redirect, url_for import os import logging from utils.meldrx import MeldRxAPI from utils.oneclick import generate_discharge_paper_one_click from huggingface_hub import InferenceClient logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) app = Flask(__name__) CLIENT_ID = os.getenv("MELDRX_CLIENT_ID", "04bdc9f9a23d488a868b93d594ee5a4a") CLIENT_SECRET = os.getenv("MELDRX_CLIENT_SECRET", None) WORKSPACE_ID = os.getenv("MELDRX_WORKSPACE_ID", "09ed4f76-b5ac-42bf-92d5-496933203dbe") SPACE_URL = os.getenv("SPACE_URL", "https://multitransformer-tonic-discharge-guard.hf.space") REDIRECT_URI = f"{SPACE_URL}/auth/callback" meldrx_api = MeldRxAPI( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, workspace_id=WORKSPACE_ID, redirect_uri=REDIRECT_URI ) HF_TOKEN = os.getenv("HF_TOKEN") if not HF_TOKEN: raise ValueError("HF_TOKEN environment variable not set.") client = InferenceClient(api_key=HF_TOKEN) @app.route('/') def index(): return render_template('index.html') @app.route('/auth', methods=['GET', 'POST']) def auth(): if request.method == 'POST': auth_code = request.form.get('auth_code') if auth_code and meldrx_api.authenticate_with_code(auth_code): return redirect(url_for('dashboard')) return render_template('auth.html', auth_url=meldrx_api.get_authorization_url(), auth_result="Authentication failed") return render_template('auth.html', auth_url=meldrx_api.get_authorization_url()) @app.route('/auth/callback', methods=['GET']) def auth_callback(): auth_code = request.args.get('code') if auth_code and meldrx_api.authenticate_with_code(auth_code): return redirect(url_for('dashboard')) return render_template('auth.html', auth_url=meldrx_api.get_authorization_url(), auth_result="Callback failed") @app.route('/dashboard', methods=['GET']) def dashboard(): if not meldrx_api.access_token: return redirect(url_for('auth')) patients_data = meldrx_api.get_patients() if not patients_data or "entry" not in patients_data: logger.error("Failed to fetch patient data for dashboard") return render_template('dashboard.html', error="Failed to fetch patient data") patients = [entry['resource'] for entry in patients_data.get('entry', [])] logger.debug(f"Patients for dashboard: {patients}") return render_template('dashboard.html', patients=patients, authenticated=True) @app.route('/oneclick', methods=['GET', 'POST']) def one_click(): if not meldrx_api.access_token: return redirect(url_for('auth')) if request.method == 'POST': patient_id = request.form.get('patient_id', '').strip() first_name = request.form.get('first_name', '').strip() last_name = request.form.get('last_name', '').strip() action = request.form.get('action', '') logger.info(f"One-click request - ID: {patient_id}, First: {first_name}, Last: {last_name}, Action: {action}") pdf_path, status, basic_summary, ai_summary, verified_summary = generate_discharge_paper_one_click( meldrx_api, client, patient_id, first_name, last_name ) if action == "Display Summary": return render_template('oneclick.html', status=status, basic_summary=basic_summary.replace('\n', '
') if basic_summary else None, ai_summary=ai_summary.replace('\n', '
') if ai_summary else None, verified_summary=verified_summary if verified_summary else None, patient_id=patient_id, first_name=first_name, last_name=last_name) elif action == "Generate PDF" and pdf_path: return send_file(pdf_path, as_attachment=True, download_name=f"discharge_summary_{patient_id or 'patient'}.pdf") return render_template('oneclick.html', status=status, basic_summary=basic_summary.replace('\n', '
') if basic_summary else None, ai_summary=ai_summary.replace('\n', '
') if ai_summary else None, verified_summary=verified_summary if verified_summary else None, patient_id=patient_id, first_name=first_name, last_name=last_name) return render_template('oneclick.html') if __name__ == '__main__': port = int(os.getenv("PORT", 7860)) app.run(debug=False, host='0.0.0.0', port=port)