Dhanushkumar commited on
Commit
60ef870
·
verified ·
1 Parent(s): 870abdc

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -210
app.py DELETED
@@ -1,210 +0,0 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import pandas as pd
5
- from agent import build_graph
6
- from langchain_core.messages import HumanMessage
7
-
8
- # --- Constants ---
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
- os.environ["GROQ_API_KEY"] = "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"
11
- os.environ["TAVILY_API_KEY"] = "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"
12
- os.environ["GOOGLE_API_KEY"] = "AIzaSyCA47F1nv1sF9_y5IYzu_nZM325ZnIXfKU"
13
- # --- Advanced Agent Definition ---
14
- class AdvancedAgent:
15
- def __init__(self, provider="groq"):
16
- print("AdvancedAgent initialized.")
17
- try:
18
- # Initialize the graph-based agent from agent.py
19
- self.graph = build_graph(provider=provider)
20
- except Exception as e:
21
- print(f"Error initializing graph: {e}")
22
- raise e
23
-
24
- def __call__(self, question: str) -> str:
25
- print(f"Agent received question (first 50 chars): {question[:50]}...")
26
- try:
27
- # Run the graph with the question
28
- messages = [HumanMessage(content=question)]
29
- result = self.graph.invoke({"messages": messages})
30
- # Extract the final answer from the last message
31
- final_answer = result["messages"][-1].content
32
- print(f"Agent returning answer: {final_answer}")
33
- return final_answer
34
- except Exception as e:
35
- print(f"Error processing question: {e}")
36
- return f"AGENT ERROR: {e}"
37
-
38
- def run_and_submit_all(profile: gr.OAuthProfile | None):
39
- """
40
- Fetches all questions, runs the AdvancedAgent on them, submits all answers,
41
- and displays the results.
42
- """
43
- # --- Determine HF Space Runtime URL and Repo URL ---
44
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
45
-
46
- if profile:
47
- username = f"{profile.username}"
48
- print(f"User logged in: {username}")
49
- else:
50
- print("User not logged in.")
51
- return "Please Login to Hugging Face with the button.", None
52
-
53
- api_url = DEFAULT_API_URL
54
- questions_url = f"{api_url}/questions"
55
- submit_url = f"{api_url}/submit"
56
-
57
- # 1. Instantiate Agent
58
- try:
59
- agent = AdvancedAgent(provider="groq") # Use Groq as the LLM provider
60
- except Exception as e:
61
- print(f"Error instantiating agent: {e}")
62
- return f"Error initializing agent: {e}", None
63
- # In the case of an app running as a Hugging Face Space, this link points toward your codebase
64
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
65
- print(agent_code)
66
-
67
- # 2. Fetch Questions
68
- print(f"Fetching questions from: {questions_url}")
69
- try:
70
- response = requests.get(questions_url, timeout=15)
71
- response.raise_for_status()
72
- questions_data = response.json()
73
- if not questions_data:
74
- print("Fetched questions list is empty.")
75
- return "Fetched questions list is empty or invalid format.", None
76
- print(f"Fetched {len(questions_data)} questions.")
77
- except requests.exceptions.RequestException as e:
78
- print(f"Error fetching questions: {e}")
79
- return f"Error fetching questions: {e}", None
80
- except requests.exceptions.JSONDecodeError as e:
81
- print(f"Error decoding JSON response from questions endpoint: {e}")
82
- print(f"Response text: {response.text[:500]}")
83
- return f"Error decoding server response for questions: {e}", None
84
- except Exception as e:
85
- print(f"An unexpected error occurred fetching questions: {e}")
86
- return f"An unexpected error occurred fetching questions: {e}", None
87
-
88
- # 3. Run your Agent
89
- results_log = []
90
- answers_payload = []
91
- print(f"Running agent on {len(questions_data)} questions...")
92
- for item in questions_data:
93
- task_id = item.get("task_id")
94
- question_text = item.get("question")
95
- if not task_id or question_text is None:
96
- print(f"Skipping item with missing task_id or question: {item}")
97
- continue
98
- try:
99
- submitted_answer = agent(question_text)
100
- # Ensure the answer is a string and remove any unwanted prefixes
101
- submitted_answer = str(submitted_answer).replace("FINAL ANSWER:", "").strip()
102
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
103
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
104
- except Exception as e:
105
- print(f"Error running agent on task {task_id}: {e}")
106
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
107
-
108
- if not answers_payload:
109
- print("Agent did not produce any answers to submit.")
110
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
-
112
- # 4. Prepare Submission
113
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
114
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
115
- print(status_update)
116
-
117
- # 5. Submit
118
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
119
- try:
120
- response = requests.post(submit_url, json=submission_data, timeout=60)
121
- response.raise_for_status()
122
- result_data = response.json()
123
- final_status = (
124
- f"Submission Successful!\n"
125
- f"User: {result_data.get('username')}\n"
126
- f"Overall Score: {result_data.get('score', 'N/A')}% "
127
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
128
- f"Message: {result_data.get('message', 'No message received.')}"
129
- )
130
- print("Submission successful.")
131
- results_df = pd.DataFrame(results_log)
132
- return final_status, results_df
133
- except requests.exceptions.HTTPError as e:
134
- error_detail = f"Server responded with status {e.response.status_code}."
135
- try:
136
- error_json = e.response.json()
137
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
138
- except requests.exceptions.JSONDecodeError:
139
- error_detail += f" Response: {e.response.text[:500]}"
140
- status_message = f"Submission Failed: {error_detail}"
141
- print(status_message)
142
- results_df = pd.DataFrame(results_log)
143
- return status_message, results_df
144
- except requests.exceptions.Timeout:
145
- status_message = "Submission Failed: The request timed out."
146
- print(status_message)
147
- results_df = pd.DataFrame(results_log)
148
- return status_message, results_df
149
- except requests.exceptions.RequestException as e:
150
- status_message = f"Submission Failed: Network error - {e}"
151
- print(status_message)
152
- results_df = pd.DataFrame(results_log)
153
- return status_message, results_df
154
- except Exception as e:
155
- status_message = f"An unexpected error occurred during submission: {e}"
156
- print(status_message)
157
- results_df = pd.DataFrame(results_log)
158
- return status_message, results_df
159
-
160
- # --- Build Gradio Interface using Blocks ---
161
- with gr.Blocks() as demo:
162
- gr.Markdown("# Advanced Agent Evaluation Runner")
163
- gr.Markdown(
164
- """
165
- **Instructions:**
166
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc.
167
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
168
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
169
- ---
170
- **Disclaimers:**
171
- Once clicking on the "submit" button, it can take some time for the agent to process all questions.
172
- This space provides a setup to encourage you to develop a robust solution. For instance, to reduce the delay in submission, consider caching answers or processing questions asynchronously.
173
- """
174
- )
175
-
176
- gr.LoginButton()
177
-
178
- run_button = gr.Button("Run Evaluation & Submit All Answers")
179
-
180
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
181
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
182
-
183
- run_button.click(
184
- fn=run_and_submit_all,
185
- outputs=[status_output, results_table]
186
- )
187
-
188
- if __name__ == "__main__":
189
- print("\n" + "-"*30 + " App Starting " + "-"*30)
190
- # Check for SPACE_HOST and SPACE_ID at startup for information
191
- space_host_startup = os.getenv("SPACE_HOST")
192
- space_id_startup = os.getenv("SPACE_ID")
193
-
194
- if space_host_startup:
195
- print(f"✅ SPACE_HOST found: {space_host_startup}")
196
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
197
- else:
198
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
199
-
200
- if space_id_startup:
201
- print(f"✅ SPACE_ID found: {space_id_startup}")
202
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
203
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
204
- else:
205
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
206
-
207
- print("-"*(60 + len(" App Starting ")) + "\n")
208
-
209
- print("Launching Gradio Interface for Advanced Agent Evaluation...")
210
- demo.launch(debug=True, share=False)