ciaochris commited on
Commit
5012a88
·
verified ·
1 Parent(s): 2a7768e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -177
app.py CHANGED
@@ -8,190 +8,52 @@ import plotly.graph_objs as go
8
  from typing import List, Dict, Any
9
  import logging
10
 
11
- # Set up logging
12
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
 
14
- # Load environment variables
15
- load_dotenv()
16
-
17
- # Initialize Groq client
18
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
19
-
20
- def parse_non_json_response(text: str) -> Dict[str, Any]:
21
- # Attempt to extract structured information from non-JSON text
22
- cancer_types = re.findall(r"(?:cancer type|Cancer Type):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
23
- risk_levels = re.findall(r"(?:risk level|Risk Level):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
24
- descriptions = re.findall(r"(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
25
-
26
- potential_cancer_types = [
27
- {"name": ct, "risk_level": rl, "description": desc}
28
- for ct, rl, desc in zip(cancer_types, risk_levels, descriptions)
29
- ]
30
-
31
- recommended_tests = re.findall(r"(?:Test Name):\s*(.+?)\n(?:description|Description):\s*(.+?)(?:\n|$)", text, re.IGNORECASE)
32
- recommended_tests = [{"name": name, "description": desc} for name, desc in recommended_tests]
33
-
34
- general_advice = re.search(r"(?:General Advice|general advice):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
35
- general_advice = general_advice.group(1) if general_advice else "No general advice provided."
36
-
37
- disclaimer = re.search(r"(?:DISCLAIMER|Disclaimer):\s*(.+?)(?:\n|$)", text, re.DOTALL | re.IGNORECASE)
38
- disclaimer = disclaimer.group(1) if disclaimer else "No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice."
39
-
40
- return {
41
- "potential_cancer_types": potential_cancer_types,
42
- "recommended_tests": recommended_tests,
43
- "general_advice": general_advice,
44
- "disclaimer": disclaimer
45
- }
46
 
47
- def get_diagnosis(age: int, gender: str, symptoms: str, medical_history: str) -> Dict[str, Any]:
48
- prompt = f"""
49
- Given the following patient information, provide a preliminary analysis of potential cancer risks and recommended tests.
50
- Give professional medical advice to the best of your ability.
51
-
52
- Patient Information:
53
- Age: {age}
54
- Gender: {gender}
55
- Symptoms: {symptoms}
56
- Medical History: {medical_history}
57
-
58
- Please provide a response with the following structure:
59
- Potential Cancer Types:
60
- - Cancer Type: [Name]
61
- Risk Level: [Low/Medium/High]
62
- Description: [Brief description of why this cancer type is considered]
63
- Recommended Tests:
64
- - Test Name: [Name]
65
- Description: [Brief description of why this test is recommended]
66
- General Advice: [General health advice for the patient]
67
- DISCLAIMER: [A strong disclaimer about the limitations of this assessment]
68
-
69
- Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
70
- """
71
-
72
- try:
73
- chat_completion = client.chat.completions.create(
74
- messages=[
75
- {
76
- "role": "user",
77
- "content": prompt,
78
- }
79
- ],
80
- model="llama-3.1-8b-instant",
81
- temperature=0.5,
82
- max_tokens=1500,
83
- )
84
-
85
- response_content = chat_completion.choices[0].message.content
86
-
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:
94
- logging.error(f"Error in get_diagnosis: {str(e)}")
95
- return {"error": f"An error occurred while communicating with the API: {str(e)}"}
96
-
97
- def plot_risk(potential_cancer_types: List[Dict[str, str]]) -> go.Figure:
98
- if not potential_cancer_types:
99
- return None
100
-
101
- names = [c["name"] for c in potential_cancer_types]
102
- risk_levels = [1 if c["risk_level"].lower() == "low" else 2 if c["risk_level"].lower() == "medium" else 3 for c in potential_cancer_types]
103
- colors = ["green" if rl == 1 else "yellow" if rl == 2 else "red" for rl in risk_levels]
104
-
105
- fig = go.Figure(data=[go.Bar(
106
- x=names,
107
- y=risk_levels,
108
- marker_color=colors,
109
- text=risk_levels,
110
- textposition='auto',
111
- )])
112
- fig.update_layout(
113
- title="Cancer Risk Levels",
114
- yaxis_title="Risk Level",
115
- xaxis_tickangle=-45,
116
- yaxis=dict(
117
- tickmode='array',
118
- tickvals=[1, 2, 3],
119
- ticktext=['Low', 'Medium', 'High']
120
- )
121
- )
122
-
123
- return fig
124
-
125
- def format_output(response: Dict[str, Any]) -> str:
126
- if "error" in response:
127
- return f"Error: {response['error']}"
128
-
129
- output = "HealthScan AI: Personalized Cancer Risk Insights\n\n"
130
-
131
- output += "Potential Cancer Types:\n"
132
- for cancer in response.get("potential_cancer_types", []):
133
- output += f"- {cancer.get('name', 'N/A')} (Risk Level: {cancer.get('risk_level', 'N/A')})\n"
134
- output += f" {cancer.get('description', 'No description provided.')}\n\n"
135
 
136
- output += "Recommended Tests:\n"
137
- for test in response.get("recommended_tests", []):
138
- output += f"- {test.get('name', 'N/A')}: {test.get('description', 'No description provided.')}\n\n"
139
 
140
- output += f"General Advice:\n{response.get('general_advice', 'No general advice provided.')}\n\n"
141
- output += f"DISCLAIMER:\n{response.get('disclaimer', 'No disclaimer provided. This tool is for educational purposes only and should not replace professional medical advice.')}"
142
-
143
- return output
144
-
145
- def validate_input(age: int, gender: str, symptoms: str, medical_history: str) -> List[str]:
146
- errors = []
147
- if not (0 < age < 120):
148
- errors.append("Please enter a valid age between 1 and 120.")
149
- if not symptoms.strip():
150
- errors.append("Please enter at least one symptom.")
151
- return errors
152
-
153
- def process_input(age: int, gender: str, symptoms: str, medical_history: str) -> tuple:
154
- errors = validate_input(age, gender, symptoms, medical_history)
155
- if errors:
156
- return "\n".join(errors), None
157
-
158
- diagnosis = get_diagnosis(age, gender, symptoms, medical_history)
159
- output = format_output(diagnosis)
160
 
161
- risk_plot = plot_risk(diagnosis.get("potential_cancer_types", []))
 
 
 
 
162
 
163
- return output, risk_plot
164
-
165
- def clear_inputs():
166
- return gr.Number(value=None), gr.Radio(value=None), gr.Textbox(value=""), gr.Textbox(value="")
167
-
168
- # Create Gradio interface
169
- iface = gr.Interface(
170
- fn=process_input,
171
- inputs=[
172
- gr.Number(label="Age"),
173
- gr.Radio(["Male", "Female", "Other"], label="Gender"),
174
- gr.Textbox(lines=3, label="Symptoms (separated by commas)"),
175
- gr.Textbox(lines=3, label="Relevant Medical History")
176
- ],
177
- outputs=[
178
- gr.Textbox(label="Assessment Results", lines=10),
179
- gr.Plot(label="Cancer Risk Levels")
180
- ],
181
- title="Vers3Dynamics HealthScan: Personalized Cancer Risk Insights",
182
- description="This Groq-powered tool provides a preliminary analysis of potential cancer risks based on the information you provide. It is designed to support early awareness and is not a substitute for professional medical advice, diagnosis, or treatment.",
183
- article="IMPORTANT: HealthScan AI is for educational and informational purposes only. Always consult with a qualified healthcare provider for medical concerns. The insights provided by this tool should not be used for self-diagnosis or treatment. Early detection and regular check-ups with healthcare professionals are crucial for managing your health effectively.",
184
- examples=[
185
- [45, "Male", "Persistent cough, weight loss", "Family history of lung cancer"],
186
- [35, "Female", "Unexplained fatigue, bruising easily", "No significant medical history"],
187
- [60, "Other", "Blood in stool, abdominal pain", "History of inflammatory bowel disease"]
188
- ],
189
- theme=gr.themes.Soft()
190
- )
191
-
192
- # Add a clear button
193
- clear_button = gr.Button("Clear Inputs")
194
- clear_button.click(fn=clear_inputs, inputs=[], outputs=[iface.inputs[0], iface.inputs[1], iface.inputs[2], iface.inputs[3]])
195
 
196
  # Launch the app
197
  if __name__ == "__main__":
 
8
  from typing import List, Dict, Any
9
  import logging
10
 
11
+ # ... (previous code remains unchanged)
 
12
 
13
+ def clear_inputs():
14
+ return gr.Number(value=None), gr.Radio(value=None), gr.Textbox(value=""), gr.Textbox(value="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Create Gradio interface
17
+ with gr.Blocks(theme=gr.themes.Soft()) as iface:
18
+ gr.Markdown("# Vers3Dynamics HealthScan: Personalized Cancer Risk Insights")
19
+ gr.Markdown("This Groq-powered tool provides a preliminary analysis of potential cancer risks based on the information you provide. It is designed to support early awareness and is not a substitute for professional medical advice, diagnosis, or treatment.")
20
+
21
+ with gr.Row():
22
+ with gr.Column():
23
+ age_input = gr.Number(label="Age")
24
+ gender_input = gr.Radio(["Male", "Female", "Other"], label="Gender")
25
+ symptoms_input = gr.Textbox(lines=3, label="Symptoms (separated by commas)")
26
+ medical_history_input = gr.Textbox(lines=3, label="Relevant Medical History")
27
+ submit_button = gr.Button("Submit")
28
+ clear_button = gr.Button("Clear Inputs")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ with gr.Column():
31
+ output_text = gr.Textbox(label="Assessment Results", lines=10)
32
+ output_plot = gr.Plot(label="Cancer Risk Levels")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ gr.Markdown("## IMPORTANT")
35
+ gr.Markdown("HealthScan AI is for educational and informational purposes only. Always consult with a qualified healthcare provider for medical concerns. The insights provided by this tool should not be used for self-diagnosis or treatment. Early detection and regular check-ups with healthcare professionals are crucial for managing your health effectively.")
 
36
 
37
+ submit_button.click(
38
+ fn=process_input,
39
+ inputs=[age_input, gender_input, symptoms_input, medical_history_input],
40
+ outputs=[output_text, output_plot]
41
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ clear_button.click(
44
+ fn=clear_inputs,
45
+ inputs=[],
46
+ outputs=[age_input, gender_input, symptoms_input, medical_history_input]
47
+ )
48
 
49
+ gr.Examples(
50
+ examples=[
51
+ [45, "Male", "Persistent cough, weight loss", "Family history of lung cancer"],
52
+ [35, "Female", "Unexplained fatigue, bruising easily", "No significant medical history"],
53
+ [60, "Other", "Blood in stool, abdominal pain", "History of inflammatory bowel disease"]
54
+ ],
55
+ inputs=[age_input, gender_input, symptoms_input, medical_history_input]
56
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Launch the app
59
  if __name__ == "__main__":