File size: 9,012 Bytes
5aec14b
9cb381b
09daffd
 
d452590
9cb381b
219df52
09daffd
 
 
219df52
09daffd
9cb381b
 
 
4a78b46
 
09daffd
9cb381b
 
 
 
 
 
09daffd
219df52
 
 
 
 
 
 
 
5aec14b
 
 
9cb381b
219df52
e714a62
 
9cb381b
 
e714a62
9cb381b
e714a62
 
 
9cb381b
 
 
 
 
 
e714a62
 
219df52
 
9cb381b
36e302c
5aec14b
219df52
5aec14b
 
 
36e302c
 
c37ed45
 
aa6bf8a
c37ed45
36e302c
 
aa6bf8a
5aec14b
aa6bf8a
 
c37ed45
 
 
 
9cb381b
5aec14b
 
 
 
aa6bf8a
 
c37ed45
 
 
 
36e302c
219df52
 
 
36e302c
c37ed45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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

logging.basicConfig(level=logging.INFO)
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
)

@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:
        return render_template('dashboard.html', error="Failed to fetch patient data")
    patients = [entry['resource'] for entry in patients_data.get('entry', [])]
    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 = generate_discharge_paper_one_click(
            meldrx_api, grok_client, patient_id, first_name, last_name
        )

        if action == "Display Summary":
            return render_template('oneclick.html', 
                                status=status,
                                basic_summary=basic_summary.replace('\n', '<br>') if basic_summary else None,
                                ai_summary=ai_summary.replace('\n', '<br>') if ai_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', '<br>') if basic_summary else None,
                            ai_summary=ai_summary.replace('\n', '<br>') if ai_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)



# # 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 xai import Grok  # Adjust import based on actual xAI SDK

# logging.basicConfig(level=logging.DEBUG)  # Changed to DEBUG for more detailed logs
# 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
# )

# # Initialize xAI client
# grok_client = Grok()  # Adjust initialization as needed

# @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 = generate_discharge_paper_one_click(
#             meldrx_api, grok_client, patient_id, first_name, last_name
#         )

#         if action == "Display Summary":
#             return render_template('oneclick.html', 
#                                 status=status,
#                                 basic_summary=basic_summary.replace('\n', '<br>') if basic_summary else None,
#                                 ai_summary=ai_summary.replace('\n', '<br>') if ai_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', '<br>') if basic_summary else None,
#                             ai_summary=ai_summary.replace('\n', '<br>') if ai_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)