Update app.py
Browse files
app.py
CHANGED
@@ -6,13 +6,6 @@ import requests
|
|
6 |
|
7 |
from pypdf import PdfReader
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
-
from getpass import getpass
|
10 |
-
|
11 |
-
################################################################################
|
12 |
-
# 0. Optionally Prompt for Gemini Key in Colab (SECURE)
|
13 |
-
################################################################################
|
14 |
-
# key_input = getpass("Enter your Gemini API key: ")
|
15 |
-
# os.environ["GEMINI_API_KEY"] = key_input
|
16 |
|
17 |
################################################################################
|
18 |
# 1. PDF Parsing and Chunking
|
@@ -25,7 +18,7 @@ def extract_pdf_text(pdf_file) -> str:
|
|
25 |
all_text.append(text.strip())
|
26 |
return "\n".join(all_text)
|
27 |
|
28 |
-
def chunk_text(text, chunk_size=
|
29 |
words = text.split()
|
30 |
chunks = []
|
31 |
start = 0
|
@@ -123,7 +116,7 @@ def process_pdf(pdf_file):
|
|
123 |
if not text:
|
124 |
return None, "No text found in PDF."
|
125 |
|
126 |
-
chunks = chunk_text(text, chunk_size=
|
127 |
if not chunks:
|
128 |
return None, "No valid text to chunk."
|
129 |
|
@@ -142,63 +135,88 @@ def chat_with_paper(query, state):
|
|
142 |
|
143 |
demo_theme = gr.themes.Soft(primary_hue="slate")
|
144 |
|
145 |
-
# Custom CSS
|
146 |
# 1. Lightest blue background
|
147 |
# 2. Green buttons
|
148 |
-
# 3. Thick black border
|
149 |
-
# 4. Large, bold,
|
150 |
-
# 5.
|
151 |
css_code = """
|
152 |
-
/* Make the background a very light blue */
|
153 |
body {
|
154 |
-
background-color: #E6F7FF; /* Lightest blue
|
|
|
|
|
155 |
}
|
156 |
|
157 |
-
/*
|
158 |
.block > .inside {
|
159 |
-
|
160 |
-
|
161 |
-
border
|
162 |
-
|
|
|
|
|
163 |
}
|
164 |
|
165 |
-
/* Title heading:
|
166 |
-
#title
|
167 |
-
text-align: center;
|
168 |
-
font-size:
|
169 |
-
font-weight: 900;
|
170 |
-
margin-bottom:
|
171 |
-
margin-top: 0;
|
172 |
}
|
173 |
|
174 |
-
/* Welcome text
|
175 |
-
#welcome
|
176 |
-
text-align: center;
|
177 |
-
font-size: 1.
|
178 |
-
color: #444;
|
179 |
-
margin-bottom: 25px;
|
180 |
-
|
181 |
-
font-weight: 500;
|
182 |
}
|
183 |
|
184 |
-
/* Buttons
|
185 |
button {
|
186 |
-
background-color: #3CB371 !important; /*
|
187 |
-
color: #
|
188 |
border: none !important;
|
189 |
font-weight: 600 !important;
|
190 |
cursor: pointer;
|
191 |
}
|
192 |
|
193 |
-
/* Button hover effect */
|
194 |
button:hover {
|
195 |
-
background-color: #2E8B57 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
}
|
197 |
"""
|
198 |
|
199 |
with gr.Blocks(theme=demo_theme, css=css_code) as demo:
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
|
203 |
state = gr.State()
|
204 |
|
|
|
6 |
|
7 |
from pypdf import PdfReader
|
8 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
################################################################################
|
11 |
# 1. PDF Parsing and Chunking
|
|
|
18 |
all_text.append(text.strip())
|
19 |
return "\n".join(all_text)
|
20 |
|
21 |
+
def chunk_text(text, chunk_size=300, overlap=50):
|
22 |
words = text.split()
|
23 |
chunks = []
|
24 |
start = 0
|
|
|
116 |
if not text:
|
117 |
return None, "No text found in PDF."
|
118 |
|
119 |
+
chunks = chunk_text(text, chunk_size=300, overlap=50)
|
120 |
if not chunks:
|
121 |
return None, "No valid text to chunk."
|
122 |
|
|
|
135 |
|
136 |
demo_theme = gr.themes.Soft(primary_hue="slate")
|
137 |
|
138 |
+
# Custom CSS:
|
139 |
# 1. Lightest blue background
|
140 |
# 2. Green buttons
|
141 |
+
# 3. Thick black border, centered content
|
142 |
+
# 4. Large, bold, center-aligned title
|
143 |
+
# 5. Representative icon at top, bigger font for welcome text
|
144 |
css_code = """
|
|
|
145 |
body {
|
146 |
+
background-color: #E6F7FF !important; /* Lightest blue */
|
147 |
+
margin: 0;
|
148 |
+
padding: 0;
|
149 |
}
|
150 |
|
151 |
+
/* Center the entire Gradio container and give a thick black border */
|
152 |
.block > .inside {
|
153 |
+
margin: auto !important;
|
154 |
+
max-width: 900px !important; /* You can increase/decrease the max-width for your preference */
|
155 |
+
border: 4px solid black !important; /* Thick black border */
|
156 |
+
border-radius: 10px !important;
|
157 |
+
background-color: #FFFFFF !important; /* White container for clarity */
|
158 |
+
padding: 20px !important;
|
159 |
}
|
160 |
|
161 |
+
/* Title heading: bigger, bolder, centered */
|
162 |
+
#app-title {
|
163 |
+
text-align: center !important;
|
164 |
+
font-size: 3rem !important;
|
165 |
+
font-weight: 900 !important;
|
166 |
+
margin-bottom: 0.5rem !important;
|
167 |
+
margin-top: 0.5rem !important;
|
168 |
}
|
169 |
|
170 |
+
/* Welcome text: slightly smaller, but still bold, centered */
|
171 |
+
#app-welcome {
|
172 |
+
text-align: center !important;
|
173 |
+
font-size: 1.5rem !important;
|
174 |
+
color: #444 !important;
|
175 |
+
margin-bottom: 25px !important;
|
176 |
+
font-weight: 700 !important;
|
|
|
177 |
}
|
178 |
|
179 |
+
/* Buttons: green background, white text */
|
180 |
button {
|
181 |
+
background-color: #3CB371 !important; /* Medium sea green */
|
182 |
+
color: #ffffff !important;
|
183 |
border: none !important;
|
184 |
font-weight: 600 !important;
|
185 |
cursor: pointer;
|
186 |
}
|
187 |
|
188 |
+
/* Button hover effect: darker green */
|
189 |
button:hover {
|
190 |
+
background-color: #2E8B57 !important;
|
191 |
+
}
|
192 |
+
|
193 |
+
/* Optional: center the text in textboxes, if you like */
|
194 |
+
textarea, input[type="text"] {
|
195 |
+
text-align: center !important;
|
196 |
+
}
|
197 |
+
|
198 |
+
/* Icon container styling */
|
199 |
+
#icon-container {
|
200 |
+
text-align: center !important;
|
201 |
+
margin-top: 1rem !important;
|
202 |
+
margin-bottom: 1rem !important;
|
203 |
}
|
204 |
"""
|
205 |
|
206 |
with gr.Blocks(theme=demo_theme, css=css_code) as demo:
|
207 |
+
# Representative icon/image at the top
|
208 |
+
# Replace the 'src' with any other icon URL you prefer
|
209 |
+
gr.Markdown("""
|
210 |
+
<div id="icon-container">
|
211 |
+
<img src="https://i.ibb.co/3Wp3yBZ/ai-icon.png" alt="AI icon" style="width:100px;">
|
212 |
+
</div>
|
213 |
+
""")
|
214 |
+
|
215 |
+
# App title (large, bold, centered)
|
216 |
+
gr.Markdown("<div id='app-title'>AI-Powered Personal Research Assistant</div>")
|
217 |
+
|
218 |
+
# Welcome text right under the title
|
219 |
+
gr.Markdown("<div id='app-welcome'>Welcome! How may I help you?</div>")
|
220 |
|
221 |
state = gr.State()
|
222 |
|