Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +148 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,150 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
import os
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 9 |
|
| 10 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 11 |
+
|
| 12 |
+
# Page config
|
| 13 |
+
st.set_page_config(page_title="π Chat with PDF", layout="wide")
|
| 14 |
+
|
| 15 |
+
# Theme switcher radio
|
| 16 |
+
theme = st.radio("π Select Theme", ["Dark", "Light"], horizontal=True)
|
| 17 |
+
|
| 18 |
+
# Define colors based on theme
|
| 19 |
+
if theme == "Dark":
|
| 20 |
+
bg_color = "rgba(0, 0, 0, 0.85)"
|
| 21 |
+
app_bg = "rgba(20, 20, 20, 0.6)"
|
| 22 |
+
sidebar_bg = "rgba(10, 10, 10, 0.9)"
|
| 23 |
+
text_color = "#FFFFFF"
|
| 24 |
+
input_bg = "rgba(50, 50, 50, 0.5)"
|
| 25 |
+
accent_color = "#2AA198"
|
| 26 |
+
else:
|
| 27 |
+
bg_color = "#f0f2f6"
|
| 28 |
+
app_bg = "rgba(255, 255, 255, 0.6)"
|
| 29 |
+
sidebar_bg = "rgba(255, 255, 255, 0.9)"
|
| 30 |
+
text_color = "#000000"
|
| 31 |
+
input_bg = "rgba(240, 240, 240, 0.9)"
|
| 32 |
+
accent_color = "#2AA198"
|
| 33 |
+
|
| 34 |
+
# Inject CSS styles
|
| 35 |
+
st.markdown(f"""
|
| 36 |
+
<style>
|
| 37 |
+
body {{
|
| 38 |
+
background: {bg_color};
|
| 39 |
+
font-family: 'Segoe UI', sans-serif;
|
| 40 |
+
}}
|
| 41 |
+
.stApp {{
|
| 42 |
+
background: {app_bg};
|
| 43 |
+
backdrop-filter: blur(15px);
|
| 44 |
+
padding: 2rem;
|
| 45 |
+
border-radius: 16px;
|
| 46 |
+
margin: 2rem auto;
|
| 47 |
+
max-width: 1000px;
|
| 48 |
+
color: {text_color};
|
| 49 |
+
}}
|
| 50 |
+
h1, h2, h3, h4, .stRadio label, label {{
|
| 51 |
+
color: {"text_color"} !important;
|
| 52 |
+
font-weight: 600;
|
| 53 |
+
}}
|
| 54 |
+
[data-testid="stSidebar"] {{
|
| 55 |
+
background: {sidebar_bg} !important;
|
| 56 |
+
color: {text_color} !important;
|
| 57 |
+
backdrop-filter: blur(10px);
|
| 58 |
+
border-right: 1px solid rgba(255,255,255,0.1);
|
| 59 |
+
}}
|
| 60 |
+
[data-testid="stSidebar"] * {{
|
| 61 |
+
color: {text_color} !important;
|
| 62 |
+
}}
|
| 63 |
+
.stTextInput input {{
|
| 64 |
+
background-color: {input_bg};
|
| 65 |
+
color: {text_color} !important;
|
| 66 |
+
border: 1px solid #ccc;
|
| 67 |
+
border-radius: 8px;
|
| 68 |
+
padding: 0.5rem;
|
| 69 |
+
}}
|
| 70 |
+
.stTextInput input::placeholder {{
|
| 71 |
+
color: #bbbbbb;
|
| 72 |
+
}}
|
| 73 |
+
.stMarkdown, .stText {{
|
| 74 |
+
color: {text_color} !important;
|
| 75 |
+
}}
|
| 76 |
+
.stSuccess {{
|
| 77 |
+
background-color: rgba(255, 255, 255, 0.07) !important;
|
| 78 |
+
border-left: 6px solid {accent_color};
|
| 79 |
+
padding: 1rem;
|
| 80 |
+
border-radius: 12px;
|
| 81 |
+
color: {text_color} !important;
|
| 82 |
+
font-size: 1.05rem;
|
| 83 |
+
}}
|
| 84 |
+
button {{
|
| 85 |
+
background-color: {accent_color} !important;
|
| 86 |
+
color: white !important;
|
| 87 |
+
border-radius: 8px;
|
| 88 |
+
padding: 0.5rem 1rem;
|
| 89 |
+
border: none;
|
| 90 |
+
}}
|
| 91 |
+
.stRadio > label {{
|
| 92 |
+
font-size: 1rem;
|
| 93 |
+
font-weight: 600;
|
| 94 |
+
color: {"text_color"} !important;
|
| 95 |
+
}}
|
| 96 |
+
.stRadio div[role="radiogroup"] > label {{
|
| 97 |
+
color: {text_color} !important;
|
| 98 |
+
font-weight: 500;
|
| 99 |
+
}}
|
| 100 |
+
</style>
|
| 101 |
+
""", unsafe_allow_html=True)
|
| 102 |
+
|
| 103 |
+
# Sidebar UI
|
| 104 |
+
with st.sidebar:
|
| 105 |
+
st.markdown("### π Upload PDF")
|
| 106 |
+
file = st.file_uploader("Upload your PDF", type="pdf")
|
| 107 |
+
|
| 108 |
+
st.write("Then ask your question below!")
|
| 109 |
+
|
| 110 |
+
# Main UI
|
| 111 |
+
st.markdown(f"<h1 style='text-align:center;'>πβ¨ Chat With Your PDF</h1>", unsafe_allow_html=True)
|
| 112 |
+
|
| 113 |
+
if file is not None:
|
| 114 |
+
pdf_pages = PdfReader(file)
|
| 115 |
+
text = ""
|
| 116 |
+
for page in pdf_pages.pages:
|
| 117 |
+
text += page.extract_text() # Note: was missing += in your original code
|
| 118 |
+
|
| 119 |
+
# Split the text into chunks
|
| 120 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 121 |
+
separators=["\n"],
|
| 122 |
+
chunk_size=1000,
|
| 123 |
+
chunk_overlap=150,
|
| 124 |
+
length_function=len
|
| 125 |
+
)
|
| 126 |
+
chunks = text_splitter.split_text(text)
|
| 127 |
+
|
| 128 |
+
# Generate Embeddings
|
| 129 |
+
model_name = "sentence-transformers/all-mpnet-base-v2"
|
| 130 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 131 |
+
|
| 132 |
+
# Create vector store
|
| 133 |
+
vector_store = FAISS.from_texts(chunks, embeddings)
|
| 134 |
+
|
| 135 |
+
# Query input
|
| 136 |
+
user_query = st.text_input("Ask a question about the file")
|
| 137 |
+
if user_query:
|
| 138 |
+
match = vector_store.similarity_search(user_query)
|
| 139 |
+
llm = ChatGroq(
|
| 140 |
+
model_name="llama-3.1-8b-instant",
|
| 141 |
+
temperature=0.0,
|
| 142 |
+
max_retries=2
|
| 143 |
+
)
|
| 144 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 145 |
+
response = chain.run(input_documents=match, question=user_query)
|
| 146 |
+
|
| 147 |
+
st.subheader("β
Response")
|
| 148 |
+
st.markdown(f"<div style='color:{text_color};'>{response}</div>", unsafe_allow_html=True)
|
| 149 |
+
else:
|
| 150 |
+
st.info("Please upload a PDF file to get started.")
|