FD900 commited on
Commit
1fdeb35
·
verified ·
1 Parent(s): 4c863b1

Upload 3 files

Browse files
Files changed (3) hide show
  1. agent.py +51 -0
  2. app.py +90 -0
  3. requirements.txt +6 -0
agent.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from duckduckgo_search import DDGS
3
+ import os
4
+
5
+ class BasicAgent:
6
+ def __init__(self):
7
+ model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
8
+ print(f"Loading model: {model_id}")
9
+ self.llm = pipeline(
10
+ "text-generation",
11
+ model=model_id,
12
+ token=os.getenv("HF_TOKEN"),
13
+ model_kwargs={"temperature": 0.2, "max_new_tokens": 200}
14
+ )
15
+
16
+ def search(self, query: str) -> str:
17
+ try:
18
+ with DDGS() as ddgs:
19
+ results = list(ddgs.text(query, max_results=1))
20
+ if results:
21
+ return results[0]["body"]
22
+ except Exception as e:
23
+ print(f"Search failed: {e}")
24
+ return ""
25
+
26
+ def __call__(self, question: str) -> str:
27
+ context = self.search(question)
28
+ system_prompt = (
29
+ "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: "
30
+ "FINAL ANSWER: [YOUR FINAL ANSWER]. "
31
+ "YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
32
+ "If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
33
+ "If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
34
+ "If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string."
35
+ )
36
+ prompt = f"{system_prompt}
37
+
38
+ Context: {context}
39
+
40
+ Question: {question}
41
+ Answer:"
42
+ try:
43
+ result = self.llm(prompt)[0]["generated_text"]
44
+ if "FINAL ANSWER:" in result:
45
+ answer = result.split("FINAL ANSWER:")[-1].strip().split("\n")[0]
46
+ return answer.lower().strip(" .")
47
+ else:
48
+ return "unknown"
49
+ except Exception as e:
50
+ print(f"LLM error: {e}")
51
+ return "unknown"
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import inspect
5
+ import pandas as pd
6
+ from agent import BasicAgent
7
+
8
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
+
10
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
11
+ space_id = os.getenv("SPACE_ID")
12
+ if profile:
13
+ username= f"{profile.username}"
14
+ print(f"User logged in: {username}")
15
+ else:
16
+ print("User not logged in.")
17
+ return "Please Login to Hugging Face with the button.", None
18
+
19
+ api_url = DEFAULT_API_URL
20
+ questions_url = f"{api_url}/questions"
21
+ submit_url = f"{api_url}/submit"
22
+
23
+ try:
24
+ agent = BasicAgent()
25
+ except Exception as e:
26
+ print(f"Error instantiating agent: {e}")
27
+ return f"Error initializing agent: {e}", None
28
+
29
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
30
+ print(agent_code)
31
+
32
+ try:
33
+ response = requests.get(questions_url, timeout=15)
34
+ response.raise_for_status()
35
+ questions_data = response.json()
36
+ if not questions_data:
37
+ return "Fetched questions list is empty or invalid format.", None
38
+ print(f"Fetched {len(questions_data)} questions.")
39
+ except Exception as e:
40
+ return f"Error fetching questions: {e}", None
41
+
42
+ results_log = []
43
+ answers_payload = []
44
+ for item in questions_data:
45
+ task_id = item.get("task_id")
46
+ question_text = item.get("question")
47
+ if not task_id or question_text is None:
48
+ continue
49
+ try:
50
+ submitted_answer = agent(question_text)
51
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
52
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
53
+ except Exception as e:
54
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
55
+
56
+ if not answers_payload:
57
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
58
+
59
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
60
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
61
+ try:
62
+ response = requests.post(submit_url, json=submission_data, timeout=60)
63
+ response.raise_for_status()
64
+ result_data = response.json()
65
+ final_status = (
66
+ f"Submission Successful!
67
+ "
68
+ f"User: {result_data.get('username')}
69
+ "
70
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
71
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)
72
+ "
73
+ f"Message: {result_data.get('message', 'No message received.')}"
74
+ )
75
+ results_df = pd.DataFrame(results_log)
76
+ return final_status, results_df
77
+ except Exception as e:
78
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
79
+
80
+ with gr.Blocks() as demo:
81
+ gr.Markdown("# GAIA Agent (Transformers + Search)")
82
+ gr.LoginButton()
83
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
84
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
85
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
86
+
87
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
88
+
89
+ if __name__ == "__main__":
90
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ duckduckgo-search
3
+ pandas
4
+ gradio
5
+ torch
6
+ accelerate