dlaima commited on
Commit
7cfb3a2
·
verified ·
1 Parent(s): 05b8101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -91
app.py CHANGED
@@ -4,132 +4,173 @@ import os
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
 
7
  from smolagents import CodeAgent, DuckDuckGoSearchTool
8
  from smolagents.models import OpenAIServerModel
9
- import openai
10
 
11
- # --- Setup ---
12
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
- if not OPENAI_API_KEY:
14
- raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets.")
15
- openai.api_key = OPENAI_API_KEY
 
16
 
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
- OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4o")
19
 
20
- model = OpenAIServerModel(model_id=OPENAI_MODEL_ID, api_key=OPENAI_API_KEY)
21
- search_tool = DuckDuckGoSearchTool()
22
- agent = CodeAgent(tools=[search_tool], model=model)
 
 
 
 
 
 
 
 
23
 
24
- answer_formatting_prompt = """
25
- You are a smart assistant with access to tools like DuckDuckGoSearchTool(query: str).
26
- Think step-by-step, then output your response.
27
 
28
- IMPORTANT:
29
- FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers/strings.
30
- Do NOT include commas, $ or % unless asked.
31
- Write digits plainly (e.g., '10', not 'ten').
 
32
 
33
- Use format:
34
- FINAL ANSWER: <your_answer>
35
- """
 
 
 
36
 
37
- def show_profile(profile):
38
- if not profile:
39
- return "⚠️ Not logged in."
40
- return f"✅ Logged in as: {profile['username']}"
41
 
42
- def run_and_submit_all(login_info):
43
- # login_info comes from LoginButton, it's None if not logged in
44
- if not login_info:
45
- return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
 
46
 
47
- username = login_info["username"]
48
- space_id = os.getenv("SPACE_ID", "")
49
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
50
 
 
51
  try:
52
- resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
53
- resp.raise_for_status()
54
- questions = resp.json()
 
 
 
 
55
  except Exception as e:
56
- return f"Error fetching questions: {e}", pd.DataFrame()
 
57
 
58
- results, payload = [], []
59
- for item in questions:
 
 
60
  task_id = item.get("task_id")
61
- question = item.get("question")
62
- if not task_id or not question:
 
63
  continue
64
- prompt = answer_formatting_prompt.strip() + f"\n\nQUESTION: {question.strip()}"
65
  try:
66
- answer = agent.run(prompt)
 
 
67
  except Exception as e:
68
- answer = f"AGENT ERROR: {e}"
69
- results.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
70
- payload.append({"task_id": task_id, "submitted_answer": answer})
71
 
72
- if not payload:
73
- return "⚠️ Agent returned no answers.", pd.DataFrame(results)
 
74
 
 
 
75
  try:
76
- post = requests.post(
77
- f"{DEFAULT_API_URL}/submit",
78
- json={"username": username, "agent_code": agent_code, "answers": payload},
79
- timeout=60
80
- )
81
- post.raise_for_status()
82
- result = post.json()
83
- score = result.get("score", "N/A")
84
- correct = result.get("correct_count", "?")
85
- attempted = result.get("total_attempted", "?")
86
- message = result.get("message", "")
87
- return (
88
- f"✅ Submission Successful!\nUser: {username}\nScore: {score}% "
89
- f"({correct}/{attempted})\nMessage: {message}",
90
- pd.DataFrame(results)
91
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  except Exception as e:
93
- return f" Submission failed: {e}", pd.DataFrame(results)
94
-
 
95
 
96
  with gr.Blocks() as demo:
97
  gr.Markdown("# Basic Agent Evaluation Runner")
98
-
99
- login_button = gr.LoginButton()
100
- login_status = gr.Textbox(label="Login Status")
101
-
 
 
 
 
 
 
 
 
102
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
103
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
104
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
105
 
106
- # Show login status when user logs in
107
- login_button.click(fn=show_profile, inputs=[login_button], outputs=[login_status])
108
-
109
- # Run evaluation on click, pass login_button's state as input
110
- run_button.click(fn=run_and_submit_all, inputs=[login_button], outputs=[status_output, results_table])
111
-
112
 
113
  if __name__ == "__main__":
114
- demo.launch()
115
-
116
-
117
- #import gradio as gr
118
-
119
- #def show_profile(profile):
120
- # if not profile:
121
- # return "⚠️ Not logged in."
122
- # return f"✅ Logged in as: {profile['username']}"
123
-
124
- # with gr.Blocks() as demo:
125
- # gr.Markdown("## 🔐 Hugging Face OAuth Login")
126
-
127
- # login_button = gr.LoginButton()
128
- # output = gr.Textbox(label="Login Status")
129
-
130
- # login_button.click(fn=show_profile, inputs=[login_button], outputs=[output])
131
-
132
- # demo.launch()
 
 
133
 
134
 
135
 
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
+
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool
9
  from smolagents.models import OpenAIServerModel
 
10
 
11
+ # System prompt as per your instructions
12
+ SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
13
+ Report your thoughts, and finish your answer with the following template:
14
+ FINAL ANSWER: [YOUR FINAL ANSWER].
15
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
16
+ of numbers and/or strings. 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. 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. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
17
 
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
19
 
20
+ class MyAgent:
21
+ def __init__(self):
22
+ # Initialize model with system prompt
23
+ self.model = OpenAIServerModel(
24
+ model_id="gpt-4",
25
+ system_message=SYSTEM_PROMPT
26
+ )
27
+ self.agent = CodeAgent(
28
+ tools=[DuckDuckGoSearchTool()],
29
+ model=self.model
30
+ )
31
 
32
+ def __call__(self, question: str) -> str:
33
+ # Run agent on the question
34
+ return self.agent.run(question)
35
 
36
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
37
+ """
38
+ Fetches questions, runs the agent, submits answers, returns status and results table.
39
+ """
40
+ space_id = os.getenv("SPACE_ID")
41
 
42
+ if profile:
43
+ username = profile.username
44
+ print(f"User logged in: {username}")
45
+ else:
46
+ print("User not logged in.")
47
+ return "Please Login to Hugging Face with the button.", None
48
 
49
+ api_url = DEFAULT_API_URL
50
+ questions_url = f"{api_url}/questions"
51
+ submit_url = f"{api_url}/submit"
 
52
 
53
+ try:
54
+ agent = MyAgent()
55
+ except Exception as e:
56
+ print(f"Error initializing agent: {e}")
57
+ return f"Error initializing agent: {e}", None
58
 
 
 
59
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
60
+ print(f"Agent code URL: {agent_code}")
61
 
62
+ print(f"Fetching questions from: {questions_url}")
63
  try:
64
+ response = requests.get(questions_url, timeout=15)
65
+ response.raise_for_status()
66
+ questions_data = response.json()
67
+ if not questions_data:
68
+ print("Fetched questions list is empty.")
69
+ return "Fetched questions list is empty or invalid format.", None
70
+ print(f"Fetched {len(questions_data)} questions.")
71
  except Exception as e:
72
+ print(f"Error fetching questions: {e}")
73
+ return f"Error fetching questions: {e}", None
74
 
75
+ results_log = []
76
+ answers_payload = []
77
+ print(f"Running agent on {len(questions_data)} questions...")
78
+ for item in questions_data:
79
  task_id = item.get("task_id")
80
+ question_text = item.get("question")
81
+ if not task_id or question_text is None:
82
+ print(f"Skipping invalid item: {item}")
83
  continue
 
84
  try:
85
+ submitted_answer = agent(question_text)
86
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
87
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
88
  except Exception as e:
89
+ print(f"Error running agent on task {task_id}: {e}")
90
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
91
 
92
+ if not answers_payload:
93
+ print("Agent did not produce any answers to submit.")
94
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
95
 
96
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
97
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
98
  try:
99
+ response = requests.post(submit_url, json=submission_data, timeout=60)
100
+ response.raise_for_status()
101
+ result_data = response.json()
102
+ final_status = (
103
+ f"Submission Successful!\n"
104
+ f"User: {result_data.get('username')}\n"
105
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
106
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
107
+ f"Message: {result_data.get('message', 'No message received.')}"
 
 
 
 
 
 
108
  )
109
+ print("Submission successful.")
110
+ results_df = pd.DataFrame(results_log)
111
+ return final_status, results_df
112
+ except requests.exceptions.HTTPError as e:
113
+ error_detail = f"Server responded with status {e.response.status_code}."
114
+ try:
115
+ error_json = e.response.json()
116
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
117
+ except Exception:
118
+ error_detail += f" Response: {e.response.text[:500]}"
119
+ status_message = f"Submission Failed: {error_detail}"
120
+ print(status_message)
121
+ return status_message, pd.DataFrame(results_log)
122
+ except requests.exceptions.Timeout:
123
+ status_message = "Submission Failed: The request timed out."
124
+ print(status_message)
125
+ return status_message, pd.DataFrame(results_log)
126
  except Exception as e:
127
+ status_message = f"An unexpected error occurred during submission: {e}"
128
+ print(status_message)
129
+ return status_message, pd.DataFrame(results_log)
130
 
131
  with gr.Blocks() as demo:
132
  gr.Markdown("# Basic Agent Evaluation Runner")
133
+ gr.Markdown(
134
+ """
135
+ **Instructions:**
136
+ 1. Clone this space, modify code to define your agent's logic, tools, and packages.
137
+ 2. Log in to your Hugging Face account using the button below.
138
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
139
+
140
+ **Note:** Submitting can take some time.
141
+ """
142
+ )
143
+
144
+ gr.LoginButton()
145
  run_button = gr.Button("Run Evaluation & Submit All Answers")
146
+
147
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
148
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
149
 
150
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
 
151
 
152
  if __name__ == "__main__":
153
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
154
+ space_host = os.getenv("SPACE_HOST")
155
+ space_id = os.getenv("SPACE_ID")
156
+
157
+ if space_host:
158
+ print(f"✅ SPACE_HOST found: {space_host}")
159
+ print(f" Runtime URL should be: https://{space_host}.hf.space")
160
+ else:
161
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
162
+
163
+ if space_id:
164
+ print(f" SPACE_ID found: {space_id}")
165
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
166
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
167
+ else:
168
+ print("ℹ️ SPACE_ID environment variable not found (running locally?).")
169
+
170
+ print("-"*(60 + len(" App Starting ")) + "\n")
171
+
172
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
173
+ demo.launch(debug=True, share=False)
174
 
175
 
176