svar-chandak commited on
Commit
babaed1
Β·
unverified Β·
2 Parent(s): 6810e51 310797f

Merge pull request #47 from NexDatawork/SC-Branch

Browse files
Files changed (1) hide show
  1. examples/app.py +131 -75
examples/app.py CHANGED
@@ -323,10 +323,18 @@ def sql_pipeline(tables,question,history):
323
  print("="*10+"\nSQL_PIPELINE\n"+"="*10)
324
  db = create_db(tables) #uploads the files added by the user and puts them in a database
325
 
 
 
 
326
  if not os.path.exists("database.db"):
327
  print("Database doesn't exist")
328
- return "Database doesn't exist"
329
- llm, tools = start_llm(db) #returns the agent and the tools for working with the database
 
 
 
 
 
330
  try:
331
  agent_executor = create_react_agent(llm, tools, prompt=system_message+sql_suffix_prompt)
332
  output = ""
@@ -339,7 +347,7 @@ def sql_pipeline(tables,question,history):
339
  final_answer = extract_code(output)
340
  return history + final_answer, final_answer
341
  except Exception as e:
342
- return f"SQL agent error: {e}"
343
 
344
  """THe following block is responsible for creating a smart ETL pipeline"""
345
 
@@ -376,28 +384,31 @@ def generate_python_code(transform_description: str) -> str:
376
  #llm is the agent that creates the etl pipeline
377
  #dataframe is a string with the name of the dataframe push through the etl process
378
  def etl_pipeline(dataframe,history):
379
- tools = [preview_data, suggest_transformation, generate_python_code]
380
-
381
- agent = initialize_agent(tools, model, agent='zero-shot-react-description',verbose=True)
382
-
383
- input_prompt = f"""
384
- Preview the table {dataframe} and \
385
- generate Python code to read the table, clean it, and finally write the \
386
- dataframe into a table called {'Cleaned_'+dataframe}]. \
387
- Do not stop the Python session
388
- """
389
-
390
- # Preview + suggest + generate code in a single run
391
- response = agent.run({
392
- "input": input_prompt,
393
- "chat_history": [],
394
- "handle_parsing_errors": True
395
- })
396
-
397
- print("Generated Python Code:\n")
398
- print(response)
399
- response2 = response.strip('`').replace('python', '')
400
- return history + response2, response2
 
 
 
401
 
402
  """The following code is responsible for AI web scraping agent"""
403
 
@@ -422,7 +433,7 @@ def web_scraping(question,history):
422
  trace = buffer.getvalue() #the trace of the agent is saved in the trace variable
423
  return history + response, response
424
  except Exception as e:
425
- return f'Web scraping error {e}',f'Web scraping error {e}',""
426
 
427
  """The next section creates a web interface using Gradio, providing a user-friendly way to analyze data and create SQL queries.
428
  ```
@@ -453,79 +464,124 @@ For debugging use `debug=True` in order to see the messages in the console.
453
  with gr.Blocks(
454
  css="""
455
  body, .gradio-container {
456
- background: #ffffff !important;
457
- color: #1f2937 !important;
458
- font-family: 'Segoe UI', sans-serif;
 
459
  }
460
  #title {
461
- color: #1f2937 !important;
462
- font-size: 2rem;
463
- font-weight: 600;
464
  text-align: center;
465
- padding-top: 20px;
466
- padding-bottom: 10px;
467
  }
468
- .gr-box, .gr-input, .gr-output, .gr-markdown, .gr-textbox, .gr-file, textarea, input {
469
- background: rgba(0, 0, 0, 0.04) !important;
470
- border: 1px solid rgba(0, 0, 0, 0.1);
 
 
 
 
 
 
471
  border-radius: 12px !important;
472
- color: #1f2937 !important;
 
 
 
 
 
 
 
 
473
  }
474
  .trace-markdown {
475
  height: 400px !important;
476
- overflow-y: scroll;
477
  resize: none;
 
478
  }
479
  textarea::placeholder, input::placeholder {
480
- color: rgba(31, 41, 55, 0.6) !important;
481
  }
482
- button {
483
- background: rgba(0, 0, 0, 0.07) !important;
484
- color: #1f2937 !important;
485
- border: 1px solid rgba(0, 0, 0, 0.15) !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
  border-radius: 8px !important;
 
 
 
487
  }
488
- button:hover {
489
- background: rgba(0, 0, 0, 0.15) !important;
 
 
 
 
490
  }
