ciaochris commited on
Commit
7675b2e
·
verified ·
1 Parent(s): 72aa582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -1
app.py CHANGED
@@ -6,7 +6,87 @@ from dotenv import load_dotenv
6
  import re
7
  import plotly.graph_objs as go
8
 
9
- # ... (previous code remains the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def plot_risk(potential_cancer_types):
12
  if not potential_cancer_types:
@@ -29,6 +109,34 @@ def plot_risk(potential_cancer_types):
29
 
30
  return fig
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def process_input(age, gender, symptoms, medical_history):
33
  errors = validate_input(age, gender, symptoms, medical_history)
34
  if errors:
 
6
  import re
7
  import plotly.graph_objs as go
8
 
9
+ # Load environment variables
10
+ load_dotenv()
11
+
12
+ # Initialize Groq client
13
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
14
+
15
+ def parse_non_json_response(text):
16
+ # Attempt to extract structured information from non-JSON text
17
+ cancer_types = re.findall(r"(?:cancer type|Cancer Type):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
18
+ risk_levels = re.findall(r"(?:risk level|Risk Level):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
19
+ descriptions = re.findall(r"(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
20
+
21
+ potential_cancer_types = [
22
+ {"name": ct, "risk_level": rl, "description": desc}
23
+ for ct, rl, desc in zip(cancer_types, risk_levels, descriptions)
24
+ ]
25
+
26
+ recommended_tests = re.findall(r"(?:Test Name):\s*(.+?)\n(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
27
+ recommended_tests = [{"name": name, "description": desc} for name, desc in recommended_tests]
28
+
29
+ general_advice = re.search(r"(?:General Advice|general advice):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
30
+ general_advice = general_advice.group(1) if general_advice else "No general advice provided."
31
+
32
+ disclaimer = re.search(r"(?:DISCLAIMER|Disclaimer):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
33
+ disclaimer = disclaimer.group(1) if disclaimer else "No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice."
34
+
35
+ return {
36
+ "potential_cancer_types": potential_cancer_types,
37
+ "recommended_tests": recommended_tests,
38
+ "general_advice": general_advice,
39
+ "disclaimer": disclaimer
40
+ }
41
+
42
+ def get_diagnosis(age, gender, symptoms, medical_history):
43
+ prompt = f"""
44
+ Given the following patient information, provide a preliminary analysis of potential cancer risks.
45
+ This is not a definitive diagnosis and should not replace professional medical advice.
46
+
47
+ Patient Information:
48
+ Age: {age}
49
+ Gender: {gender}
50
+ Symptoms: {symptoms}
51
+ Medical History: {medical_history}
52
+
53
+ Please provide a response with the following structure:
54
+ Potential Cancer Types:
55
+ - Cancer Type: [Name]
56
+ Risk Level: [Low/Medium/High]
57
+ Description: [Brief description of why this cancer type is considered]
58
+ Recommended Tests:
59
+ - Test Name: [Name]
60
+ Description: [Brief description of why this test is recommended]
61
+ General Advice: [General health advice for the patient]
62
+ DISCLAIMER: [A strong disclaimer about the limitations of this assessment]
63
+
64
+ Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
65
+ """
66
+
67
+ try:
68
+ chat_completion = client.chat.completions.create(
69
+ messages=[
70
+ {
71
+ "role": "user",
72
+ "content": prompt,
73
+ }
74
+ ],
75
+ model="llama-3.1-8b-instant", # Updated to a valid Groq model
76
+ temperature=0.5,
77
+ max_tokens=1500,
78
+ )
79
+
80
+ response_content = chat_completion.choices[0].message.content
81
+
82
+ try:
83
+ response = json.loads(response_content)
84
+ except json.JSONDecodeError:
85
+ response = parse_non_json_response(response_content)
86
+
87
+ return response
88
+ except Exception as e:
89
+ return {"error": f"An error occurred while communicating with the API: {str(e)}"}
90
 
91
  def plot_risk(potential_cancer_types):
92
  if not potential_cancer_types:
 
109
 
110
  return fig
111
 
112
+ def format_output(response):
113
+ if "error" in response:
114
+ return f"Error: {response['error']}"
115
+
116
+ output = "HealthScan AI: Personalized Cancer Risk Insights\n\n"
117
+
118
+ output += "Potential Cancer Types:\n"
119
+ for cancer in response.get("potential_cancer_types", []):
120
+ output += f"- {cancer.get('name', 'N/A')} (Risk Level: {cancer.get('risk_level', 'N/A')})\n"
121
+ output += f" {cancer.get('description', 'No description provided.')}\n\n"
122
+
123
+ output += "Recommended Tests:\n"
124
+ for test in response.get("recommended_tests", []):
125
+ output += f"- {test.get('name', 'N/A')}: {test.get('description', 'No description provided.')}\n\n"
126
+
127
+ output += f"General Advice:\n{response.get('general_advice', 'No general advice provided.')}\n\n"
128
+ output += f"DISCLAIMER:\n{response.get('disclaimer', 'No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice.')}"
129
+
130
+ return output
131
+
132
+ def validate_input(age, gender, symptoms, medical_history):
133
+ errors = []
134
+ if not (0 < age < 120):
135
+ errors.append("Please enter a valid age between 1 and 120.")
136
+ if not symptoms.strip():
137
+ errors.append("Please enter at least one symptom.")
138
+ return errors
139
+
140
  def process_input(age, gender, symptoms, medical_history):
141
  errors = validate_input(age, gender, symptoms, medical_history)
142
  if errors: