Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import os | |
| import io | |
| import shutil | |
| from PIL import Image | |
| from pathlib import Path | |
| # Define API endpoint | |
| API_URL = "https://nagur-shareef-shaik-retinal-health-diagnostics.hf.space/predict" | |
| ###### Sidebar ###### | |
| # Custom CSS to make the sidebar fixed and adjust width | |
| st.markdown( | |
| """ | |
| <style> | |
| [data-testid="stSidebar"] { | |
| width: 750px !important; /* Adjust width as needed */ | |
| background-color: #f8f9fa; /* Light grey background */ | |
| } | |
| [data-testid="stSidebarNav"] { | |
| width: 750px; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| st.sidebar.header("Intelligent Application for Retinal Diseases Diagnosis") | |
| st.sidebar.image("images/rhd_img_1.jpeg", width=1250) | |
| st.sidebar.markdown(""" | |
| ### Get AI-Powered Diagnosis Report | |
| 1. **Enter Patient Details** | |
| - Name, Age, Gender, Contact Information, Address | |
| - Select Diagnosis Task (**General, Retinopathy, DME, Cataracts**) | |
| - **General:** Diagnoses Diabetic Retinopathy, Cataracts, Glaucoma, & Normal conditions from retinal fundus images. | |
| - **Retinopathy:** Detects 7 retinopathy severity grades (No DR, Mild NPDR, Moderate NPDR, Severe NPDR, Very Severe NPDR, PDR, and Advanced PDR) from retinal scans. | |
| - **DME:** Identifies the presence of Diabetic Macular Edema (DME) from retinal scans. | |
| - **Cataracts:** Diagnoses Cataracts from digital eye images. | |
| - Add Medical Notes (if any) | |
| 2. **Upload Retinal Scan** | |
| - Accepts **JPG, JPEG, PNG** formats | |
| - Ensure clear, high-quality images | |
| 3. **Submit for AI Analysis** | |
| - Click **"Submit Request"** to send details to AI model | |
| - AI processes the image and generates a diagnosis | |
| 4. **View Results Instantly** | |
| - AI-powered diagnosis displayed with insights | |
| - Download or share the report if needed | |
| """) | |
| # Create two columns | |
| col1, col2 = st.columns([1, 3]) # Adjust the width ratio as needed | |
| with col1: | |
| st.image("images/rhd-logo.png", width=300) # Adjust width as needed | |
| with col2: | |
| st.title("Retinal Health Diagnostics") | |
| # User input fields | |
| task = st.selectbox("Select Task", ["general", "retinopathy", "dme", "cataracts"], index=0) | |
| name = st.text_input("Name") | |
| a, b, c = st.columns([1, 1, 1]) | |
| with a: | |
| age = st.text_input("Age") | |
| with b: | |
| gender = st.selectbox("Gender", ["Male", "Female", "Other"], index=0) | |
| with c: | |
| phone = st.text_input("Phone") | |
| email = st.text_input("Email") | |
| address = st.text_area("Address") | |
| med_notes = st.text_area("Medical Notes") | |
| # Image upload | |
| uploaded_file = st.file_uploader("Upload Retinal Image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file: | |
| # Convert file to bytes | |
| files = {"file": (uploaded_file.name, uploaded_file, uploaded_file.type)} | |
| # Submit button | |
| if st.button("Submit Request"): | |
| if not uploaded_file: | |
| st.error("Please upload an image.") | |
| else: | |
| # Create request payload | |
| payload = { | |
| "task": task, | |
| "name": name, | |
| "age": age, | |
| "gender": gender, | |
| "address": address, | |
| "phone": phone, | |
| "email": email, | |
| "med_notes": med_notes, | |
| } | |
| try: | |
| response = requests.post(API_URL, files=files, data=payload) | |
| if response.status_code == 200: | |
| st.success("Diagnosis Complete!") | |
| # Extract image from response | |
| img_bytes = response.content # Get image as bytes | |
| # Convert bytes to PIL Image | |
| image = Image.open(io.BytesIO(img_bytes)) | |
| # Get response metadata | |
| predicted_class = response.headers.get('Predicted-Class') | |
| confidence = response.headers.get('Confidence') | |
| st.markdown( | |
| f""" | |
| <div style="padding: 15px; border-radius: 10px; background-color: #f0f2f6; text-align: center;"> | |
| <h3 style="color: #4CAF50;">Diagnosis Result</h3> | |
| <p style="font-size: 18px;"><strong>Findings:</strong> <span style="color: #FF5722;">{predicted_class}</span></p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| st.image(image) | |
| # st.json(response.json()) | |
| else: | |
| st.error(f"Error {response.status_code}: {response.text}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Request failed: {e}") | |