Spaces:
Sleeping
Sleeping
import streamlit as st | |
import fitz # PyMuPDF | |
from gtts import gTTS | |
import tempfile | |
import os | |
import requests | |
# Streamlit page setup | |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺") | |
st.title("🩺 Health Report Analyzer") | |
# Upload PDF | |
uploaded_file = st.file_uploader("Upload a Health Report (PDF only)", type="pdf") | |
# Extract text from PDF | |
def extract_text(file): | |
doc = fitz.open(stream=file.read(), filetype="pdf") | |
text = "" | |
for page in doc: | |
text += page.get_text() | |
return text | |
# Ask medical question using Hugging Face Inference API (BioGPT) | |
API_URL = "https://api-inference.huggingface.co/models/microsoft/BioGPT-Large" | |
headers = {"Content-Type": "application/json"} | |
def ask_medical_question(question): | |
payload = { | |
"inputs": f"Explain this medical concept in detail for a general audience: {question}" | |
} | |
response = requests.post(API_URL, headers=headers, json=payload) | |
if response.status_code == 200: | |
return response.json()[0]['generated_text'] | |
else: | |
return "⚠️ Sorry, something went wrong while contacting the medical AI." | |
# Doctor recommendation logic | |
def suggest_doctor(term): | |
term = term.lower() | |
if "kidney" in term or "creatinine" in term: | |
return "You should consider seeing a **nephrologist**." | |
elif "hemoglobin" in term: | |
return "You might want to consult a **hematologist** if your hemoglobin is outside the normal range." | |
elif "liver" in term or "bilirubin" in term: | |
return "Consult a **hepatologist** for liver-related concerns." | |
elif "sugar" in term or "glucose" in term: | |
return "A **diabetologist** or **endocrinologist** is recommended for sugar level issues." | |
return "Please consult a **general physician** for further evaluation." | |
# Basic reference ranges | |
def get_reference_range(term): | |
term = term.lower() | |
if "hemoglobin" in term: | |
return "Normal range: 13.8 to 17.2 g/dL for men, 12.1 to 15.1 g/dL for women." | |
elif "creatinine" in term: | |
return "Normal range: 0.74 to 1.35 mg/dL for men, 0.59 to 1.04 mg/dL for women." | |
elif "bilirubin" in term: | |
return "Normal range: 0.1 to 1.2 mg/dL." | |
return "Range data not available for this term." | |
# Main logic | |
if uploaded_file: | |
text_data = extract_text(uploaded_file) | |
st.success("✅ Health Report Uploaded Successfully!") | |
# Display the report text | |
st.markdown("### 📄 Health Report Content") | |
st.write(text_data) | |
st.session_state['report_text'] = text_data | |
st.subheader("💬 Ask About Any Medical Term or Part of the Report") | |
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):", key="question") | |
if st.button("Get AI Explanation") and user_question: | |
with st.spinner("Thinking..."): | |
explanation = ask_medical_question(user_question) | |
range_info = get_reference_range(user_question) | |
suggestion = suggest_doctor(user_question) | |
st.success("Explanation:") | |
st.write(explanation) | |
st.info(f"📏 Reference Range: {range_info}") | |
st.warning(f"👨⚕️ Doctor Suggestion: {suggestion}") | |
# Text-to-speech using gTTS | |
tts = gTTS(text=f"{explanation}. {range_info}. {suggestion}") | |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
tts.save(temp_audio.name) | |
with open(temp_audio.name, 'rb') as audio_file: | |
st.audio(audio_file.read(), format='audio/mp3') | |
else: | |
st.info("📂 Upload a PDF Health Report to begin.") | |