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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -114
app.py CHANGED
@@ -6,90 +6,7 @@ from dotenv import load_dotenv
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
-
59
- Recommended Tests:
60
- - Test Name: [Name]
61
- Description: [Brief description of why this test is recommended]
62
-
63
- General Advice: [General health advice for the patient]
64
-
65
- DISCLAIMER: [A strong disclaimer about the limitations of this assessment]
66
-
67
- Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
68
- """
69
-
70
- try:
71
- chat_completion = client.chat.completions.create(
72
- messages=[
73
- {
74
- "role": "user",
75
- "content": prompt,
76
- }
77
- ],
78
- model="llama-3.1-8b-instant", # Updated to a valid Groq model
79
- temperature=0.5,
80
- max_tokens=1500,
81
- )
82
-
83
- response_content = chat_completion.choices[0].message.content
84
-
85
- try:
86
- response = json.loads(response_content)
87
- except json.JSONDecodeError:
88
- response = parse_non_json_response(response_content)
89
-
90
- return response
91
- except Exception as e:
92
- return {"error": f"An error occurred while communicating with the API: {str(e)}"}
93
 
94
  def plot_risk(potential_cancer_types):
95
  if not potential_cancer_types:
@@ -112,34 +29,6 @@ def plot_risk(potential_cancer_types):
112
 
113
  return fig
114
 
115
- def format_output(response):
116
- if "error" in response:
117
- return f"Error: {response['error']}"
118
-
119
- output = "HealthScan AI: Personalized Cancer Risk Insights\n\n"
120
-
121
- output += "Potential Cancer Types:\n"
122
- for cancer in response.get("potential_cancer_types", []):
123
- output += f"- {cancer.get('name', 'N/A')} (Risk Level: {cancer.get('risk_level', 'N/A')})\n"
124
- output += f" {cancer.get('description', 'No description provided.')}\n\n"
125
-
126
- output += "Recommended Tests:\n"
127
- for test in response.get("recommended_tests", []):
128
- output += f"- {test.get('name', 'N/A')}: {test.get('description', 'No description provided.')}\n\n"
129
-
130
- output += f"General Advice:\n{response.get('general_advice', 'No general advice provided.')}\n\n"
131
- output += f"DISCLAIMER:\n{response.get('disclaimer', 'No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice.')}"
132
-
133
- return output
134
-
135
- def validate_input(age, gender, symptoms, medical_history):
136
- errors = []
137
- if not (0 < age < 120):
138
- errors.append("Please enter a valid age between 1 and 120.")
139
- if not symptoms.strip():
140
- errors.append("Please enter at least one symptom.")
141
- return errors
142
-
143
  def process_input(age, gender, symptoms, medical_history):
144
  errors = validate_input(age, gender, symptoms, medical_history)
145
  if errors:
@@ -172,5 +61,4 @@ iface = gr.Interface(
172
 
173
  # Launch the app
174
  if __name__ == "__main__":
175
- iface.launch()
176
-
 
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
 
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:
 
61
 
62
  # Launch the app
63
  if __name__ == "__main__":
64
+ iface.launch()