Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import pdfplumber
|
4 |
+
from langchain.llms import HuggingFacePipeline
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
|
7 |
+
# Function to extract text from a PDF
|
8 |
+
def extract_text_from_pdf(pdf_file):
|
9 |
+
with pdfplumber.open(pdf_file) as pdf:
|
10 |
+
text = ''
|
11 |
+
for page in pdf.pages:
|
12 |
+
text += page.extract_text()
|
13 |
+
return text
|
14 |
+
|
15 |
+
# Define the prompt template
|
16 |
+
template = """
|
17 |
+
You are a medical summarization expert. Focus on the following key aspects when summarizing:
|
18 |
+
|
19 |
+
1. Patient History
|
20 |
+
2. Diagnosis
|
21 |
+
3. Treatment Recommendations
|
22 |
+
4. Follow-up Plans
|
23 |
+
|
24 |
+
Here’s the medical report to summarize:
|
25 |
+
|
26 |
+
{text}
|
27 |
+
"""
|
28 |
+
|
29 |
+
prompt = PromptTemplate(
|
30 |
+
input_variables=["text"],
|
31 |
+
template=template
|
32 |
+
)
|
33 |
+
|
34 |
+
# Streamlit application layout
|
35 |
+
st.title("Medical Report Summarizer")
|
36 |
+
|
37 |
+
# Option to upload PDF or enter text
|
38 |
+
option = st.selectbox("Choose Input Method", ["Upload PDF", "Enter Text"])
|
39 |
+
|
40 |
+
if option == "Upload PDF":
|
41 |
+
uploaded_file = st.file_uploader("Upload your PDF file", type=["pdf"])
|
42 |
+
if uploaded_file is not None:
|
43 |
+
# Extract text from the uploaded PDF
|
44 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
45 |
+
|
46 |
+
# Dynamic calculation for max_length based on extracted text length
|
47 |
+
length = max(2, int(len(extracted_text) // 2))
|
48 |
+
|
49 |
+
# Load the summarization pipeline with updated max_length
|
50 |
+
summarizer = pipeline(
|
51 |
+
"summarization",
|
52 |
+
model="fine_tuned_model", # Ensure the path to your fine-tuned model is correct
|
53 |
+
temperature=0.3,
|
54 |
+
min_length=100,
|
55 |
+
max_length=int(length),
|
56 |
+
# top_k=80, # Uncomment if you want to use top_k
|
57 |
+
# top_p=0.95 # Uncomment if you want to use top_p
|
58 |
+
)
|
59 |
+
|
60 |
+
llm = HuggingFacePipeline(pipeline=summarizer)
|
61 |
+
|
62 |
+
# Create the formatted prompt
|
63 |
+
formatted_prompt = prompt.format(text=extracted_text)
|
64 |
+
|
65 |
+
# Generate the summary
|
66 |
+
summary = llm(formatted_prompt)
|
67 |
+
|
68 |
+
st.subheader("Summary:")
|
69 |
+
st.write(summary)
|
70 |
+
|
71 |
+
elif option == "Enter Text":
|
72 |
+
input_text = st.text_area("Enter the text to summarize", height=300)
|
73 |
+
|
74 |
+
if st.button("Summarize"):
|
75 |
+
if input_text:
|
76 |
+
# Dynamic calculation for max_length based on entered text length
|
77 |
+
length = max(2, int(len(input_text) // 2))
|
78 |
+
|
79 |
+
# Load the summarization pipeline with updated max_length
|
80 |
+
summarizer = pipeline(
|
81 |
+
"summarization",
|
82 |
+
model="fine_tuned_model",
|
83 |
+
temperature=0.3,
|
84 |
+
min_length=100,
|
85 |
+
max_length=int(length),
|
86 |
+
top_k=80,
|
87 |
+
top_p=0.95
|
88 |
+
)
|
89 |
+
|
90 |
+
llm = HuggingFacePipeline(pipeline=summarizer)
|
91 |
+
|
92 |
+
# Create the formatted prompt
|
93 |
+
formatted_prompt = prompt.format(text=input_text)
|
94 |
+
|
95 |
+
# Generate the summary
|
96 |
+
summary = llm(formatted_prompt)
|
97 |
+
|
98 |
+
st.subheader("Summary:")
|
99 |
+
st.write(summary)
|
100 |
+
else:
|
101 |
+
st.warning("Please enter some text to summarize.")
|