File size: 3,615 Bytes
8b3cbd2
 
0b5729b
9ef77aa
f8b54f3
3644112
8b3cbd2
23863d5
401e8d2
 
8b3cbd2
 
 
 
 
 
 
 
 
 
 
 
3644112
 
 
 
 
 
 
 
 
 
 
 
 
43c6ee7
 
 
 
 
 
 
 
 
 
 
 
 
 
3644112
43c6ee7
 
 
 
 
 
 
 
 
9ef77aa
23863d5
8b3cbd2
 
3644112
f8b54f3
2b9a5dc
f8b54f3
 
 
 
 
 
 
3644112
f8b54f3
 
 
3644112
43c6ee7
 
f8b54f3
 
43c6ee7
 
 
 
f8b54f3
0a2cbb7
43c6ee7
2b9a5dc
 
 
43c6ee7
 
72935fc
7f14f70
3644112
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.")