Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
-
import requests
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
|
|
|
| 7 |
from keybert import KeyBERT
|
| 8 |
from textblob import TextBlob
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
load_dotenv()
|
| 12 |
-
|
| 13 |
-
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 14 |
GROQ_MODEL = "llama3-8b-8192"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
st.
|
| 18 |
-
|
| 19 |
st.markdown("""
|
| 20 |
-
|
| 21 |
""")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# ---------- Utilities ----------
|
| 27 |
|
|
|
|
| 28 |
def extract_text_from_pdf(file):
|
| 29 |
doc = fitz.open(stream=file.read(), filetype="pdf")
|
| 30 |
text = ""
|
|
@@ -32,38 +36,42 @@ def extract_text_from_pdf(file):
|
|
| 32 |
text += page.get_text()
|
| 33 |
return text
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
response.
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def extract_keywords(text, top_n=10):
|
| 62 |
kw_model = KeyBERT()
|
| 63 |
keywords = kw_model.extract_keywords(text, top_n=top_n, stop_words='english')
|
| 64 |
return [kw[0] for kw in keywords]
|
| 65 |
|
| 66 |
-
|
| 67 |
def get_sentiment(text):
|
| 68 |
blob = TextBlob(text)
|
| 69 |
polarity = blob.sentiment.polarity
|
|
@@ -74,48 +82,47 @@ def get_sentiment(text):
|
|
| 74 |
else:
|
| 75 |
return "π Neutral"
|
| 76 |
|
| 77 |
-
|
| 78 |
def make_download_button(text, filename="summary.txt"):
|
| 79 |
st.download_button("πΎ Download Summary", data=text, file_name=filename, mime="text/plain")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
else:
|
| 121 |
-
st.info("π₯ Upload
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import fitz # PyMuPDF
|
|
|
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
+
import tempfile
|
| 6 |
+
import faiss
|
| 7 |
+
import numpy as np
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 10 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 11 |
+
from langchain.vectorstores import FAISS
|
| 12 |
+
from langchain.docstore.document import Document
|
| 13 |
from keybert import KeyBERT
|
| 14 |
from textblob import TextBlob
|
| 15 |
+
from groq import Groq
|
| 16 |
|
| 17 |
+
# Load environment
|
| 18 |
load_dotenv()
|
| 19 |
+
client = Groq(api_key=os.environ.get("wbm1"))
|
|
|
|
| 20 |
GROQ_MODEL = "llama3-8b-8192"
|
| 21 |
|
| 22 |
+
# Streamlit setup
|
| 23 |
+
st.set_page_config(page_title="π§ Smart PDF ChatBot", layout="centered")
|
| 24 |
+
st.title("π¬ Smart PDF ChatBot")
|
| 25 |
st.markdown("""
|
| 26 |
+
Upload one or more PDFs. Get summaries, insights, and interact with AI about the content using a persistent memory chat.
|
| 27 |
""")
|
| 28 |
|
| 29 |
+
uploaded_files = st.file_uploader("π Upload PDF files", type=["pdf"], accept_multiple_files=True)
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Utilities
|
| 32 |
def extract_text_from_pdf(file):
|
| 33 |
doc = fitz.open(stream=file.read(), filetype="pdf")
|
| 34 |
text = ""
|
|
|
|
| 36 |
text += page.get_text()
|
| 37 |
return text
|
| 38 |
|
| 39 |
+
def split_text(text):
|
| 40 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 41 |
+
return splitter.split_text(text)
|
| 42 |
+
|
| 43 |
+
def create_vector_store(chunks):
|
| 44 |
+
documents = [Document(page_content=c) for c in chunks]
|
| 45 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 46 |
+
return FAISS.from_documents(documents, embeddings)
|
| 47 |
+
|
| 48 |
+
def summarize_text(text):
|
| 49 |
+
response = client.chat.completions.create(
|
| 50 |
+
model=GROQ_MODEL,
|
| 51 |
+
messages=[
|
| 52 |
+
{"role": "system", "content": "You are an AI that summarizes documents."},
|
| 53 |
+
{"role": "user", "content": f"Summarize this:\n{text}"}
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
return response.choices[0].message.content
|
| 57 |
+
|
| 58 |
+
def ask_question(vectorstore, question):
|
| 59 |
+
docs = vectorstore.similarity_search(question, k=3)
|
| 60 |
+
context = "\n".join([d.page_content for d in docs])
|
| 61 |
+
response = client.chat.completions.create(
|
| 62 |
+
model=GROQ_MODEL,
|
| 63 |
+
messages=[
|
| 64 |
+
{"role": "system", "content": "You answer questions based on document context."},
|
| 65 |
+
{"role": "user", "content": f"Context:\n{context}\n\nQuestion:\n{question}"}
|
| 66 |
+
]
|
| 67 |
+
)
|
| 68 |
+
return response.choices[0].message.content
|
| 69 |
|
| 70 |
def extract_keywords(text, top_n=10):
|
| 71 |
kw_model = KeyBERT()
|
| 72 |
keywords = kw_model.extract_keywords(text, top_n=top_n, stop_words='english')
|
| 73 |
return [kw[0] for kw in keywords]
|
| 74 |
|
|
|
|
| 75 |
def get_sentiment(text):
|
| 76 |
blob = TextBlob(text)
|
| 77 |
polarity = blob.sentiment.polarity
|
|
|
|
| 82 |
else:
|
| 83 |
return "π Neutral"
|
| 84 |
|
|
|
|
| 85 |
def make_download_button(text, filename="summary.txt"):
|
| 86 |
st.download_button("πΎ Download Summary", data=text, file_name=filename, mime="text/plain")
|
| 87 |
|
| 88 |
+
# App logic
|
| 89 |
+
if uploaded_files:
|
| 90 |
+
all_text = ""
|
| 91 |
+
for file in uploaded_files:
|
| 92 |
+
st.write(f"π Processing {file.name}...")
|
| 93 |
+
text = extract_text_from_pdf(file)
|
| 94 |
+
all_text += f"\n\n{text}"
|
| 95 |
+
|
| 96 |
+
st.subheader("π Extracting Insights...")
|
| 97 |
+
chunks = split_text(all_text)
|
| 98 |
+
vectorstore = create_vector_store(chunks)
|
| 99 |
+
|
| 100 |
+
st.write("π Generating summary...")
|
| 101 |
+
summary = summarize_text(all_text)
|
| 102 |
+
st.success(summary)
|
| 103 |
+
make_download_button(summary)
|
| 104 |
+
|
| 105 |
+
st.subheader("π Keywords")
|
| 106 |
+
keywords = extract_keywords(summary)
|
| 107 |
+
st.write(", ".join(keywords))
|
| 108 |
+
|
| 109 |
+
st.subheader("π Sentiment")
|
| 110 |
+
sentiment = get_sentiment(summary)
|
| 111 |
+
st.write(sentiment)
|
| 112 |
+
|
| 113 |
+
st.markdown("---")
|
| 114 |
+
st.subheader("π¬ Ask a question about the documents")
|
| 115 |
+
if "chat_history" not in st.session_state:
|
| 116 |
+
st.session_state.chat_history = []
|
| 117 |
+
|
| 118 |
+
user_question = st.text_input("Type your question")
|
| 119 |
+
if user_question:
|
| 120 |
+
with st.spinner("π€ Thinking..."):
|
| 121 |
+
answer = ask_question(vectorstore, user_question)
|
| 122 |
+
st.session_state.chat_history.append((user_question, answer))
|
| 123 |
+
|
| 124 |
+
for q, a in st.session_state.chat_history:
|
| 125 |
+
st.markdown(f"**You:** {q}")
|
| 126 |
+
st.markdown(f"**AI:** {a}")
|
| 127 |
else:
|
| 128 |
+
st.info("π₯ Upload one or more PDF files to get started.")
|