nomanabdullah2025 commited on
Commit
e45bc04
Β·
verified Β·
1 Parent(s): 2038e37

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +136 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,138 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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
+ # Only Light theme colors now
16
+ bg_color = "#f0f2f6"
17
+ app_bg = "rgba(255, 255, 255, 0.6)"
18
+ sidebar_bg = "rgba(255, 255, 255, 0.9)"
19
+ text_color = "#000000"
20
+ input_bg = "rgba(240, 240, 240, 0.9)"
21
+ accent_color = "#2AA198"
22
+
23
+ # Inject CSS styles (Light theme only)
24
+ st.markdown(f"""
25
+ <style>
26
+ body {{
27
+ background: {bg_color};
28
+ font-family: 'Segoe UI', sans-serif;
29
+ }}
30
+ .stApp {{
31
+ background: {app_bg};
32
+ backdrop-filter: blur(15px);
33
+ padding: 2rem;
34
+ border-radius: 16px;
35
+ margin: 2rem auto;
36
+ max-width: 1000px;
37
+ color: {text_color};
38
+ }}
39
+ h1, h2, h3, h4, .stRadio label, label {{
40
+ color: {text_color} !important;
41
+ font-weight: 600;
42
+ }}
43
+ [data-testid="stSidebar"] {{
44
+ background: {sidebar_bg} !important;
45
+ color: {text_color} !important;
46
+ backdrop-filter: blur(10px);
47
+ border-right: 1px solid rgba(0,0,0,0.1);
48
+ }}
49
+ [data-testid="stSidebar"] * {{
50
+ color: {text_color} !important;
51
+ }}
52
+ .stTextInput input {{
53
+ background-color: {input_bg};
54
+ color: {text_color} !important;
55
+ border: 1px solid #ccc;
56
+ border-radius: 8px;
57
+ padding: 0.5rem;
58
+ }}
59
+ .stTextInput input::placeholder {{
60
+ color: #777777;
61
+ }}
62
+ .stMarkdown, .stText, .stSubheader, .stHeader, p {{
63
+ color: {text_color} !important;
64
+ }}
65
+ .stSuccess {{
66
+ background-color: rgba(0, 0, 0, 0.03) !important;
67
+ border-left: 6px solid {accent_color};
68
+ padding: 1rem;
69
+ border-radius: 12px;
70
+ color: {text_color} !important;
71
+ font-size: 1.05rem;
72
+ }}
73
+ button {{
74
+ background-color: {accent_color} !important;
75
+ color: white !important;
76
+ border-radius: 8px;
77
+ padding: 0.5rem 1rem;
78
+ border: none;
79
+ }}
80
+ .stRadio > label {{
81
+ font-size: 1rem;
82
+ font-weight: 600;
83
+ color: {text_color} !important;
84
+ }}
85
+ .stRadio div[role="radiogroup"] > label {{
86
+ color: {text_color} !important;
87
+ font-weight: 500;
88
+ }}
89
+ </style>
90
+ """, unsafe_allow_html=True)
91
+
92
+ # Sidebar UI
93
+ with st.sidebar:
94
+ st.markdown("### πŸ“ Upload PDF")
95
+ file = st.file_uploader("Upload your PDF", type="pdf")
96
+ st.write("Then ask your question below!")
97
+
98
+ # Main UI
99
+ st.markdown(f"<h1 style='text-align:center;'>πŸ“„βœ¨ Chat With Your PDF</h1>", unsafe_allow_html=True)
100
+
101
+ if file is not None:
102
+ pdf_pages = PdfReader(file)
103
+ text = ""
104
+ for page in pdf_pages.pages:
105
+ text += page.extract_text()
106
+
107
+ # Split the text into chunks
108
+ text_splitter = RecursiveCharacterTextSplitter(
109
+ separators=["\n"],
110
+ chunk_size=1000,
111
+ chunk_overlap=150,
112
+ length_function=len
113
+ )
114
+ chunks = text_splitter.split_text(text)
115
+
116
+ # Generate Embeddings
117
+ model_name = "sentence-transformers/all-mpnet-base-v2"
118
+ embeddings = HuggingFaceEmbeddings(model_name=model_name)
119
+
120
+ # Create vector store
121
+ vector_store = FAISS.from_texts(chunks, embeddings)
122
+
123
+ # Query input
124
+ user_query = st.text_input("Ask a question about the file")
125
+ if user_query:
126
+ match = vector_store.similarity_search(user_query)
127
+ llm = ChatGroq(
128
+ model_name="llama-3.1-8b-instant",
129
+ temperature=0.0,
130
+ max_retries=2
131
+ )
132
+ chain = load_qa_chain(llm, chain_type="stuff")
133
+ response = chain.run(input_documents=match, question=user_query)
134
+
135
+ st.subheader("βœ… Response")
136
+ st.markdown(f"<div style='color:{text_color};'>{response}</div>", unsafe_allow_html=True)
137
+ else:
138
+ st.info("Please upload a PDF file to get started.")