Sachin B M commited on
Commit
b66efdf
·
1 Parent(s): 67c26fd

gpu setting

Browse files
Files changed (1) hide show
  1. app.py +44 -57
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
 
2
  import gradio as gr
 
3
  from dotenv import load_dotenv
4
  from PyPDF2 import PdfReader
5
 
@@ -8,11 +10,21 @@ from langchain_cohere import CohereEmbeddings, ChatCohere
8
  from langchain_community.vectorstores import FAISS
9
  from langchain_classic.chains import ConversationalRetrievalChain
10
 
 
11
  load_dotenv()
12
 
13
 
14
  # =========================================================
15
- # 1. EXTRACT TEXT FROM PDF FILES
 
 
 
 
 
 
 
 
 
16
  # =========================================================
17
 
18
  def get_pdf_text(pdf_files):
@@ -28,7 +40,7 @@ def get_pdf_text(pdf_files):
28
 
29
 
30
  # =========================================================
31
- # 2. SPLIT TEXT INTO CHUNKS
32
  # =========================================================
33
 
34
  def get_text_chunks(text):
@@ -43,7 +55,7 @@ def get_text_chunks(text):
43
 
44
 
45
  # =========================================================
46
- # 3. CREATE COHERE EMBEDDINGS
47
  # =========================================================
48
 
49
  def get_embeddings():
@@ -54,7 +66,7 @@ def get_embeddings():
54
 
55
 
56
  # =========================================================
57
- # 4. CREATE CONVERSATIONAL RETRIEVAL CHAIN
58
  # =========================================================
59
 
60
  def get_conversational_chain(vectorstore):
@@ -73,7 +85,7 @@ def get_conversational_chain(vectorstore):
73
 
74
 
75
  # =========================================================
76
- # 5. PROCESS UPLOADED DOCUMENTS
77
  # =========================================================
78
 
79
  def process_documents(pdf_files):
@@ -135,7 +147,7 @@ def process_documents(pdf_files):
135
 
136
 
137
  # =========================================================
138
- # 6. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT
139
  # =========================================================
140
 
141
  def convert_chat_history(history):
@@ -148,7 +160,6 @@ def convert_chat_history(history):
148
  role = message.get("role")
149
  content = message.get("content", "")
150
 
151
- # Convert list-based content into a string
152
  if isinstance(content, list):
153
  content = " ".join(
154
  str(item.get("text", ""))
@@ -183,7 +194,7 @@ def convert_chat_history(history):
183
 
184
 
185
  # =========================================================
186
- # 7. HANDLE USER QUESTIONS
187
  # =========================================================
188
 
189
  def handle_user_question(question, history, chain):
@@ -209,10 +220,8 @@ def handle_user_question(question, history, chain):
209
  return history, ""
210
 
211
  try:
212
- # Convert previous messages into LangChain format
213
  chat_history = convert_chat_history(history)
214
 
215
- # Ask the question
216
  response = chain.invoke({
217
  "question": question,
218
  "chat_history": chat_history
@@ -223,7 +232,6 @@ def handle_user_question(question, history, chain):
223
  "No answer was generated."
224
  )
225
 
226
- # Ensure the answer is a string
227
  if isinstance(answer, list):
228
  answer = " ".join(
229
  str(item.get("text", ""))
@@ -234,7 +242,6 @@ def handle_user_question(question, history, chain):
234
  else:
235
  answer = str(answer)
236
 
237
- # Add the new conversation to Gradio history
238
  history.append({
239
  "role": "user",
240
  "content": question
@@ -262,7 +269,7 @@ def handle_user_question(question, history, chain):
262
 
263
 
264
  # =========================================================
265
- # 8. CLEAR CHAT
266
  # =========================================================
267
 
268
  def clear_chat():
@@ -270,11 +277,10 @@ def clear_chat():
270
 
271
 
272
  # =========================================================
273
- # 9. CUSTOM CSS - RESPONSIVE DESIGN
274
  # =========================================================
275
 
276
  custom_css = """
277
- /* Overall application */
278
  .gradio-container {
279
  background: #0e0f14 !important;
280
  color: #f8fafc !important;
@@ -283,7 +289,6 @@ custom_css = """
283
  padding: 16px !important;
284
  }
285
 
286
- /* Main row */
287
  #main-row {
288
  display: flex !important;
289
  align-items: flex-start !important;
@@ -292,7 +297,6 @@ custom_css = """
292
  margin: 0 auto !important;
293
  }
294
 
295
- /* Sidebar */
296
  #sidebar {
297
  background: #24252f !important;
298
  border: 1px solid #343642 !important;
@@ -304,50 +308,38 @@ custom_css = """
304
  margin-top: 40px;
305
  }
306
 
307
- /* Sidebar heading */
308
  #sidebar h2 {
309
  font-size: 1.15rem !important;
310
  margin: 0 0 12px 0 !important;
311
  color: #ffffff !important;
312
  }
313
 
314
- /* Compact PDF uploader */
315
  #sidebar [data-testid="file-upload"] {
316
  min-height: 110px !important;
317
  }
318
 
319
- /* Process button */
320
  #process-button {
321
  margin-top: 10px !important;
322
  border-radius: 8px !important;
323
- transition: transform 0.2s ease,
324
- box-shadow 0.2s ease;
325
  }
