import os import gradio as gr import google.generativeai as genai # Configure the Gemini API with environment variable GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") if not GOOGLE_API_KEY: raise ValueError("GOOGLE_API_KEY environment variable not set. Please configure it in the Hugging Face Space settings.") genai.configure(api_key=GOOGLE_API_KEY) # Use Gemini 1.5 Flash model model = genai.GenerativeModel('gemini-1.5-flash-latest') def predict_tumor(headaches, neurological_symptoms, family_history, weight_loss, fatigue, nausea, balance_issues, speech_difficulties, mood_changes, radiation_exposure): # Validate inputs inputs = [headaches, neurological_symptoms, family_history, weight_loss, fatigue, nausea, balance_issues, speech_difficulties, mood_changes, radiation_exposure] if not all(inputs): return "Error: Please provide answers to all questions." # Format user responses user_data = f""" Persistent Headaches: {headaches} Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): {neurological_symptoms} Family History of Cancer/Tumors: {family_history} Unexplained Weight Loss: {weight_loss} Persistent Fatigue: {fatigue} Unexplained Nausea or Vomiting: {nausea} Balance or Coordination Issues: {balance_issues} Speech Difficulties: {speech_difficulties} Personality or Mood Changes: {mood_changes} History of Radiation Exposure: {radiation_exposure} """ prompt = f""" You are a health assistant predicting the likelihood of a tumor (e.g., brain tumor) based on user responses to 10 health questions. Classify the risk as "Tumor Detected" or "No Tumor" and provide a brief reason for your classification. Return the response in this format: Prediction: [Tumor Detected / No Tumor] Reason: [Brief explanation] Examples: User Data: Persistent Headaches: Frequent and severe Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): Yes Family History of Cancer/Tumors: Yes Unexplained Weight Loss: Yes Persistent Fatigue: Yes Unexplained Nausea or Vomiting: Yes Balance or Coordination Issues: Yes Speech Difficulties: Yes Personality or Mood Changes: Yes History of Radiation Exposure: Yes Prediction: Tumor Detected Reason: Multiple symptoms including frequent severe headaches, neurological symptoms, nausea, balance issues, speech difficulties, mood changes, along with family history, weight loss, fatigue, and radiation exposure, strongly suggest a potential tumor. User Data: Persistent Headaches: None Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): No Family History of Cancer/Tumors: No Unexplained Weight Loss: No Persistent Fatigue: No Unexplained Nausea or Vomiting: No Balance or Coordination Issues: No Speech Difficulties: No Personality or Mood Changes: No History of Radiation Exposure: No Prediction: No Tumor Reason: The absence of symptoms such as headaches, neurological issues, nausea, balance problems, speech difficulties, mood changes, and risk factors like family history or radiation exposure suggests a low likelihood of a tumor. User Data: Persistent Headaches: Occasional and mild Neurological Symptoms (seizures, vision/hearing changes, cognitive issues): No Family History of Cancer/Tumors: Yes Unexplained Weight Loss: No Persistent Fatigue: Yes Unexplained Nausea or Vomiting: No Balance or Coordination Issues: No Speech Difficulties: No Personality or Mood Changes: No History of Radiation Exposure: No Prediction: No Tumor Reason: Mild headaches, fatigue, and family history alone, without other significant symptoms like neurological issues or nausea, suggest a low likelihood of a tumor. User Data: {user_data} Prediction: Reason: """ try: response = model.generate_content(prompt) return response.text.strip() except Exception as e: return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/" # Define Gradio interface iface = gr.Interface( fn=predict_tumor, inputs=[ gr.Dropdown(choices=["Frequent and severe", "Occasional and mild", "None"], label="1. Do you experience persistent headaches?"), gr.Dropdown(choices=["Yes", "No"], label="2. Do you have neurological symptoms (e.g., seizures, vision/hearing changes, cognitive issues)?"), gr.Dropdown(choices=["Yes", "No"], label="3. Do you have a family history of cancer or tumors?"), gr.Dropdown(choices=["Yes", "No"], label="4. Have you experienced unexplained weight loss?"), gr.Dropdown(choices=["Yes", "No"], label="5. Do you experience persistent fatigue or weakness?"), gr.Dropdown(choices=["Yes", "No"], label="6. Do you experience unexplained nausea or vomiting?"), gr.Dropdown(choices=["Yes", "No"], label="7. Do you have balance or coordination issues (e.g., difficulty walking)?"), gr.Dropdown(choices=["Yes", "No"], label="8. Do you have speech difficulties (e.g., slurred speech)?"), gr.Dropdown(choices=["Yes", "No"], label="9. Have you noticed personality or mood changes?"), gr.Dropdown(choices=["Yes", "No"], label="10. Have you been exposed to significant radiation (e.g., medical treatments, environmental)?") ], outputs=gr.Textbox(label="Tumor Detection Prediction"), title="Tumor Risk Predictor", description="Answer 10 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." ) # Launch the interface if __name__ == "__main__": iface.launch()