XeroDN commited on
Commit
97a3559
·
verified ·
1 Parent(s): 7556de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -39
app.py CHANGED
@@ -1,45 +1,108 @@
1
- from langchain_openai import ChatOpenAI
2
- from langchain_community.tools import DuckDuckGoSearchRun
 
 
 
3
 
4
- class BasicAgent:
 
 
 
 
 
 
5
  def __init__(self):
6
  print("SmartAgent initialized.")
7
- self.llm = ChatOpenAI(model="gpt-4", temperature=0)
8
- self.search_tool = DuckDuckGoSearchRun()
9
-
10
- def __call__(self, question: str) -> str:
11
- print(f"Agent received question: {question[:50]}...")
12
 
13
- # Step 1: Ask LLM if it can confidently answer or needs to search
 
14
  prompt = f"""
15
- You are a helpful assistant. Given the question below, decide if you know the answer with high confidence.
16
- If yes, answer concisely.
17
- If you need more information, respond with SEARCH: followed by the search query.
18
- Question: {question}
19
- Answer or SEARCH:
20
- """
21
- response = self.llm.invoke(prompt).content.strip()
22
-
23
- if response.upper().startswith("SEARCH:"):
24
- # Extract search query
25
- query = response[len("SEARCH:"):].strip()
26
- print(f"Performing search with query: {query}")
27
-
28
- # Use DuckDuckGo search to get info
29
- search_result = self.search_tool.run(query)
30
-
31
- # Step 2: Now use LLM again with search results to answer
32
- prompt2 = f"""
33
- You are a helpful assistant. Use the following search results to answer the question concisely.
34
- Question: {question}
35
- Search Results: {search_result}
36
- Answer:
37
  """
38
- final_answer = self.llm.invoke(prompt2).content.strip()
39
- print(f"Agent final answer: {final_answer}")
40
- return final_answer
41
-
42
- else:
43
- # LLM confident answer directly
44
- print(f"Agent direct answer: {response}")
45
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+ import openai
6
 
7
+ # Load OpenAI API Key from environment
8
+ openai.api_key = os.getenv("OPENAI_API_KEY")
9
+
10
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
+
12
+ # --- Smart Agent Definition ---
13
+ class SmartAgent:
14
  def __init__(self):
15
  print("SmartAgent initialized.")
 
 
 
 
 
16
 
17
+ def __call__(self, question: str) -> str:
18
+ print(f"[Agent] Processing question: {question[:50]}...")
19
  prompt = f"""
20
+ You are an expert AI agent. Answer the following question clearly and precisely.
21
+ If external data is required (e.g. Wikipedia), use your knowledge base.
22
+ Only return the final answer. Do NOT include 'Answer:' or any extra words.
23
+
24
+ Question:
25
+ {question}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  """
27
+ try:
28
+ response = openai.ChatCompletion.create(
29
+ model="gpt-4",
30
+ messages=[{"role": "user", "content": prompt}],
31
+ temperature=0.4,
32
+ max_tokens=300
33
+ )
34
+ answer = response.choices[0].message["content"].strip()
35
+ print(f"[Agent] Answer: {answer}")
36
+ return answer
37
+ except Exception as e:
38
+ print(f"[Agent Error]: {e}")
39
+ return "Error: Could not generate answer."
40
+
41
+ # --- Submission Logic ---
42
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
43
+ space_id = os.getenv("SPACE_ID", "your_username/your_space_name")
44
+
45
+ if profile:
46
+ username = profile.username
47
+ print(f"User logged in: {username}")
48
+ else:
49
+ return "❌ Please log in to Hugging Face first.", None
50
+
51
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
52
+ agent = SmartAgent()
53
+
54
+ try:
55
+ response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
56
+ response.raise_for_status()
57
+ questions = response.json()
58
+ except Exception as e:
59
+ return f"❌ Failed to fetch questions: {e}", None
60
+
61
+ results_log = []
62
+ answers_payload = []
63
+
64
+ for item in questions:
65
+ task_id = item.get("task_id")
66
+ question_text = item.get("question")
67
+ if not task_id or not question_text:
68
+ continue
69
+ try:
70
+ answer = agent(question_text)
71
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
72
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
73
+ except Exception as e:
74
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
75
+
76
+ submission = {
77
+ "username": username,
78
+ "agent_code": agent_code,
79
+ "answers": answers_payload
80
+ }
81
+
82
+ try:
83
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
84
+ resp.raise_for_status()
85
+ result = resp.json()
86
+ score = result.get("score", "N/A")
87
+ correct = result.get("correct_count", "?")
88
+ total = result.get("total_attempted", "?")
89
+
90
+ msg = f"✅ Submission Successful!\nUser: {username}\nScore: {score}% ({correct}/{total} correct)"
91
+ return msg, pd.DataFrame(results_log)
92
+ except Exception as e:
93
+ return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
94
+
95
+ # --- Gradio UI ---
96
+ with gr.Blocks() as demo:
97
+ gr.Markdown("## 🤖 GAIA Agent Submission Tool")
98
+ gr.Markdown("Click below to log in and run your agent on the GAIA benchmark.")
99
+ gr.LoginButton()
100
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
101
+ status = gr.Textbox(label="Status", lines=5)
102
+ table = gr.DataFrame(label="Results")
103
+
104
+ run_btn.click(fn=run_and_submit_all, outputs=[status, table])
105
+
106
+ if __name__ == "__main__":
107
+ print("Launching GAIA Agent App")
108
+ demo.launch()