326
 
327
- /* Button hover effect */
328
  #process-button:hover {
329
  transform: translateY(-1px);
330
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
331
  }
332
 
333
- /* Status box */
334
  #status {
335
  margin-top: 10px !important;
336
  }
337
 
338
- /* Main content */
339
  #main-panel {
340
  padding: 0 8px !important;
341
  min-width: 0 !important;
342
  }
343
 
344
- /* Hero header */
345
  #hero {
346
- background: linear-gradient(
347
- 120deg,
348
- #0f172a 0%,
349
- #1d4ed8 100%
350
- );
351
  padding: 28px 32px;
352
  border-radius: 18px;
353
  box-shadow: 0 10px 30px rgba(29, 78, 216, 0.18);
@@ -355,12 +347,10 @@ custom_css = """
355
  transition: box-shadow 0.2s ease;
356
  }
357
 
358
- /* Hero hover effect */
359
  #hero:hover {
360
  box-shadow: 0 12px 34px rgba(29, 78, 216, 0.28);
361
  }
362
 
363
- /* Hero title */
364
  #hero h1 {
365
  color: white !important;
366
  font-size: clamp(1.5rem, 2.5vw, 2.3rem) !important;
@@ -369,14 +359,12 @@ custom_css = """
369
  margin: 0 !important;
370
  }
371
 
372
- /* Hero description */
373
  #hero p {
374
  color: #dbeafe !important;
375
  font-size: 1rem !important;
376
  margin: 10px 0 0 0 !important;
377
  }
378
 
379
- /* Chat window */
380
  #chatbot {
381
  border-radius: 12px !important;
382
  min-height: 280px !important;
@@ -385,29 +373,24 @@ custom_css = """
385
  overflow: auto !important;
386
  }
387
 
388
- /* Question input */
389
  #question-box {
390
  margin-top: 12px !important;
391
  }
392
 
393
- /* Input field */
394
  #question-box textarea {
395
  min-height: 48px !important;
396
  border-radius: 10px !important;
397
  }
398
 
399
- /* Clear button */
400
  #main-panel button {
401
  border-radius: 8px !important;
402
  transition: background 0.2s ease;
403
  }
404
 
405
- /* Hide Gradio footer */
406
  footer {
407
  display: none !important;
408
  }
409
 
410
- /* Tablet screens */
411
  @media (max-width: 900px) {
412
  .gradio-container {
413
  padding: 12px !important;
@@ -431,7 +414,6 @@ footer {
431
  }
432
  }
433
 
434
- /* Mobile screens */
435
  @media (max-width: 700px) {
436
  .gradio-container {
437
  padding: 8px !important;
@@ -483,18 +465,22 @@ footer {
483
 
484
 
485
  # =========================================================
486
- # 10. BUILD GRADIO INTERFACE
487
  # =========================================================
488
 
489
- with gr.Blocks(
490
- title="Chat with Documents",
491
- theme=gr.themes.Base(),
492
- css=custom_css
493
- ) as demo:
494
 
495
- # Store the conversational chain
496
  chain_state = gr.State(None)
497
 
 
 
 
 
 
 
 
 
498
  with gr.Row(
499
  elem_id="main-row",
500
  equal_height=False
@@ -536,7 +522,6 @@ with gr.Blocks(
536
  elem_id="main-panel"
537
  ):
538
 
539
- # Header
540
  gr.HTML("""
541
  <div id="hero">
542
  <h1>AI Chatbot for Your Documents</h1>
