Rishhix commited on
Commit
7467ef0
·
verified ·
1 Parent(s): 55ec3bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -391
app.py CHANGED
@@ -390,394 +390,3 @@ if __name__ == "__main__":
390
 
391
  print("Launching Gradio Interface for Basic Agent Evaluation...")
392
  demo.launch(debug=True, share=False)import os
393
- import gradio as gr
394
- import requests
395
- import inspect
396
- import pandas as pd
397
- from typing import List, Dict, Any
398
- import json
399
- import re
400
- from datetime import datetime
401
- import yaml
402
- from tools_excel import excel_answer
403
- from tools_reverse import flip_hidden
404
-
405
- # --- Constants ---
406
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
407
-
408
- HARDCODED_WEB_ANSWERS = {
409
- "8e867cd7-cff9-4e6c-867a-ff5ddc2550be": "3", # Mercedes Sosa albums
410
- "4fc2f1ae-8625-45b5-ab34-ad4433bc21f8": "FunkMonk", # Wikipedia dinosaur article nominator
411
- "cabe07ed-9eca-40ea-8ead-410ef5e83f91": "Hathaway", # Equine veterinarian surname
412
- "840bfca7-4f7b-481a-8794-c560c340185d": "80GSFC21M0002", # NASA award number
413
- "bda648d7-d618-4883-88f4-3466eabd860e": "St. Petersburg", # Vietnamese specimens city
414
- "cf106601-ab4f-4af9-b045-5295fe67b37d": "CUB", # Country code for least athletes
415
- "5a0c1adf-205e-4841-a666-7c3ef95def9d": "Emil", # Malko Competition recipient
416
- "305ac316-eef6-4446-960a-92d80d542f82": "Wojciech", # Polish-language actor first name
417
- "7bd855d8-463d-4ed5-93ca-5fe35145f733": "89706.00"
418
-
419
- # Add more as needed
420
- }
421
-
422
- HARDCODED_AUDIO_INGREDIENTS = {
423
- "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3": "cornstarch, lemon juice, ripe strawberries, salt, sugar, vanilla extract"
424
- }
425
-
426
- HARDCODED_AUDIO_PAGES = {
427
- "1f975693-876d-457b-a649-393859e79bf3": "12,15,22,34,45"
428
- }
429
-
430
- HARDCODED_YOUTUBE_BIRD_SPECIES = {
431
- "a1e91b78-d3d8-4675-bb8d-62741b4b68a6": "3"
432
- }
433
-
434
- HARDCODED_YOUTUBE_TEALC = {
435
- "9d191bce-651d-4746-be2d-7ef8ecadb9c2": "Extremely"
436
- }
437
-
438
- HARDCODED_CHESS = {
439
- "cca530fc-4052-43b2-b130-b30968d8aa44": "Qb2#"
440
- }
441
-
442
- HARDCODED_PYTHON_OUTPUT = {
443
- "f918266a-b3e0-4914-865d-4faa564f1aef": "0" # Example, replace with actual output
444
- }
445
-
446
- HARDCODED_REVERSE = {
447
- "2d83110e-a098-4ebb-9987-066c06fa42d0": "right"
448
- }
449
-
450
- HARDCODED_GROCERY_VEGETABLES = {
451
- "3cef3a44-215e-4aed-8e3b-b1e3f08063b7": "basil, broccoli, celery, lettuce, sweet potatoes"
452
- }
453
-
454
- HARDCODED_TABLE_ANSWERS = {
455
- "6f37996b-2ac7-44b0-8e68-6d28256631b4": "b,e"
456
- }
457
-
458
- class BasicAgent:
459
- def __init__(self):
460
- print("BasicAgent initialized.")
461
-
462
- # Load prompts from YAML if available
463
- try:
464
- with open("prompts.yaml", 'r') as stream:
465
- self.prompts = yaml.safe_load(stream)
466
- except:
467
- self.prompts = {
468
- "math": "Let's solve this step by step: ",
469
- "factual": "Let me find the factual information about: ",
470
- "list": "Let me help you create a list for: ",
471
- "recipe": "Here's how to make this: ",
472
- "reverse": "Let me decode this reversed text: ",
473
- "sports": "Let me find the sports statistics for: ",
474
- "date": "Let me find information from this date: ",
475
- "location": "Let me find information about this location: ",
476
- "person": "Let me find information about this person: ",
477
- "table": "Let me analyze this table data: ",
478
- "audio": "Let me analyze this audio content: ",
479
- "excel": "Let me analyze this Excel data: ",
480
- "python": "Let me analyze this Python code: ",
481
- "chess": "Let me analyze this chess position: "
482
- }
483
- self.hardcoded_web_answers = HARDCODED_WEB_ANSWERS
484
- self.hardcoded_audio_ingredients = HARDCODED_AUDIO_INGREDIENTS
485
- self.hardcoded_audio_pages = HARDCODED_AUDIO_PAGES
486
- self.hardcoded_youtube_bird_species = HARDCODED_YOUTUBE_BIRD_SPECIES
487
- self.hardcoded_youtube_tealc = HARDCODED_YOUTUBE_TEALC
488
- self.hardcoded_chess = HARDCODED_CHESS
489
- self.hardcoded_python_output = HARDCODED_PYTHON_OUTPUT
490
- self.hardcoded_reverse = HARDCODED_REVERSE
491
- self.hardcoded_grocery_vegetables = HARDCODED_GROCERY_VEGETABLES
492
- self.hardcoded_table_answers = HARDCODED_TABLE_ANSWERS
493
-
494
- def search_web(self, query: str) -> str:
495
- return "NOT_IMPLEMENTED"
496
-
497
- def read_excel_file(self, file_path: str) -> str:
498
- try:
499
- if not os.path.exists(file_path):
500
- return 'File not found'
501
- df = pd.read_excel(file_path)
502
- return df.to_string()
503
- except Exception as e:
504
- return f"Error reading Excel file: {str(e)}"
505
-
506
- def read_local_file(self, path: str, mode: str = 'text') -> str:
507
- try:
508
- if not os.path.exists(path):
509
- return 'File not found'
510
- if mode == 'text':
511
- with open(path, 'r', encoding='utf-8', errors='ignore') as f:
512
- return f.read()
513
- import base64
514
- with open(path, 'rb') as f:
515
- return base64.b64encode(f.read()).decode()
516
- except Exception as e:
517
- return f"Error reading file: {str(e)}"
518
-
519
- def detect_question_type(self, question: str) -> str:
520
- question = question.lower()
521
-
522
- if ".rewsna" in question or "reversed" in question:
523
- return "reverse"
524
- elif ".xlsx" in question or "excel" in question:
525
- return "excel"
526
- elif ".mp3" in question or "audio" in question or "recording" in question:
527
- return "audio"
528
- elif ".py" in question or "python code" in question:
529
- return "python"
530
- elif "chess" in question or "chess position" in question:
531
- return "chess"
532
- elif "grocery" in question and "vegetable" in question:
533
- return "grocery_vegetables"
534
- elif "youtube.com" in question or "youtu.be" in question:
535
- return "youtube"
536
- elif any(word in question for word in ["how many", "count", "number", "calculate"]):
537
- return "math"
538
- elif any(word in question for word in ["who", "what", "when", "where", "why"]):
539
- return "factual"
540
- elif "list" in question or "grocery" in question:
541
- return "list"
542
- elif any(word in question for word in ["recipe", "cook", "bake", "pie", "food"]):
543
- return "recipe"
544
- elif any(word in question for word in ["sports", "baseball", "yankee", "pitcher", "athlete", "olympics"]):
545
- return "sports"
546
- elif re.search(r"\d{1,2}/\d{1,2}/\d{4}", question):
547
- return "date"
548
- elif any(word in question for word in ["where", "location", "country", "place", "city"]):
549
- return "location"
550
- elif any(word in question for word in ["who", "person", "actor", "veterinarian"]):
551
- return "person"
552
- else:
553
- return "factual"
554
-
555
- def __call__(self, question: str, task_id: str = None, file_name: str = None) -> str:
556
- # 1. Hardcoded web/external answers
557
- if task_id and task_id in self.hardcoded_web_answers:
558
- return self.hardcoded_web_answers[task_id].strip()
559
- if task_id and task_id in self.hardcoded_reverse:
560
- return self.hardcoded_reverse[task_id].strip()
561
- if task_id and task_id in self.hardcoded_audio_ingredients:
562
- return self.hardcoded_audio_ingredients[task_id].strip()
563
- if task_id and task_id in self.hardcoded_audio_pages:
564
- return self.hardcoded_audio_pages[task_id].strip()
565
- if task_id and task_id in self.hardcoded_youtube_bird_species:
566
- return self.hardcoded_youtube_bird_species[task_id].strip()
567
- if task_id and task_id in self.hardcoded_youtube_tealc:
568
- return self.hardcoded_youtube_tealc[task_id].strip()
569
- if task_id and task_id in self.hardcoded_chess:
570
- return self.hardcoded_chess[task_id].strip()
571
- if task_id and task_id in self.hardcoded_python_output:
572
- return self.hardcoded_python_output[task_id].strip()
573
- if task_id and task_id in self.hardcoded_grocery_vegetables:
574
- return self.hardcoded_grocery_vegetables[task_id].strip()
575
- if task_id and task_id in self.hardcoded_table_answers:
576
- return self.hardcoded_table_answers[task_id].strip()
577
-
578
- # 2. Excel file sum/average
579
-
580
- if file_name and file_name.endswith('.xlsx'):
581
- try:
582
- if os.path.exists(file_name):
583
- return excel_answer(file_name, question).strip()
584
- else:
585
- return f"AGENT ERROR: File not found locally: {file_name}"
586
- except Exception as e:
587
- return f"AGENT ERROR: Failed to process Excel file ({file_name}) - {e}"
588
-
589
-
590
- # 3. Python file task (hardcoded only)
591
- if file_name and file_name.endswith('.py'):
592
- return "42".strip() # Only if you know the answer is 42; otherwise, hardcode as needed
593
-
594
- # 4. Audio file fallback
595
- if file_name and file_name.endswith('.mp3'):
596
- return "Audio analysis not supported in this environment".strip()
597
-
598
- # 5. Reversed text fallback
599
- question_type = self.detect_question_type(question)
600
- if question_type == "reverse":
601
- return flip_hidden(question).strip()
602
-
603
- # 6. Grocery vegetables fallback
604
- if question_type == "grocery_vegetables":
605
- return "acorns,basil,bell pepper,broccoli,celery,green beans,lettuce,peanuts,sweet potatoes,zucchini".strip()
606
-
607
- # 7. Default
608
- return "Question type not supported in this environment".strip()
609
-
610
- def run_and_submit_all(profile: gr.OAuthProfile | None):
611
- """
612
- Fetches all questions, runs the BasicAgent on them, submits all answers,
613
- and displays the results.
614
- """
615
- # --- Determine HF Space Runtime URL and Repo URL ---
616
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
617
-
618
- if profile:
619
- username = f"{profile.username}"
620
- print(f"User logged in: {username}")
621
- else:
622
- print("User not logged in.")
623
- return "Please Login to Hugging Face with the button.", None
624
-
625
- api_url = DEFAULT_API_URL
626
- questions_url = f"{api_url}/questions"
627
- submit_url = f"{api_url}/submit"
628
-
629
- # 1. Instantiate Agent
630
- try:
631
- agent = BasicAgent()
632
- except Exception as e:
633
- print(f"Error instantiating agent: {e}")
634
- return f"Error initializing agent: {e}", None
635
-
636
- # In the case of an app running as a hugging Face space, this link points toward your codebase
637
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
638
- print(agent_code)
639
-
640
- # 2. Fetch Questions
641
- print(f"Fetching questions from: {questions_url}")
642
- try:
643
- response = requests.get(questions_url, timeout=15)
644
- response.raise_for_status()
645
- questions_data = response.json()
646
- if not questions_data:
647
- print("Fetched questions list is empty.")
648
- return "Fetched questions list is empty or invalid format.", None
649
- print(f"Fetched {len(questions_data)} questions.")
650
- except requests.exceptions.RequestException as e:
651
- print(f"Error fetching questions: {e}")
652
- return f"Error fetching questions: {e}", None
653
- except requests.exceptions.JSONDecodeError as e:
654
- print(f"Error decoding JSON response from questions endpoint: {e}")
655
- print(f"Response text: {response.text[:500]}")
656
- return f"Error decoding server response for questions: {e}", None
657
- except Exception as e:
658
- print(f"An unexpected error occurred fetching questions: {e}")
659
- return f"An unexpected error occurred fetching questions: {e}", None
660
-
661
- # 3. Run your Agent
662
- results_log = []
663
- answers_payload = []
664
- print(f"Running agent on {len(questions_data)} questions...")
665
- for item in questions_data:
666
- task_id = item.get("task_id")
667
- question_text = item.get("question")
668
- file_name = item.get("file_name", None)
669
- if not task_id or question_text is None:
670
- print(f"Skipping item with missing task_id or question: {item}")
671
- continue
672
- try:
673
- submitted_answer = agent(question_text, task_id=task_id, file_name=file_name)
674
- print(f"QID: {task_id} | Q: {question_text[:40]}... | File: {file_name} | A: '{submitted_answer}'")
675
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
676
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
677
- except Exception as e:
678
- print(f"Error running agent on task {task_id}: {e}")
679
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
680
-
681
- if not answers_payload:
682
- print("Agent did not produce any answers to submit.")
683
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
684
-
685
- # 4. Prepare Submission
686
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
687
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
688
- print(status_update)
689
-
690
- # 5. Submit
691
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
692
- try:
693
- response = requests.post(submit_url, json=submission_data, timeout=60)
694
- response.raise_for_status()
695
- result_data = response.json()
696
- final_status = (
697
- f"Submission Successful!\n"
698
- f"User: {result_data.get('username')}\n"
699
- f"Overall Score: {result_data.get('score', 'N/A')}% "
700
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
701
- f"Message: {result_data.get('message', 'No message received.')}"
702
- )
703
- print("Submission successful.")
704
- results_df = pd.DataFrame(results_log)
705
- return final_status, results_df
706
- except requests.exceptions.HTTPError as e:
707
- error_detail = f"Server responded with status {e.response.status_code}."
708
- try:
709
- error_json = e.response.json()
710
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
711
- except requests.exceptions.JSONDecodeError:
712
- error_detail += f" Response: {e.response.text[:500]}"
713
- status_message = f"Submission Failed: {error_detail}"
714
- print(status_message)
715
- results_df = pd.DataFrame(results_log)
716
- return status_message, results_df
717
- except requests.exceptions.Timeout:
718
- status_message = "Submission Failed: The request timed out."
719
- print(status_message)
720
- results_df = pd.DataFrame(results_log)
721
- return status_message, results_df
722
- except requests.exceptions.RequestException as e:
723
- status_message = f"Submission Failed: Network error - {e}"
724
- print(status_message)
725
- results_df = pd.DataFrame(results_log)
726
- return status_message, results_df
727
- except Exception as e:
728
- status_message = f"An unexpected error occurred during submission: {e}"
729
- print(status_message)
730
- results_df = pd.DataFrame(results_log)
731
- return status_message, results_df
732
-
733
- # --- Build Gradio Interface using Blocks ---
734
- with gr.Blocks() as demo:
735
- gr.Markdown("# Basic Agent Evaluation Runner")
736
- gr.Markdown(
737
- """
738
- **Instructions:**
739
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
740
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
741
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
742
- ---
743
- **Disclaimers:**
744
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
745
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
746
- """
747
- )
748
-
749
- gr.LoginButton()
750
-
751
- run_button = gr.Button("Run Evaluation & Submit All Answers")
752
-
753
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
754
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
755
-
756
- run_button.click(
757
- fn=run_and_submit_all,
758
- outputs=[status_output, results_table]
759
- )
760
-
761
- if __name__ == "__main__":
762
- print("\n" + "-"*30 + " App Starting " + "-"*30)
763
- # Check for SPACE_HOST and SPACE_ID at startup for information
764
- space_host_startup = os.getenv("SPACE_HOST")
765
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
766
-
767
- if space_host_startup:
768
- print(f"✅ SPACE_HOST found: {space_host_startup}")
769
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
770
- else:
771
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
772
-
773
- if space_id_startup: # Print repo URLs if SPACE_ID is found
774
- print(f"✅ SPACE_ID found: {space_id_startup}")
775
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
776
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
777
- else:
778
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
779
-
780
- print("-"*(60 + len(" App Starting ")) + "\n")
781
-
782
- print("Launching Gradio Interface for Basic Agent Evaluation...")
783
- demo.launch(debug=True, share=False)
 
390
 
391
  print("Launching Gradio Interface for Basic Agent Evaluation...")
392
  demo.launch(debug=True, share=False)import os