nomanabdullah2025 commited on
Commit
25d6523
Β·
verified Β·
1 Parent(s): f08a71e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # 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
+ # 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.")