Spaces:
Runtime error
Runtime error
Commit
·
cd6d75f
1
Parent(s):
3f2b048
Final run with gemini model
Browse files
agent.py
CHANGED
|
@@ -46,7 +46,7 @@ llm_gemini = ChatGoogleGenerativeAI(
|
|
| 46 |
llm_openai = ChatOpenAI(
|
| 47 |
model="openai/gpt-oss-120b:together",
|
| 48 |
temperature=0,
|
| 49 |
-
max_tokens=
|
| 50 |
timeout=60,
|
| 51 |
max_retries=2,
|
| 52 |
api_key=os.getenv("HF_TOKEN"),
|
|
@@ -54,7 +54,7 @@ llm_openai = ChatOpenAI(
|
|
| 54 |
)
|
| 55 |
|
| 56 |
if MODEL_PROVIDER == "gemini":
|
| 57 |
-
llm =
|
| 58 |
elif MODEL_PROVIDER == "openai":
|
| 59 |
llm = llm_openai
|
| 60 |
else:
|
|
|
|
| 46 |
llm_openai = ChatOpenAI(
|
| 47 |
model="openai/gpt-oss-120b:together",
|
| 48 |
temperature=0,
|
| 49 |
+
max_tokens=None, # type: ignore
|
| 50 |
timeout=60,
|
| 51 |
max_retries=2,
|
| 52 |
api_key=os.getenv("HF_TOKEN"),
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
if MODEL_PROVIDER == "gemini":
|
| 57 |
+
llm = llm_gemini
|
| 58 |
elif MODEL_PROVIDER == "openai":
|
| 59 |
llm = llm_openai
|
| 60 |
else:
|
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import inspect
|
|
@@ -6,7 +7,7 @@ import pandas as pd
|
|
| 6 |
import json
|
| 7 |
from logging_config import logger # Import the shared logger
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
-
from agent import MODEL_PROVIDER
|
| 10 |
|
| 11 |
load_dotenv(".env")
|
| 12 |
# (Keep Constants as is)
|
|
@@ -20,6 +21,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 20 |
class BasicAgent:
|
| 21 |
def __init__(self):
|
| 22 |
print("BasicAgent initialized.")
|
|
|
|
| 23 |
self.graph = graph
|
| 24 |
def __call__(self, item: dict) -> str:
|
| 25 |
"""Process the input item and return a response.
|
|
@@ -98,27 +100,60 @@ def run_all(profile: gr.OAuthProfile | None):
|
|
| 98 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 99 |
continue
|
| 100 |
try:
|
| 101 |
-
submitted_answer = agent(
|
| 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 |
-
|
| 113 |
-
|
| 114 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 115 |
print(status_update)
|
| 116 |
-
|
| 117 |
-
# 5. Submit
|
| 118 |
-
def submit_all(profile: gr.OAuthProfile | None):
|
| 119 |
api_url = DEFAULT_API_URL
|
| 120 |
submit_url = f"{api_url}/submit"
|
| 121 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
| 122 |
try:
|
| 123 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 124 |
response.raise_for_status()
|
|
@@ -131,7 +166,7 @@ def submit_all(profile: gr.OAuthProfile | None):
|
|
| 131 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 132 |
)
|
| 133 |
print("Submission successful.")
|
| 134 |
-
results_df = pd.DataFrame(results_log)
|
| 135 |
return final_status, results_df
|
| 136 |
except requests.exceptions.HTTPError as e:
|
| 137 |
error_detail = f"Server responded with status {e.response.status_code}."
|
|
@@ -142,22 +177,22 @@ def submit_all(profile: gr.OAuthProfile | None):
|
|
| 142 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 143 |
status_message = f"Submission Failed: {error_detail}"
|
| 144 |
print(status_message)
|
| 145 |
-
results_df = pd.DataFrame(results_log)
|
| 146 |
return status_message, results_df
|
| 147 |
except requests.exceptions.Timeout:
|
| 148 |
status_message = "Submission Failed: The request timed out."
|
| 149 |
print(status_message)
|
| 150 |
-
results_df = pd.DataFrame(results_log)
|
| 151 |
return status_message, results_df
|
| 152 |
except requests.exceptions.RequestException as e:
|
| 153 |
status_message = f"Submission Failed: Network error - {e}"
|
| 154 |
print(status_message)
|
| 155 |
-
results_df = pd.DataFrame(results_log)
|
| 156 |
return status_message, results_df
|
| 157 |
except Exception as e:
|
| 158 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 159 |
print(status_message)
|
| 160 |
-
results_df = pd.DataFrame(results_log)
|
| 161 |
return status_message, results_df
|
| 162 |
|
| 163 |
|
|
@@ -183,13 +218,18 @@ with gr.Blocks() as demo:
|
|
| 183 |
|
| 184 |
gr.LoginButton()
|
| 185 |
|
| 186 |
-
run_button = gr.Button("Run Evaluation &
|
| 187 |
-
|
| 188 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 189 |
# Removed max_rows=10 from DataFrame constructor
|
| 190 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 191 |
|
| 192 |
run_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
fn=submit_all,
|
| 194 |
outputs=[status_output, results_table]
|
| 195 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import inspect
|
|
|
|
| 7 |
import json
|
| 8 |
from logging_config import logger # Import the shared logger
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from agent import MODEL_PROVIDER
|
| 11 |
|
| 12 |
load_dotenv(".env")
|
| 13 |
# (Keep Constants as is)
|
|
|
|
| 21 |
class BasicAgent:
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized.")
|
| 24 |
+
from agent import graph
|
| 25 |
self.graph = graph
|
| 26 |
def __call__(self, item: dict) -> str:
|
| 27 |
"""Process the input item and return a response.
|
|
|
|
| 100 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 101 |
continue
|
| 102 |
try:
|
| 103 |
+
submitted_answer = agent(item)
|
| 104 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 105 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Error running agent on task {task_id}: {e}")
|
| 108 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 109 |
+
time.sleep(15) # To avoid hitting rate limits or overwhelming the system
|
| 110 |
|
| 111 |
if not answers_payload:
|
| 112 |
print("Agent did not produce any answers to submit.")
|
| 113 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 114 |
+
|
| 115 |
+
# save results log to a json file for debugging
|
| 116 |
+
with open(f"results_log_{MODEL_PROVIDER}.json", "w") as fh:
|
| 117 |
+
json.dump(results_log, fh, indent=2)
|
| 118 |
+
logger.info(f"Results log saved to results_log_{MODEL_PROVIDER}.json")
|
| 119 |
+
# save answers payload to a json file. Caching the answers to be able to submit them later
|
| 120 |
+
with open(f"answers_payload_{MODEL_PROVIDER}.json", "w") as fh:
|
| 121 |
+
json.dump(answers_payload, fh, indent=2)
|
| 122 |
+
logger.info(f"Answers payload saved to answers_payload_{MODEL_PROVIDER}.json")
|
| 123 |
+
|
| 124 |
+
message = f"Agent run completed. {len(answers_payload)} answers ready for submission."
|
| 125 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 126 |
+
return message, results_df
|
| 127 |
+
|
| 128 |
+
def submit_all(profile: gr.OAuthProfile | None):
|
| 129 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 130 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 131 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 132 |
+
if profile:
|
| 133 |
+
username= f"{profile.username}"
|
| 134 |
+
print(f"User logged in: {username}")
|
| 135 |
+
else:
|
| 136 |
+
print("User not logged in.")
|
| 137 |
+
return "Please Login to Hugging Face with the button.", None
|
| 138 |
+
# 4. Prepare Submission
|
| 139 |
+
try:
|
| 140 |
+
with open(f"answers_payload.json", "r") as fh:
|
| 141 |
+
answers_payload = json.load(fh)
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f"Error loading answers payload: {e}")
|
| 144 |
+
try:
|
| 145 |
+
with open(f"results_log.json", "r") as fh:
|
| 146 |
+
results_log = json.load(fh)
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Error loading results log: {e}")
|
| 149 |
|
| 150 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} # type: ignore
|
| 151 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..." # type: ignore
|
|
|
|
| 152 |
print(status_update)
|
|
|
|
|
|
|
|
|
|
| 153 |
api_url = DEFAULT_API_URL
|
| 154 |
submit_url = f"{api_url}/submit"
|
| 155 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}") # type: ignore
|
| 156 |
+
# 5. Submit
|
| 157 |
try:
|
| 158 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 159 |
response.raise_for_status()
|
|
|
|
| 166 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 167 |
)
|
| 168 |
print("Submission successful.")
|
| 169 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 170 |
return final_status, results_df
|
| 171 |
except requests.exceptions.HTTPError as e:
|
| 172 |
error_detail = f"Server responded with status {e.response.status_code}."
|
|
|
|
| 177 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 178 |
status_message = f"Submission Failed: {error_detail}"
|
| 179 |
print(status_message)
|
| 180 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 181 |
return status_message, results_df
|
| 182 |
except requests.exceptions.Timeout:
|
| 183 |
status_message = "Submission Failed: The request timed out."
|
| 184 |
print(status_message)
|
| 185 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 186 |
return status_message, results_df
|
| 187 |
except requests.exceptions.RequestException as e:
|
| 188 |
status_message = f"Submission Failed: Network error - {e}"
|
| 189 |
print(status_message)
|
| 190 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 191 |
return status_message, results_df
|
| 192 |
except Exception as e:
|
| 193 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 194 |
print(status_message)
|
| 195 |
+
results_df = pd.DataFrame(results_log) # type: ignore
|
| 196 |
return status_message, results_df
|
| 197 |
|
| 198 |
|
|
|
|
| 218 |
|
| 219 |
gr.LoginButton()
|
| 220 |
|
| 221 |
+
run_button = gr.Button("Run Evaluation & Cache Answers") # Renamed for clarity
|
| 222 |
+
submit_button = gr.Button("Submit Cached Answers") # New button for submission
|
| 223 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 224 |
# Removed max_rows=10 from DataFrame constructor
|
| 225 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 226 |
|
| 227 |
run_button.click(
|
| 228 |
+
fn=run_all,
|
| 229 |
+
outputs=[status_output, results_table]
|
| 230 |
+
)
|
| 231 |
+
# Add the click event for the new submit_button
|
| 232 |
+
submit_button.click(
|
| 233 |
fn=submit_all,
|
| 234 |
outputs=[status_output, results_table]
|
| 235 |
)
|