AiActivity commited on
Commit
5fd0efc
Β·
verified Β·
1 Parent(s): 402ee99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -41
app.py CHANGED
@@ -1,5 +1,3 @@
1
-
2
-
3
  import gradio as gr
4
  import os
5
  import torch
@@ -214,6 +212,12 @@ def generate_related_topics(query):
214
  "Impact of climate change on ecosystems",
215
  "Renewable energy technologies"
216
  ]
 
 
 
 
 
 
217
  else:
218
  # Generate simple variations for any query
219
  return [
@@ -293,14 +297,14 @@ def format_sources(sources):
293
  html = ""
294
  for i, source in enumerate(sources, 1):
295
  html += f"""
296
- <div style="margin-bottom: 15px; padding: 15px; background-color: #f8f9fa;
297
- border-radius: 8px; border-left: 4px solid #1976d2;">
298
- <a href="{source['url']}" target="_blank" style="font-weight: bold;
299
- color: #1976d2; text-decoration: none;">
300
  {source['title']}
301
  </a>
302
- <div style="color: #5f6368; font-size: 14px; margin-top: 5px;">{source['url']}</div>
303
- <div style="margin-top: 10px;">{source['snippet']}</div>
304
  </div>
305
  """
306
  return html
@@ -310,12 +314,16 @@ def format_related(topics):
310
  if not topics:
311
  return ""
312
 
313
- html = "<div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px;'>"
314
  for topic in topics:
 
315
  html += f"""
316
- <div style="background-color: #e3f2fd; padding: 8px 16px; border-radius: 20px;
317
- color: #1976d2; font-size: 14px; cursor: pointer; display: inline-block;"
318
- onclick="document.getElementById('query-input').value = '{topic}'; search();">
 
 
 
319
  {topic}
320
  </div>
321
  """
@@ -333,6 +341,9 @@ def search_interface(query):
333
 
334
  start_time = time.time()
335
 
 
 
 
336
  # Perform search and answer generation
337
  result = search_and_answer(query)
338
 
@@ -349,69 +360,189 @@ def search_interface(query):
349
  processing_time = time.time() - start_time
350
  print(f"Query processed in {processing_time:.2f} seconds")
351
 
352
- return (
353
  answer_html,
354
  sources_html,
355
  related_html
356
  )
357
 
358
- # Create the Gradio interface
359
  css = """
 
360
  body {
361
- font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
 
 
 
 
 
 
 
362
  max-width: 1200px;
363
  margin: 0 auto;
 
364
  }
365
- .container {
366
- margin-top: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  }
 
 
368
  .answer {
369
- border-radius: 8px;
370
- background-color: white;
371
- padding: 20px;
372
- box-shadow: 0 1px 3px rgba(0,0,0,0.12);
373
- margin-bottom: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  }
375
- h1 {
376
- color: #1976d2;
377
- font-size: 2.2rem;
378
  font-weight: 600;
379
- margin-bottom: 10px;
380
  }
381
- h3 {
382
- color: #1976d2;
383
- font-weight: 500;
384
- margin-top: 25px;
385
- margin-bottom: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  }
387
  """
388
 
389
- with gr.Blocks(css=css) as demo:
 
390
  gr.HTML("""
391
- <h1>πŸ” AI Search System</h1>
392
- <p style="margin-bottom: 20px;">Get accurate answers with sources for any question</p>
 
 
 
 
393
  """)
394
 
395
- with gr.Row():
 
396
  query_input = gr.Textbox(
397
  label="Search Query",
398
- placeholder="Enter your search query here...",
399
- elem_id="query-input"
 
400
  )
401
- search_button = gr.Button("Search πŸ”", variant="primary")
402
 
 
403
  with gr.Row():
 
404
  with gr.Column(scale=2):
 
405
  gr.HTML("<h3>πŸ“ Answer</h3>")
406
  answer_output = gr.HTML(elem_classes=["answer"])
407
 
 
408
  gr.HTML("<h3>πŸ”— Related Topics</h3>")
409
  related_output = gr.HTML()
410
 
 
411
  with gr.Column(scale=1):
412
  gr.HTML("<h3>πŸ“š Sources</h3>")
413
  sources_output = gr.HTML()
414
 
 
415
  search_button.click(
416
  fn=search_interface,
417
  inputs=[query_input],
@@ -424,10 +555,11 @@ with gr.Blocks(css=css) as demo:
424
  outputs=[answer_output, sources_output, related_output]
425
  )
426
 
 
427
  gr.HTML("""
428
- <div style="margin-top: 20px; text-align: center; color: #666;">
429
- <p>Built with Hugging Face Spaces</p>
430
- </div>
431
  """)
432
 
433
  # Launch app with queue to prevent overloading
 
 
 
1
  import gradio as gr
2
  import os
3
  import torch
 
212
  "Impact of climate change on ecosystems",
213
  "Renewable energy technologies"
214
  ]
