Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pdfplumber
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load AI Models
|
6 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
7 |
+
qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
8 |
+
|
9 |
+
# Function to extract text from PDF
|
10 |
+
def extract_text_from_pdf(pdf):
|
11 |
+
with pdfplumber.open(pdf) as pdf_file:
|
12 |
+
text = ""
|
13 |
+
for page in pdf_file.pages:
|
14 |
+
text += page.extract_text() + "\n"
|
15 |
+
return text
|
16 |
+
|
17 |
+
# Function to summarize text
|
18 |
+
def summarize_text(text):
|
19 |
+
summary = summarizer(text, max_length=200, min_length=50, do_sample=False)
|
20 |
+
return summary[0]['summary_text']
|
21 |
+
|
22 |
+
# Function for Q&A
|
23 |
+
def answer_question(context, question):
|
24 |
+
response = qa_model(question=question, context=context)
|
25 |
+
return response['answer']
|
26 |
+
|
27 |
+
# Streamlit UI
|
28 |
+
st.set_page_config(page_title="MedGen-AI", page_icon="🩺", layout="wide")
|
29 |
+
st.title("🩺 MedGen-AI: Medical Report Simplifier")
|
30 |
+
|
31 |
+
uploaded_file = st.file_uploader("📄 Upload a Medical Report (PDF)", type="pdf")
|
32 |
+
|
33 |
+
if uploaded_file:
|
34 |
+
text = extract_text_from_pdf(uploaded_file)
|
35 |
+
st.subheader("📜 Extracted Text from Report")
|
36 |
+
st.write(text[:1000] + " ...") # Show first 1000 characters
|
37 |
+
|
38 |
+
summary = summarize_text(text)
|
39 |
+
st.subheader("📝 Simplified Medical Summary")
|
40 |
+
st.write(summary)
|
41 |
+
|
42 |
+
st.subheader("💬 Ask a Question About Your Report")
|
43 |
+
user_question = st.text_input("🔍 Enter your question:")
|
44 |
+
if user_question:
|
45 |
+
answer = answer_question(text, user_question)
|
46 |
+
st.write("🧑⚕️ **AI Answer:**", answer)
|