ciaochris commited on
Commit
6c17691
·
verified ·
1 Parent(s): d21926a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -25
app.py CHANGED
@@ -1,50 +1,106 @@
1
  import gradio as gr
2
  import os
3
  from groq import Groq
 
 
 
 
 
4
 
5
  # Initialize Groq client
6
  client = Groq(
7
  api_key=os.environ.get("GROQ_API_KEY"),
8
  )
9
 
10
- def get_diagnosis(symptoms):
11
  # IMPORTANT: This is a simplified example and should not be used for actual medical diagnosis
12
  prompt = f"""
13
- Given the following symptoms, provide a preliminary analysis of potential cancer risks.
14
  This is not a definitive diagnosis and should not replace professional medical advice.
15
 
 
 
 
16
  Symptoms: {symptoms}
 
17
 
18
- Please provide:
19
- 1. Potential cancer types that might be associated with these symptoms
20
- 2. Recommendations for further medical tests or consultations
21
- 3. General health advice
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- Remember to emphasize the importance of consulting with a medical professional for accurate diagnosis and treatment.
24
  """
25
 
26
- chat_completion = client.chat.completions.create(
27
- messages=[
28
- {
29
- "role": "user",
30
- "content": prompt,
31
- }
32
- ],
33
- model="mixtral-8x7b-32768",
34
- temperature=0.5,
35
- max_tokens=1000,
36
- )
 
 
 
 
 
 
 
 
 
 
37
 
38
- return chat_completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  # Create Gradio interface
41
  iface = gr.Interface(
42
- fn=get_diagnosis,
43
- inputs=gr.Textbox(lines=5, label="Enter symptoms (separated by commas)"),
44
- outputs="text",
 
 
 
 
 
45
  title="Cancer Risk Assessment Tool",
46
- description="This tool provides a preliminary analysis based on symptoms. It is not a substitute for professional medical advice, diagnosis, or treatment.",
 
47
  )
48
 
49
- # Launch the app
50
- iface.launch()
 
1
  import gradio as gr
2
  import os
3
  from groq import Groq
4
+ import json
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
 
10
  # Initialize Groq client
11
  client = Groq(
12
  api_key=os.environ.get("GROQ_API_KEY"),
13
  )
14
 
15
+ def get_diagnosis(age, gender, symptoms, medical_history):
16
  # IMPORTANT: This is a simplified example and should not be used for actual medical diagnosis
17
  prompt = f"""
18
+ Given the following patient information, provide a preliminary analysis of potential cancer risks.
19
  This is not a definitive diagnosis and should not replace professional medical advice.
20
 
21
+ Patient Information:
22
+ Age: {age}
23
+ Gender: {gender}
24
  Symptoms: {symptoms}
25
+ Medical History: {medical_history}
26
 
27
+ Please provide a JSON response with the following structure:
28
+ {{
29
+ "potential_cancer_types": [
30
+ {{
31
+ "name": "Cancer Type",
32
+ "risk_level": "Low/Medium/High",
33
+ "description": "Brief description of why this cancer type is considered"
34
+ }}
35
+ ],
36
+ "recommended_tests": [
37
+ {{
38
+ "name": "Test Name",
39
+ "description": "Brief description of why this test is recommended"
40
+ }}
41
+ ],
42
+ "general_advice": "General health advice for the patient",
43
+ "disclaimer": "A strong disclaimer about the limitations of this assessment"
44
+ }}
45
 
46
+ Ensure the response emphasizes the importance of consulting with a medical professional for accurate diagnosis and treatment.
47
  """
48
 
49
+ try:
50
+ chat_completion = client.chat.completions.create(
51
+ messages=[
52
+ {
53
+ "role": "user",
54
+ "content": prompt,
55
+ }
56
+ ],
57
+ model="mixtral-8x7b-32768",
58
+ temperature=0.5,
59
+ max_tokens=1500,
60
+ )
61
+
62
+ response = json.loads(chat_completion.choices[0].message.content)
63
+ return response
64
+ except Exception as e:
65
+ return {"error": str(e)}
66
+
67
+ def format_output(response):
68
+ if "error" in response:
69
+ return f"An error occurred: {response['error']}"
70
 
71
+ output = "Cancer Risk Assessment Results\n\n"
72
+
73
+ output += "Potential Cancer Types:\n"
74
+ for cancer in response["potential_cancer_types"]:
75
+ output += f"- {cancer['name']} (Risk Level: {cancer['risk_level']})\n {cancer['description']}\n\n"
76
+
77
+ output += "Recommended Tests:\n"
78
+ for test in response["recommended_tests"]:
79
+ output += f"- {test['name']}: {test['description']}\n\n"
80
+
81
+ output += f"General Advice:\n{response['general_advice']}\n\n"
82
+ output += f"DISCLAIMER:\n{response['disclaimer']}"
83
+
84
+ return output
85
+
86
+ def process_input(age, gender, symptoms, medical_history):
87
+ diagnosis = get_diagnosis(age, gender, symptoms, medical_history)
88
+ return format_output(diagnosis)
89
 
90
  # Create Gradio interface
91
  iface = gr.Interface(
92
+ fn=process_input,
93
+ inputs=[
94
+ gr.Number(label="Age"),
95
+ gr.Radio(["Male", "Female", "Other"], label="Gender"),
96
+ gr.Textbox(lines=3, label="Symptoms (separated by commas)"),
97
+ gr.Textbox(lines=3, label="Relevant Medical History")
98
+ ],
99
+ outputs=gr.Textbox(label="Assessment Results", lines=10),
100
  title="Cancer Risk Assessment Tool",
101
+ description="This tool provides a preliminary analysis based on provided information. It is not a substitute for professional medical advice, diagnosis, or treatment.",
102
+ article="IMPORTANT: This application is for educational purposes only. Always consult with a qualified healthcare provider for medical concerns. The information provided by this tool should not be used for self-diagnosis or treatment."
103
  )
104
 
105
+ # Add usage tracking (for demonstration purposes only, ensure compliance with privacy laws in practice)
106
+ iface.launch(analytics_enabled=True)