491
  """
492
  ) as demo:
493
 
494
  gr.Markdown("<h2 id='title'>πŸ“Š NexDatawork Data Agent</h2>")
 
495
 
496
  with gr.Column():
497
-
498
- result_display = gr.Markdown(label="πŸ“Œ Report Output (Markdown)")
499
-
500
- with gr.Row():
501
-
502
- trace_display = gr.Markdown(label="πŸ› οΈ Data Agent Reasoning - Your Explainable Agent", elem_classes=["trace-markdown"])
503
-
504
- sql_display = gr.Markdown(label='SQL Process')
505
-
 
 
506
 
507
  with gr.Row(equal_height=True):
508
-
509
- file_input = gr.File(label="πŸ“ Upload CSV(s)", file_types=[".csv"], file_count="multiple",height=120)
510
-
511
- question_input = gr.Textbox(label="πŸ’¬ Ask Your Agent",placeholder="e.g., What is the trend for revenue over time?",lines=2)
 
 
 
 
 
 
 
 
 
 
512
 
513
  with gr.Row():
514
-
515
- ask_button = gr.Button("πŸ’‘ Analyze")
516
-
517
- with gr.Row():
518
-
519
- sql_button = gr.Button('Create Query')
520
-
521
- scraping_button = gr.Button('Find the answer online')
522
-
523
- history = gr.State(value="")
524
-
525
- sql_button.click(fn=sql_pipeline,inputs=[file_input,question_input,history],outputs = [trace_display,history])
526
-
527
- scraping_button.click(fn=web_scraping,inputs=[question_input,history],outputs = [trace_display,history])
528
-
529
- ask_button.click(fn=ask_agent,inputs=[file_input, question_input,history],outputs=[trace_display,history])
530
 
531
  demo.launch(share=True,debug=False)
 
323
  print("="*10+"\nSQL_PIPELINE\n"+"="*10)
324
  db = create_db(tables) #uploads the files added by the user and puts them in a database
325
 
326
+ if isinstance(db, str): # Error message returned
327
+ return f"❌ {db}", history
328
+
329
  if not os.path.exists("database.db"):
330
  print("Database doesn't exist")
331
+ return "❌ Database doesn't exist", history
332
+
333
+ result = start_llm(db) #returns the agent and the tools for working with the database
334
+ if isinstance(result, str): # Error message returned
335
+ return f"❌ {result}", history
336
+
337
+ llm, tools = result
338
  try:
339
  agent_executor = create_react_agent(llm, tools, prompt=system_message+sql_suffix_prompt)
340
  output = ""
 
347
  final_answer = extract_code(output)
348
  return history + final_answer, final_answer
349
  except Exception as e:
350
+ return f"❌ SQL agent error: {e}", history
351
 
352
  """THe following block is responsible for creating a smart ETL pipeline"""
353
 
 
384
  #llm is the agent that creates the etl pipeline
385
  #dataframe is a string with the name of the dataframe push through the etl process
386
  def etl_pipeline(dataframe,history):
387
+ try:
388
+ tools = [preview_data, suggest_transformation, generate_python_code]
389
+
390
+ agent = initialize_agent(tools, model, agent='zero-shot-react-description',verbose=True)
391
+
392
+ input_prompt = f"""
393
+ Preview the table {dataframe} and \
394
+ generate Python code to read the table, clean it, and finally write the \
395
+ dataframe into a table called {'Cleaned_'+dataframe}]. \
396
+ Do not stop the Python session
397
+ """
398
+
399
+ # Preview + suggest + generate code in a single run
400
+ response = agent.run({
401
+ "input": input_prompt,
402
+ "chat_history": [],
403
+ "handle_parsing_errors": True
404
+ })
405
+
406
+ print("Generated Python Code:\n")
407
+ print(response)
408
+ response2 = response.strip('`').replace('python', '')
409
+ return history + response2, response2
410
+ except Exception as e:
411
+ return f"❌ ETL pipeline error: {e}", history
412
 
413
  """The following code is responsible for AI web scraping agent"""
414
 
 
433
  trace = buffer.getvalue() #the trace of the agent is saved in the trace variable
434
  return history + response, response
435
  except Exception as e:
436
+ return f'❌ Web scraping error: {e}', history
437
 
438
  """The next section creates a web interface using Gradio, providing a user-friendly way to analyze data and create SQL queries.
