hashirlodhi commited on
Commit
e554ae6
·
verified ·
1 Parent(s): d3c1222

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+
5
+ # Configure the Gemini API with environment variable
6
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
7
+ if not GOOGLE_API_KEY:
8
+ raise ValueError("GOOGLE_API_KEY environment variable not set. Please configure it in the Hugging Face Space settings.")
9
+
10
+ genai.configure(api_key=GOOGLE_API_KEY)
11
+
12
+ # Use Gemini 1.5 Flash model
13
+ model = genai.GenerativeModel('gemini-1.5-flash-latest')
14
+
15
+ def predict_tumor(headaches, neurological_symptoms, family_history, weight_loss, fatigue):
16
+ # Validate inputs
17
+ inputs = [headaches, neurological_symptoms, family_history, weight_loss, fatigue]
18
+ if not all(inputs):
19
+ return "Error: Please provide answers to all questions."
20
+
21
+ # Format user responses
22
+ user_data = f"""
23
+ Persistent Headaches: {headaches}
24
+ Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): {neurological_symptoms}
25
+ Family History of Cancer/Tumors: {family_history}
26
+ Unexplained Weight Loss: {weight_loss}
27
+ Persistent Fatigue: {fatigue}
28
+ """
29
+
30
+ prompt = f"""
31
+ You are a health assistant predicting the likelihood of a tumor (e.g., brain tumor) based on user responses to five health questions.
32
+ Classify the risk as "Tumor Detected" or "No Tumor" and provide a brief reason for your classification.
33
+ Return the response in this format:
34
+
35
+ Prediction: [Tumor Detected / No Tumor]
36
+ Reason: [Brief explanation]
37
+
38
+ Examples:
39
+ User Data:
40
+ Persistent Headaches: Frequent and severe
41
+ Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): Yes
42
+ Family History of Cancer/Tumors: Yes
43
+ Unexplained Weight Loss: Yes
44
+ Persistent Fatigue: Yes
45
+ Prediction: Tumor Detected
46
+ Reason: Frequent severe headaches, neurological symptoms, family history, weight loss, and fatigue are strong indicators of a potential tumor.
47
+
48
+ User Data:
49
+ Persistent Headaches: None
50
+ Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): No
51
+ Family History of Cancer/Tumors: No
52
+ Unexplained Weight Loss: No
53
+ Persistent Fatigue: No
54
+ Prediction: No Tumor
55
+ Reason: The absence of headaches, neurological symptoms, family history, weight loss, and fatigue suggests a low likelihood of a tumor.
56
+
57
+ User Data:
58
+ Persistent Headaches: Occasional and mild
59
+ Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): No
60
+ Family History of Cancer/Tumors: Yes
61
+ Unexplained Weight Loss: No
62
+ Persistent Fatigue: Yes
63
+ Prediction: No Tumor
64
+ Reason: Mild headaches, no neurological symptoms, and no weight loss reduce the likelihood of a tumor, despite family history and fatigue.
65
+
66
+ User Data:
67
+ {user_data}
68
+ Prediction:
69
+ Reason:
70
+ """
71
+
72
+ try:
73
+ response = model.generate_content(prompt)
74
+ return response.text.strip()
75
+ except Exception as e:
76
+ return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/"
77
+
78
+ # Define Gradio interface
79
+ iface = gr.Interface(
80
+ fn=predict_tumor,
81
+ inputs=[
82
+ gr.Dropdown(choices=["Frequent and severe", "Occasional and mild", "None"], label="1. Do you experience persistent headaches?"),
83
+ gr.Dropdown(choices=["Yes", "No"], label="2. Do you have neurological symptoms (e.g., seizures, vision/hearing changes, cognitive issues)?"),
84
+ gr.Dropdown(choices=["Yes", "No"], label="3. Do you have a family history of cancer or tumors?"),
85
+ gr.Dropdown(choices=["Yes", "No"], label="4. Have you experienced unexplained weight loss?"),
86
+ gr.Dropdown(choices=["Yes", "No"], label="5. Do you experience persistent fatigue or weakness?")
87
+ ],
88
+ outputs=gr.Textbox(label="Tumor Detection Prediction"),
89
+ title="Tumor Risk Predictor",
90
+ description="Answer five health questions to predict the likelihood of a tumor using the Gemini API. Set your GOOGLE_API_KEY in the Hugging Face Space settings. Note: This is for demonstration purposes only and not a clinical tool."
91
+ )
92
+
93
+ # Launch the interface
94
+ if __name__ == "__main__":
95
+ iface.launch()