Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Placeholder for your Gemini API key - store securely!
|
7 |
+
# st.secrets is the recommended way to manage secrets in Streamlit Cloud and Hugging Face Spaces
|
8 |
+
GEMINI_API_KEY = st.secrets.get("GEMINI_API_KEY")
|
9 |
+
|
10 |
+
if GEMINI_API_KEY:
|
11 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
12 |
+
model = genai.GenerativeModel('gemini-pro')
|
13 |
+
vision_model = genai.GenerativeModel('gemini-pro-vision')
|
14 |
+
else:
|
15 |
+
st.error("Please add your Gemini API key to the Streamlit secrets manager or environment variables.")
|
16 |
+
st.stop()
|
17 |
+
|
18 |
+
def diagnose_text(text_input):
|
19 |
+
try:
|
20 |
+
response = model.generate_content(f"Analyze the following medical information and provide a potential diagnosis and risk assessment: {text_input}")
|
21 |
+
return response.text
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error during diagnosis: {e}"
|
24 |
+
|
25 |
+
def diagnose_image(image_file, text_prompt="Analyze this medical image for potential anomalies and provide a diagnosis:"):
|
26 |
+
try:
|
27 |
+
image = Image.open(image_file)
|
28 |
+
response = vision_model.generate_content([text_prompt, image])
|
29 |
+
return response.text
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error during image analysis: {e}"
|
32 |
+
|
33 |
+
def main():
|
34 |
+
st.title("Agentic AI for Automated Disease Diagnosis")
|
35 |
+
st.subheader("Powered by Google Gemini")
|
36 |
+
|
37 |
+
data_type = st.radio("Select Input Type:",)
|
38 |
+
|
39 |
+
if data_type == "Text Description":
|
40 |
+
text_input = st.text_area("Enter medical data or symptoms:", height=200)
|
41 |
+
if st.button("Diagnose"):
|
42 |
+
if text_input:
|
43 |
+
with st.spinner("Analyzing..."):
|
44 |
+
diagnosis = diagnose_text(text_input)
|
45 |
+
st.subheader("Potential Diagnosis and Risk Assessment:")
|
46 |
+
st.write(diagnosis)
|
47 |
+
else:
|
48 |
+
st.warning("Please enter some medical data or symptoms.")
|
49 |
+
|
50 |
+
elif data_type == "Medical Image":
|
51 |
+
image_file = st.file_uploader("Upload a medical image (e.g., X-ray, CT scan, MRI):", type=["png", "jpg", "jpeg"])
|
52 |
+
text_prompt = st.text_input("Optional: Add a description or specific instructions for the image analysis:", "")
|
53 |
+
if st.button("Diagnose Image"):
|
54 |
+
if image_file:
|
55 |
+
with st.spinner("Analyzing image..."):
|
56 |
+
diagnosis = diagnose_image(image_file, text_prompt)
|
57 |
+
st.subheader("Potential Diagnosis based on Image:")
|
58 |
+
st.write(diagnosis)
|
59 |
+
st.image(image_file, caption="Uploaded Medical Image.", use_column_width=True)
|
60 |
+
else:
|
61 |
+
st.warning("Please upload a medical image.")
|
62 |
+
|
63 |
+
st.info("Disclaimer: This application provides potential diagnoses based on AI analysis and should not be considered a substitute for professional medical advice. Always consult with a qualified healthcare provider for any health concerns.")
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
main()
|