@@ -546,14 +531,13 @@ with gr.Blocks(
546
  </div>
547
  """)
548
 
549
- # Chatbot
550
  chatbot = gr.Chatbot(
551
  label="Chat with your PDFs",
 
552
  height=500,
553
  elem_id="chatbot"
554
  )
555
 
556
- # Question input
557
  question = gr.Textbox(
558
  label="Ask a question",
559
  placeholder="Ask a question about your PDFs",
@@ -562,10 +546,7 @@ with gr.Blocks(
562
  elem_id="question-box"
563
  )
564
 
565
- # Clear chat button
566
- clear_button = gr.Button(
567
- "Clear Chat"
568
- )
569
 
570
  # Process PDF documents
571
  process_button.click(
@@ -590,8 +571,14 @@ with gr.Blocks(
590
 
591
 
592
  # =========================================================
593
- # 11. LAUNCH APPLICATION
594
  # =========================================================
595
 
596
  if __name__ == "__main__":
597
- demo.queue().launch()
 
 
 
 
 
 
 
1
  import os
2
+
3
  import gradio as gr
4
+ import spaces
5
  from dotenv import load_dotenv
6
  from PyPDF2 import PdfReader
7
 
 
10
  from langchain_community.vectorstores import FAISS
11
  from langchain_classic.chains import ConversationalRetrievalChain
12
 
13
+
14
  load_dotenv()
15
 
16
 
17
  # =========================================================
18
+ # 1. ZERO-GPU STARTUP CHECK
19
+ # =========================================================
20
+
21
+ @spaces.GPU(duration=1)
22
+ def zerogpu_check():
23
+ return None
24
+
25
+
26
+ # =========================================================
27
+ # 2. EXTRACT TEXT FROM PDF FILES
28
  # =========================================================
29
 
30
  def get_pdf_text(pdf_files):
 
40
 
41
 
42
  # =========================================================
43
+ # 3. SPLIT TEXT INTO CHUNKS
44
  # =========================================================
45
 
46
  def get_text_chunks(text):
 
55
 
56
 
57
  # =========================================================
58
+ # 4. CREATE COHERE EMBEDDINGS
59
  # =========================================================
60
 
61
  def get_embeddings():
 
66
 
67
 
68
  # =========================================================
69
+ # 5. CREATE CONVERSATIONAL RETRIEVAL CHAIN
70
  # =========================================================
71
 
72
  def get_conversational_chain(vectorstore):
 
85
 
86
 
87
  # =========================================================
88
+ # 6. PROCESS UPLOADED DOCUMENTS
89
  # =========================================================
90
 
91
  def process_documents(pdf_files):
 
147
 
148
 
149
  # =========================================================
150
+ # 7. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT
151
  # =========================================================
152
 
153
  def convert_chat_history(history):
 
160
  role = message.get("role")
161
  content = message.get("content", "")
162
 
 
163
  if isinstance(content, list):
164
  content = " ".join(
165
  str(item.get("text", ""))
 
194
 
195
 
196
  # =========================================================
197
+ # 8. HANDLE USER QUESTIONS
198
  # =========================================================
199
 
200
  def handle_user_question(question, history, chain):
 
220
  return history, ""
221
 
222
  try:
 
223
  chat_history = convert_chat_history(history)
224
 
 
225
  response = chain.invoke({
226
  "question": question,
227
  "chat_history": chat_history
 
232
  "No answer was generated."
233
  )
234
 
 
235
  if isinstance(answer, list):
236
  answer = " ".join(
237
  str(item.get("text", ""))
 
242
  else:
243
  answer = str(answer)
244
 
 
245
  history.append({
246
  "role": "user",
247
  "content": question
 
269
 
270
 
271
  # =========================================================
272
+ # 9. CLEAR CHAT
273
  # =========================================================
274
 
275
  def clear_chat():
 
277
 
278
 
279
  # =========================================================
280
+ # 10. CUSTOM CSS
281
  # =========================================================
282
 
283
  custom_css = """
 
284
  .gradio-container {
285
  background: #0e0f14 !important;
286
  color: #f8fafc !important;
 
289
  padding: 16px !important;
290
  }
291
 
 
292
  #main-row {
293
  display: flex !important;
294
  align-items: flex-start !important;
 
297
  margin: 0 auto !important;
298
  }
299
 
 
300
  #sidebar {
301
  background: #24252f !important;
302
  border: 1px solid #343642 !important;
 
308
  margin-top: 40px;
309
  }