439
  ```
 
464
  with gr.Blocks(
465
  css="""
466
  body, .gradio-container {
467
+ background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%) !important;
468
+ color: #1e293b !important;
469
+ font-family: 'Inter', 'SF Pro Display', -apple-system, sans-serif;
470
+ min-height: 100vh;
471
  }
472
  #title {
473
+ color: #0f172a !important;
474
+ font-size: 2.25rem;
475
+ font-weight: 700;
476
  text-align: center;
477
+ padding: 24px 0 8px 0;
478
+ letter-spacing: -0.025em;
479
  }
480
+ #subtitle {
481
+ text-align: center;
482
+ color: #64748b !important;
483
+ font-size: 1rem;
484
+ margin-bottom: 20px;
485
+ }
486
+ .instructions-box {
487
+ background: linear-gradient(135deg, #dbeafe 0%, #e0e7ff 100%) !important;
488
+ border: 1px solid #93c5fd !important;
489
  border-radius: 12px !important;
490
+ padding: 16px !important;
491
+ margin-bottom: 16px !important;
492
+ }
493
+ .gr-box, .gr-input, .gr-output, .gr-markdown, .gr-textbox, .gr-file, textarea, input {
494
+ background: #ffffff !important;
495
+ border: 1px solid #e2e8f0 !important;
496
+ border-radius: 10px !important;
497
+ color: #1e293b !important;
498
+ box-shadow: 0 1px 3px rgba(0,0,0,0.05);
499
  }
500
  .trace-markdown {
501
  height: 400px !important;
502
+ overflow-y: auto;
503
  resize: none;
504
+ background: #ffffff !important;
505
  }
506
  textarea::placeholder, input::placeholder {
507
+ color: #94a3b8 !important;
508
  }
509
+ .primary-btn {
510
+ background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
511
+ color: #ffffff !important;
512
+ border: none !important;
513
+ border-radius: 8px !important;
514
+ font-weight: 600 !important;
515
+ padding: 10px 24px !important;
516
+ transition: all 0.2s ease !important;
517
+ }
518
+ .primary-btn:hover {
519
+ background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%) !important;
520
+ transform: translateY(-1px);
521
+ box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4) !important;
522
+ }
523
+ .secondary-btn {
524
+ background: #ffffff !important;
525
+ color: #475569 !important;
526
+ border: 1px solid #cbd5e1 !important;
527
  border-radius: 8px !important;
528
+ font-weight: 500 !important;
529
+ padding: 10px 24px !important;
530
+ transition: all 0.2s ease !important;
531
  }
532
+ .secondary-btn:hover {
533
+ background: #f8fafc !important;
534
+ border-color: #94a3b8 !important;
535
+ }
536
+ .button-row {
537
+ gap: 12px !important;
538
  }
539
  """
540
  ) as demo:
541
 
542
  gr.Markdown("<h2 id='title'>πŸ“Š NexDatawork Data Agent</h2>")
543
+ gr.Markdown("<p id='subtitle'>AI-powered data analysis without writing code</p>")
544
 
545
  with gr.Column():
546
+
547
+ # Instructions Section
548
+ gr.Markdown("""
549
+ ### πŸ“‹ Instructions
550
+ 1. **Upload CSV Files** β€” Drag & drop or click to upload one or more CSV files
551
+ 2. **Ask Your Question** β€” Type your data analysis question in natural language
552
+ 3. **Choose an Action:**
553
+ - **Analyze Data** β€” Get AI-powered insights and analysis from your data
554
+ - **Generate SQL** β€” Create SQL queries based on your question
555
+ - **Web Scraping** β€” Find relevant data from the web
556
+ """, elem_classes=["instructions-box"])
557
 
558
  with gr.Row(equal_height=True):
559
+ file_input = gr.File(label="πŸ“ Upload CSV Files", file_types=[".csv"], file_count="multiple", height=140)
560
+ question_input = gr.Textbox(
561
+ label="πŸ’¬ Ask Your Question",
562
+ placeholder="e.g., What is the trend for revenue over time? Show me top 10 customers by sales.",
563
+ lines=4
564
+ )
565
+
566
+ # Buttons aligned to the left
567
+ with gr.Row(elem_classes=["button-row"]):
568
+ ask_button = gr.Button("πŸ” Analyze Data", elem_classes=["primary-btn"])
569
+ sql_button = gr.Button("πŸ—„οΈ Generate SQL", elem_classes=["secondary-btn"])
570
+ scraping_button = gr.Button("🌐 Web Scraping", elem_classes=["secondary-btn"])
571
+
572
+ history = gr.State(value="")
573
 
574
  with gr.Row():
575
+ with gr.Column():
576
+ gr.Markdown("### πŸ“ˆ Analysis Results")
577
+ trace_display = gr.Markdown(elem_classes=["trace-markdown"])
578
+ with gr.Column():
579
+ gr.Markdown("### πŸ—ƒοΈ SQL / ETL Output")
580
+ sql_display = gr.Markdown(elem_classes=["trace-markdown"])
581
+
582
+ # Event handlers
583
+ ask_button.click(fn=ask_agent, inputs=[file_input, question_input, history], outputs=[trace_display, history])
584
+ sql_button.click(fn=sql_pipeline, inputs=[file_input, question_input, history], outputs=[sql_display, history])
585
+ scraping_button.click(fn=web_scraping, inputs=[question_input, history], outputs=[trace_display, history])
 
 
 
 
 
586
 
587
  demo.launch(share=True,debug=False)