kseth9852 commited on
Commit
3644112
·
verified ·
1 Parent(s): 7646c4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -3,7 +3,7 @@ import fitz # PyMuPDF
3
  from gtts import gTTS
4
  import tempfile
5
  import os
6
- from transformers import pipeline
7
 
8
  # Streamlit page setup
9
  st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
@@ -20,10 +20,19 @@ def extract_text(file):
20
  text += page.get_text()
21
  return text
22
 
23
- # Load BioGPT model for detailed medical explanation
24
- @st.cache_resource
25
- def load_medical_explainer():
26
- return pipeline("text2text-generation", model="microsoft/BioGPT-Large")
 
 
 
 
 
 
 
 
 
27
 
28
  # Doctor recommendation logic
29
  def suggest_doctor(term):
@@ -38,7 +47,7 @@ def suggest_doctor(term):
38
  return "A **diabetologist** or **endocrinologist** is recommended for sugar level issues."
39
  return "Please consult a **general physician** for further evaluation."
40
 
41
- # Normal ranges (very basic examples)
42
  def get_reference_range(term):
43
  term = term.lower()
44
  if "hemoglobin" in term:
@@ -52,24 +61,21 @@ def get_reference_range(term):
52
  # Main logic
53
  if uploaded_file:
54
  text_data = extract_text(uploaded_file)
55
- st.success("Health Report Uploaded Successfully!")
56
 
57
  # Display the report text
58
  st.markdown("### 📄 Health Report Content")
59
  st.write(text_data)
60
 
61
- explainer = load_medical_explainer()
62
  st.session_state['report_text'] = text_data
63
 
64
  st.subheader("💬 Ask About Any Medical Term or Part of the Report")
65
 
66
- user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
67
 
68
  if st.button("Get AI Explanation") and user_question:
69
  with st.spinner("Thinking..."):
70
- prompt = f"Explain this medical concept in detail for a general audience: {user_question}"
71
- response = explainer(prompt, max_length=512)[0]['generated_text']
72
- explanation = response
73
  range_info = get_reference_range(user_question)
74
  suggestion = suggest_doctor(user_question)
75
 
@@ -88,4 +94,4 @@ if uploaded_file:
88
  st.audio(audio_file.read(), format='audio/mp3')
89
 
90
  else:
91
- st.info("Upload a PDF Health Report to begin.")
 
3
  from gtts import gTTS
4
  import tempfile
5
  import os
6
+ import requests
7
 
8
  # Streamlit page setup
9
  st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
 
20
  text += page.get_text()
21
  return text
22
 
23
+ # Ask medical question using Hugging Face Inference API (BioGPT)
24
+ API_URL = "https://api-inference.huggingface.co/models/microsoft/BioGPT-Large"
25
+ headers = {"Content-Type": "application/json"}
26
+
27
+ def ask_medical_question(question):
28
+ payload = {
29
+ "inputs": f"Explain this medical concept in detail for a general audience: {question}"
30
+ }
31
+ response = requests.post(API_URL, headers=headers, json=payload)
32
+ if response.status_code == 200:
33
+ return response.json()[0]['generated_text']
34
+ else:
35
+ return "⚠️ Sorry, something went wrong while contacting the medical AI."
36
 
37
  # Doctor recommendation logic
38
  def suggest_doctor(term):
 
47
  return "A **diabetologist** or **endocrinologist** is recommended for sugar level issues."
48
  return "Please consult a **general physician** for further evaluation."
49
 
50
+ # Basic reference ranges
51
  def get_reference_range(term):
52
  term = term.lower()
53
  if "hemoglobin" in term:
 
61
  # Main logic
62
  if uploaded_file:
63
  text_data = extract_text(uploaded_file)
64
+ st.success("Health Report Uploaded Successfully!")
65
 
66
  # Display the report text
67
  st.markdown("### 📄 Health Report Content")
68
  st.write(text_data)
69
 
 
70
  st.session_state['report_text'] = text_data
71
 
72
  st.subheader("💬 Ask About Any Medical Term or Part of the Report")
73
 
74
+ user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):", key="question")
75
 
76
  if st.button("Get AI Explanation") and user_question:
77
  with st.spinner("Thinking..."):
78
+ explanation = ask_medical_question(user_question)
 
 
79
  range_info = get_reference_range(user_question)
80
  suggestion = suggest_doctor(user_question)
81
 
 
94
  st.audio(audio_file.read(), format='audio/mp3')
95
 
96
  else:
97
+ st.info("📂 Upload a PDF Health Report to begin.")