ciaochris commited on
Commit
6b56e31
·
verified ·
1 Parent(s): 201d311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  from groq import Groq
4
  import json
5
  from dotenv import load_dotenv
 
6
 
7
  # Load environment variables
8
  load_dotenv()
@@ -12,6 +13,33 @@ client = Groq(
12
  api_key=os.environ.get("GROQ_API_KEY"),
13
  )
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def get_diagnosis(age, gender, symptoms, medical_history):
16
  prompt = f"""
17
  Given the following patient information, provide a preliminary analysis of potential cancer risks.
@@ -23,24 +51,19 @@ def get_diagnosis(age, gender, symptoms, medical_history):
23
  Symptoms: {symptoms}
24
  Medical History: {medical_history}
25
 
26
- Please provide a JSON response with the following structure:
27
- {{
28
- "potential_cancer_types": [
29
- {{
30
- "name": "Cancer Type",
31
- "risk_level": "Low/Medium/High",
32
- "description": "Brief description of why this cancer type is considered"
33
- }}
34
- ],
35
- "recommended_tests": [
36
- {{
37
- "name": "Test Name",
38
- "description": "Brief description of why this test is recommended"
39
- }}
40
- ],
41
- "general_advice": "General health advice for the patient",
42
- "disclaimer": "A strong disclaimer about the limitations of this assessment"
43
- }}
44
 
45
  Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
46
  """
@@ -60,15 +83,11 @@ def get_diagnosis(age, gender, symptoms, medical_history):
60
 
61
  response_content = chat_completion.choices[0].message.content
62
 
63
- # Try to parse the JSON response
64
  try:
65
  response = json.loads(response_content)
66
  except json.JSONDecodeError:
67
- # If JSON parsing fails, return the raw text response
68
- return {
69
- "error": "Failed to parse API response as JSON. Raw response:",
70
- "raw_response": response_content
71
- }
72
 
73
  return response
74
  except Exception as e:
@@ -76,10 +95,7 @@ def get_diagnosis(age, gender, symptoms, medical_history):
76
 
77
  def format_output(response):
78
  if "error" in response:
79
- if "raw_response" in response:
80
- return f"Error: {response['error']}\n\nRaw API Response:\n{response['raw_response']}"
81
- else:
82
- return f"Error: {response['error']}"
83
 
84
  output = "HealthScan AI: Personalized Cancer Risk Insights\n\n"
85
 
 
3
  from groq import Groq
4
  import json
5
  from dotenv import load_dotenv
6
+ import re
7
 
8
  # Load environment variables
9
  load_dotenv()
 
13
  api_key=os.environ.get("GROQ_API_KEY"),
14
  )
15
 
16
+ def parse_non_json_response(text):
17
+ # Attempt to extract structured information from non-JSON text
18
+ cancer_types = re.findall(r"(?:cancer type|Cancer Type):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
19
+ risk_levels = re.findall(r"(?:risk level|Risk Level):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
20
+ descriptions = re.findall(r"(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
21
+
22
+ potential_cancer_types = [
23
+ {"name": ct, "risk_level": rl, "description": desc}
24
+ for ct, rl, desc in zip(cancer_types, risk_levels, descriptions)
25
+ ]
26
+
27
+ recommended_tests = re.findall(r"(?:Test Name):\s*(.+?)\n(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
28
+ recommended_tests = [{"name": name, "description": desc} for name, desc in recommended_tests]
29
+
30
+ general_advice = re.search(r"(?:General Advice|general advice):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
31
+ general_advice = general_advice.group(1) if general_advice else "No general advice provided."
32
+
33
+ disclaimer = re.search(r"(?:DISCLAIMER|Disclaimer):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
34
+ disclaimer = disclaimer.group(1) if disclaimer else "No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice."
35
+
36
+ return {
37
+ "potential_cancer_types": potential_cancer_types,
38
+ "recommended_tests": recommended_tests,
39
+ "general_advice": general_advice,
40
+ "disclaimer": disclaimer
41
+ }
42
+
43
  def get_diagnosis(age, gender, symptoms, medical_history):
44
  prompt = f"""
45
  Given the following patient information, provide a preliminary analysis of potential cancer risks.
 
51
  Symptoms: {symptoms}
52
  Medical History: {medical_history}
53
 
54
+ Please provide a response with the following structure:
55
+ Potential Cancer Types:
56
+ - Cancer Type: [Name]
57
+ Risk Level: [Low/Medium/High]
58
+ Description: [Brief description of why this cancer type is considered]
59
+
60
+ Recommended Tests:
61
+ - Test Name: [Name]
62
+ Description: [Brief description of why this test is recommended]
63
+
64
+ General Advice: [General health advice for the patient]
65
+
66
+ DISCLAIMER: [A strong disclaimer about the limitations of this assessment]
 
 
 
 
 
67
 
68
  Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
69
  """
 
83
 
84
  response_content = chat_completion.choices[0].message.content
85
 
86
+ # Try to parse the response as JSON, if it fails, parse it as text
87
  try:
88
  response = json.loads(response_content)
89
  except json.JSONDecodeError:
90
+ response = parse_non_json_response(response_content)
 
 
 
 
91
 
92
  return response
93
  except Exception as e:
 
95
 
96
  def format_output(response):
97
  if "error" in response:
98
+ return f"Error: {response['error']}"
 
 
 
99
 
100
  output = "HealthScan AI: Personalized Cancer Risk Insights\n\n"
101