215
+ elif "computer" in query_lower:
216
+ return [
217
+ "History of computers",
218
+ "Latest developments in computer technology",
219
+ "How do computers work?"
220
+ ]
221
  else:
222
  # Generate simple variations for any query
223
  return [
 
297
  html = ""
298
  for i, source in enumerate(sources, 1):
299
  html += f"""
300
+ <div style="margin-bottom: 15px; padding: 15px; background-color: #FFFFFF;
301
+ border-radius: 12px; border-left: 4px solid #2563EB; box-shadow: 0 2px 6px rgba(0,0,0,0.08);">
302
+ <a href="{source['url']}" target="_blank" style="font-weight: 600;
303
+ color: #2563EB; text-decoration: none; font-size: 16px;">
304
  {source['title']}
305
  </a>
306
+ <div style="color: #64748B; font-size: 14px; margin-top: 6px;">{source['url']}</div>
307
+ <div style="margin-top: 10px; color: #374151; line-height: 1.5;">{source['snippet']}</div>
308
  </div>
309
  """
310
  return html
 
314
  if not topics:
315
  return ""
316
 
317
+ html = "<div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px;'>"
318
  for topic in topics:
319
+ # Each topic becomes a button that triggers a new search when clicked
320
  html += f"""
321
+ <div style="background-color: #EFF6FF; padding: 10px 16px; border-radius: 100px;
322
+ color: #2563EB; font-size: 14px; font-weight: 500; cursor: pointer; display: inline-block;
323
+ transition: all 0.2s ease; border: 1px solid #DBEAFE; box-shadow: 0 1px 2px rgba(0,0,0,0.05);"
324
+ onclick="document.getElementById('query-input').value = '{topic}'; document.querySelector('button[data-testid=\\\"submit\\\"]').click();"
325
+ onmouseover="this.style.backgroundColor='#DBEAFE'; this.style.boxShadow='0 2px 5px rgba(0,0,0,0.1)';"
326
+ onmouseout="this.style.backgroundColor='#EFF6FF'; this.style.boxShadow='0 1px 2px rgba(0,0,0,0.05)';">
327
  {topic}
328
  </div>
329
  """
 
341
 
342
  start_time = time.time()
343
 
344
+ # Show loading message while processing
345
+ yield ("Searching...", "", "")
346
+
347
  # Perform search and answer generation
348
  result = search_and_answer(query)
349
 
 
360
  processing_time = time.time() - start_time
361
  print(f"Query processed in {processing_time:.2f} seconds")
362
 
363
+ yield (
364
  answer_html,
365
  sources_html,
366
  related_html
367
  )
368
 
369
+ # Create the Gradio interface with modern UI
370
  css = """
371
+ /* Global styles */
372
  body {
373
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
374
+ background-color: #F9FAFB;
375
+ color: #1F2937;
376
+ line-height: 1.6;
377
+ }
378
+
379
+ /* Container styling */
380
+ .container {
381
  max-width: 1200px;
382
  margin: 0 auto;
383
+ padding: 0 20px;
384
  }
385
+
386
+ /* Header styling */
387
+ .header {
388
+ text-align: center;
389
+ margin-bottom: 2rem;
390
+ }
391
+
392
+ /* Search box styling */
393
+ #search-container input {
394
+ border: 1px solid #E5E7EB;
395
+ border-radius: 12px;
396
+ padding: 12px 20px;
397
+ font-size: 16px;
398
+ box-shadow: 0 1px 3px rgba(0,0,0,0.1);
399
+ transition: all 0.2s ease;
400
+ }
401
+
402
+ #search-container input:focus {
403
+ border-color: #2563EB;
404
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
405
+ outline: none;
406
+ }
407
+
408
+ /* Button styling */
409
+ button[data-testid="submit"] {
410
+ background-color: #2563EB !important;
411
+ color: white !important;
412
+ font-weight: 600 !important;
413
+ border-radius: 12px !important;
414
+ padding: 12px 24px !important;
415
+ border: none !important;
416
+ cursor: pointer !important;
417
+ transition: all 0.2s ease !important;
418
+ box-shadow: 0 2px 5px rgba(37, 99, 235, 0.3) !important;
419
+ }
420
+
421
+ button[data-testid="submit"]:hover {
422
+ background-color: #1D4ED8 !important;
423
+ box-shadow: 0 4px 8px rgba(37, 99, 235, 0.4) !important;
424
+ transform: translateY(-1px) !important;
425
+ }
426
+
427
+ /* Section headers */
428
+ h3 {
429
+ color: #2563EB;
430
+ font-weight: 600;
431
+ margin-top: 2rem;
432
+ margin-bottom: 1rem;
433
+ font-size: 1.25rem;
434
+ border-bottom: 2px solid #DBEAFE;
435
+ padding-bottom: 0.5rem;
436
  }
437
+
438
+ /* Answer box styling */
439
  .answer {
440
+ background-color: #FFFFFF;
441
+ padding: 24px;
442
+ border-radius: 12px;
443
+ box-shadow: 0 2px 6px rgba(0,0,0,0.05);
444
+ border: 1px solid #E5E7EB;
445
+ line-height: 1.7;
446
+ margin-bottom: 1.5rem;
447
+ color: #374151;
448
+ min-height: 100px;
449
+ }
450
+
451
+ .answer p {
452
+ margin-bottom: 1rem;
453
+ color: #1F2937;
454
+ }
455
+
456
+ .answer ul, .answer ol {
457
+ margin-left: 1.5rem;
458
+ margin-bottom: 1rem;
459
  }
460
+
461
+ .answer strong, .answer b {
462
+ color: #111827;
463
  font-weight: 600;
 
464
  }
465
+
466
+ .answer a {
467
+ color: #2563EB;
468
+ text-decoration: none;
469
+ border-bottom: 1px solid currentColor;
470
+ }
471
+
472
+ /* Empty answer styling */
473
+ .answer:empty::after {
474
+ content: 'Enter a search query to see results';
475
+ color: #9CA3AF;
476
+ font-style: italic;
477
+ }
478
+
479
+ /* Loading state */
480
+ .answer.loading {
481
+ display: flex;
482
+ align-items: center;
483
+ justify-content: center;
484
+ }
485
+
486
+ /* Footer styling */
487
+ footer {
488
+ margin-top: 2rem;
489
+ text-align: center;
490
+ color: #6B7280;
491
+ font-size: 0.875rem;
492
+ padding: 1rem 0;
493
+ }
494
+
495
+ /* Responsive styles */
496
+ @media (max-width: 768px) {
497
+ .answer {
498
+ padding: 16px;
499
+ }
500
+
501
+ button[data-testid="submit"] {
502
+ padding: 10px 16px !important;
503
+ }
504
  }
505
  """
506
 
507
+ with gr.Blocks(css=css, theme=gr.themes.Default()) as demo:
508
+ # Custom header with improved design
509
  gr.HTML("""
510
+ <div class="header">
511
+ <h1 style="color: #2563EB; font-size: 2.2rem; font-weight: 700; margin-bottom: 0.5rem;">πŸ” AI Search System</h1>
512
+ <p style="color: #64748B; font-size: 1.1rem; max-width: 600px; margin: 0 auto;">
513
+ Get comprehensive answers with reliable sources for any question you have.
514
+ </p>
515
+ </div>
516
  """)
517
 
518
+ # Search container with improved styling
519
+ with gr.Row(elem_id="search-container"):
520
  query_input = gr.Textbox(
521
  label="Search Query",
522
+ placeholder="What would you like to know?",
523
+ elem_id="query-input",
524
+ scale=4
525
  )
526
+ search_button = gr.Button("Search πŸ”", variant="primary", scale=1)
527
 
528
+ # Results container with improved layout
529
  with gr.Row():
530
+ # Left column for answer and related topics
531
  with gr.Column(scale=2):
532
+ # Answer section with better styling
533
  gr.HTML("<h3>πŸ“ Answer</h3>")
534
  answer_output = gr.HTML(elem_classes=["answer"])
535
 
536
+ # Related topics with better styling
537
  gr.HTML("<h3>πŸ”— Related Topics</h3>")
538
  related_output = gr.HTML()
539
 
540
+ # Right column for sources
541
  with gr.Column(scale=1):
542
  gr.HTML("<h3>πŸ“š Sources</h3>")
543
  sources_output = gr.HTML()
544
 
545
+ # Set up event handlers with progress indicators
546
  search_button.click(
547
  fn=search_interface,
548
  inputs=[query_input],
 
555
  outputs=[answer_output, sources_output, related_output]
556
  )
557
 
558
+ # Footer with attribution
559
  gr.HTML("""
560
+ <footer>
561
+ <p>Built with Hugging Face Spaces & Microsoft Phi-2</p>
562
+ </footer>
563
  """)
564
 
565
  # Launch app with queue to prevent overloading