Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,35 +20,64 @@ logger = logging.getLogger(__name__)
|
|
20 |
api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
|
21 |
external_user_id = 'plugin-1717464304'
|
22 |
|
23 |
-
# Step 1: Create a chat session
|
24 |
def create_chat_session():
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
39 |
def submit_query(session_id, query):
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
def extract_json_from_answer(answer):
|
|
|
20 |
api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
|
21 |
external_user_id = 'plugin-1717464304'
|
22 |
|
|
|
23 |
def create_chat_session():
|
24 |
+
try:
|
25 |
+
create_session_url = 'https://api.on-demand.io/chat/v1/sessions'
|
26 |
+
create_session_headers = {
|
27 |
+
'apikey': api_key,
|
28 |
+
'Content-Type': 'application/json'
|
29 |
+
}
|
30 |
+
create_session_body = {
|
31 |
+
"pluginIds": [],
|
32 |
+
"externalUserId": external_user_id
|
33 |
+
}
|
34 |
+
|
35 |
+
response = requests.post(create_session_url, headers=create_session_headers, json=create_session_body)
|
36 |
+
response.raise_for_status()
|
37 |
+
return response.json()['data']['id']
|
38 |
+
|
39 |
+
except requests.exceptions.RequestException as e:
|
40 |
+
logger.error(f"Error creating chat session: {str(e)}")
|
41 |
+
raise
|
42 |
+
|
43 |
def submit_query(session_id, query):
|
44 |
+
try:
|
45 |
+
submit_query_url = f'https://api.on-demand.io/chat/v1/sessions/{session_id}/query'
|
46 |
+
submit_query_headers = {
|
47 |
+
'apikey': api_key,
|
48 |
+
'Content-Type': 'application/json'
|
49 |
+
}
|
50 |
+
|
51 |
+
structured_query = f"""
|
52 |
+
Based on the following patient information, provide a detailed medical analysis in JSON format:
|
53 |
+
|
54 |
+
{query}
|
55 |
+
|
56 |
+
Return only valid JSON with these fields:
|
57 |
+
- diagnosis_details
|
58 |
+
- probable_diagnoses (array)
|
59 |
+
- treatment_plans (array)
|
60 |
+
- lifestyle_modifications (array)
|
61 |
+
- medications (array of objects with name and dosage)
|
62 |
+
- additional_tests (array)
|
63 |
+
- precautions (array)
|
64 |
+
- follow_up (string)
|
65 |
+
"""
|
66 |
+
|
67 |
+
submit_query_body = {
|
68 |
+
"endpointId": "predefined-openai-gpt4o",
|
69 |
+
"query": structured_query,
|
70 |
+
"pluginIds": ["plugin-1712327325", "plugin-1713962163"],
|
71 |
+
"responseMode": "sync"
|
72 |
+
}
|
73 |
+
|
74 |
+
response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
|
75 |
+
response.raise_for_status()
|
76 |
+
return response.json()
|
77 |
+
|
78 |
+
except requests.exceptions.RequestException as e:
|
79 |
+
logger.error(f"Error submitting query: {str(e)}")
|
80 |
+
raise
|
81 |
|
82 |
|
83 |
def extract_json_from_answer(answer):
|