310
 
 
311
  #sidebar h2 {
312
  font-size: 1.15rem !important;
313
  margin: 0 0 12px 0 !important;
314
  color: #ffffff !important;
315
  }
316
 
 
317
  #sidebar [data-testid="file-upload"] {
318
  min-height: 110px !important;
319
  }
320
 
 
321
  #process-button {
322
  margin-top: 10px !important;
323
  border-radius: 8px !important;
324
+ transition: transform 0.2s ease, box-shadow 0.2s ease;
 
325
  }
326
 
 
327
  #process-button:hover {
328
  transform: translateY(-1px);
329
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
330
  }
331
 
 
332
  #status {
333
  margin-top: 10px !important;
334
  }
335
 
 
336
  #main-panel {
337
  padding: 0 8px !important;
338
  min-width: 0 !important;
339
  }
340
 
 
341
  #hero {
342
+ background: linear-gradient(120deg, #0f172a 0%, #1d4ed8 100%);
 
 
 
 
343
  padding: 28px 32px;
344
  border-radius: 18px;
345
  box-shadow: 0 10px 30px rgba(29, 78, 216, 0.18);
 
347
  transition: box-shadow 0.2s ease;
348
  }
349
 
 
350
  #hero:hover {
351
  box-shadow: 0 12px 34px rgba(29, 78, 216, 0.28);
352
  }
353
 
 
354
  #hero h1 {
355
  color: white !important;
356
  font-size: clamp(1.5rem, 2.5vw, 2.3rem) !important;
 
359
  margin: 0 !important;
360
  }
361
 
 
362
  #hero p {
363
  color: #dbeafe !important;
364
  font-size: 1rem !important;
365
  margin: 10px 0 0 0 !important;
366
  }
367
 
 
368
  #chatbot {
369
  border-radius: 12px !important;
370
  min-height: 280px !important;
 
373
  overflow: auto !important;
374
  }
375
 
 
376
  #question-box {
377
  margin-top: 12px !important;
378
  }
379
 
 
380
  #question-box textarea {
381
  min-height: 48px !important;
382
  border-radius: 10px !important;
383
  }
384
 
 
385
  #main-panel button {
386
  border-radius: 8px !important;
387
  transition: background 0.2s ease;
388
  }
389
 
 
390
  footer {
391
  display: none !important;
392
  }
393
 
 
394
  @media (max-width: 900px) {
395
  .gradio-container {
396
  padding: 12px !important;
 
414
  }
415
  }
416
 
 
417
  @media (max-width: 700px) {
418
  .gradio-container {
419
  padding: 8px !important;
 
465
 
466
 
467
  # =========================================================
468
+ # 11. BUILD GRADIO INTERFACE
469
  # =========================================================
470
 
471
+ with gr.Blocks(title="Chat with Documents") as demo:
 
 
 
 
472
 
473
+ # State to store the conversational chain
474
  chain_state = gr.State(None)
475
 
476
+ # Hidden event to satisfy ZeroGPU startup detection
477
+ hidden_button = gr.Button(visible=False)
478
+ hidden_button.click(
479
+ fn=zerogpu_check,
480
+ inputs=[],
481
+ outputs=[]
482
+ )
483
+
484
  with gr.Row(
485
  elem_id="main-row",
486
  equal_height=False
 
522
  elem_id="main-panel"
523
  ):
524
 
 
525
  gr.HTML("""
526
  <div id="hero">
527
  <h1>AI Chatbot for Your Documents</h1>
 
531
  </div>
532
  """)
533
 
 
534
  chatbot = gr.Chatbot(
535
  label="Chat with your PDFs",
536
+ type="messages",
537
  height=500,
538
  elem_id="chatbot"
539
  )
540
 
 
541
  question = gr.Textbox(
542
  label="Ask a question",
543
  placeholder="Ask a question about your PDFs",
 
546
  elem_id="question-box"
547
  )
548
 
549
+ clear_button = gr.Button("Clear Chat")
 
 
 
550
 
551
  # Process PDF documents
552
  process_button.click(
 
571
 
572
 
573
  # =========================================================
574
+ # 12. LAUNCH APPLICATION
575
  # =========================================================
576
 
577
  if __name__ == "__main__":
578
+ demo.queue().launch(
579
+ theme=gr.themes.Base(),
580
+ css=custom_css,
581
+ server_name="0.0.0.0",
582
+ server_port=7860,
583
+ ssr_mode=False
584
+ )