-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
84 lines (75 loc) · 2.48 KB
/
app.py
File metadata and controls
84 lines (75 loc) · 2.48 KB
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
import streamlit as st
from transformers import pipeline
import fitz # PyMuPDF
# Load Hugging Face models with specific model names to avoid loading issues
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
def summarize_text(text):
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
return summary[0]['summary_text']
def answer_question(question, context):
answer = qa_model(question=question, context=context)
return answer['answer']
def extract_text_from_pdf(file):
doc = fitz.open(stream=file.read(), filetype="pdf")
text = ""
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text += page.get_text()
return text
# Custom CSS
st.markdown(
"""
<style>
.css-18e3th9 {
background-color: #FFFFFF;
}
.css-1d391kg {
background-color: #F0F2F6;
}
.stButton>button {
background-color: #4CAF50;
color: white;
border: none;
padding: 8px 16px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.stButton>button:hover {
background-color: white;
color: black;
border: 2px solid #4CAF50;
}
</style>
""",
unsafe_allow_html=True
)
st.title("AI Assistant")
menu = ["Summarize PDF", "Ask a Question"]
choice = st.sidebar.selectbox("Choose an action", menu)
if choice == "Summarize PDF":
st.header("Summarize PDF")
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
text = extract_text_from_pdf(uploaded_file)
st.subheader("Extracted Text")
st.write(text[:1000] + "...") # Display first 1000 characters for readability
summary = summarize_text(text)
st.subheader("Summary")
st.write(summary)
elif choice == "Ask a Question":
st.header("Ask a Question")
context = st.text_area("Enter the context (e.g., a paragraph from a document)")
question = st.text_input("Enter your question")
if st.button("Get Answer"):
if context and question:
answer = answer_question(question, context)
st.subheader("Answer")
st.write(answer)
else:
st.error("Please provide both context and a question.")