Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ load_dotenv()
|
|
| 13 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
-
|
| 17 |
# ββββββββββββββββ Utility: Python Executor ββββββββββββββββ
|
| 18 |
def python_exec(code: str) -> str:
|
| 19 |
try:
|
|
@@ -27,7 +26,6 @@ def python_exec(code: str) -> str:
|
|
| 27 |
except Exception:
|
| 28 |
return "Error during Python execution:\n" + traceback.format_exc()
|
| 29 |
|
| 30 |
-
|
| 31 |
# ββββββββββββββββ Function Schema ββββββββββββββββ
|
| 32 |
function_definitions = [
|
| 33 |
{
|
|
@@ -46,7 +44,6 @@ function_definitions = [
|
|
| 46 |
}
|
| 47 |
]
|
| 48 |
|
| 49 |
-
|
| 50 |
# ββββββββββββββββ Agent Class ββββββββββββββββ
|
| 51 |
class BasicAgent:
|
| 52 |
def __init__(self):
|
|
@@ -114,7 +111,6 @@ class BasicAgent:
|
|
| 114 |
else:
|
| 115 |
return first_msg.content.strip()
|
| 116 |
|
| 117 |
-
|
| 118 |
# ββββββββββββββββ API Helpers ββββββββββββββββ
|
| 119 |
def get_all_questions(api_url: str) -> list[dict]:
|
| 120 |
resp = requests.get(f"{api_url}/questions", timeout=15)
|
|
@@ -131,11 +127,13 @@ def submit_answers(api_url: str, username: str, code_link: str, answers: list[di
|
|
| 131 |
resp.raise_for_status()
|
| 132 |
return resp.json()
|
| 133 |
|
| 134 |
-
|
| 135 |
# ββββββββββββββββ Gradio Evaluation Logic ββββββββββββββββ
|
| 136 |
def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
| 137 |
if profile:
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
else:
|
| 140 |
return "β Please log in to Hugging Face using the button above.", None
|
| 141 |
|
|
@@ -193,7 +191,6 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
|
| 193 |
)
|
| 194 |
return final_status, pd.DataFrame(results_log)
|
| 195 |
|
| 196 |
-
|
| 197 |
# ββββββββββββββββ Gradio UI ββββββββββββββββ
|
| 198 |
with gr.Blocks() as demo:
|
| 199 |
gr.Markdown("# π§ Basic Agent Evaluation")
|
|
@@ -219,7 +216,6 @@ with gr.Blocks() as demo:
|
|
| 219 |
outputs=[status_output, results_table]
|
| 220 |
)
|
| 221 |
|
| 222 |
-
|
| 223 |
# ββββββββββββββββ Launch App ββββββββββββββββ
|
| 224 |
if __name__ == "__main__":
|
| 225 |
space_host = os.getenv("SPACE_HOST")
|
|
@@ -238,4 +234,4 @@ if __name__ == "__main__":
|
|
| 238 |
print("βΉοΈ SPACE_ID not found")
|
| 239 |
|
| 240 |
print("Launching Gradio Interface...")
|
| 241 |
-
demo.launch(debug=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 13 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
|
|
|
| 16 |
# ββββββββββββββββ Utility: Python Executor ββββββββββββββββ
|
| 17 |
def python_exec(code: str) -> str:
|
| 18 |
try:
|
|
|
|
| 26 |
except Exception:
|
| 27 |
return "Error during Python execution:\n" + traceback.format_exc()
|
| 28 |
|
|
|
|
| 29 |
# ββββββββββββββββ Function Schema ββββββββββββββββ
|
| 30 |
function_definitions = [
|
| 31 |
{
|
|
|
|
| 44 |
}
|
| 45 |
]
|
| 46 |
|
|
|
|
| 47 |
# ββββββββββββββββ Agent Class ββββββββββββββββ
|
| 48 |
class BasicAgent:
|
| 49 |
def __init__(self):
|
|
|
|
| 111 |
else:
|
| 112 |
return first_msg.content.strip()
|
| 113 |
|
|
|
|
| 114 |
# ββββββββββββββββ API Helpers ββββββββββββββββ
|
| 115 |
def get_all_questions(api_url: str) -> list[dict]:
|
| 116 |
resp = requests.get(f"{api_url}/questions", timeout=15)
|
|
|
|
| 127 |
resp.raise_for_status()
|
| 128 |
return resp.json()
|
| 129 |
|
|
|
|
| 130 |
# ββββββββββββββββ Gradio Evaluation Logic ββββββββββββββββ
|
| 131 |
def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
| 132 |
if profile:
|
| 133 |
+
if isinstance(profile, dict):
|
| 134 |
+
username = profile.get("preferred_username", "").strip()
|
| 135 |
+
else:
|
| 136 |
+
username = str(profile).strip()
|
| 137 |
else:
|
| 138 |
return "β Please log in to Hugging Face using the button above.", None
|
| 139 |
|
|
|
|
| 191 |
)
|
| 192 |
return final_status, pd.DataFrame(results_log)
|
| 193 |
|
|
|
|
| 194 |
# ββββββββββββββββ Gradio UI ββββββββββββββββ
|
| 195 |
with gr.Blocks() as demo:
|
| 196 |
gr.Markdown("# π§ Basic Agent Evaluation")
|
|
|
|
| 216 |
outputs=[status_output, results_table]
|
| 217 |
)
|
| 218 |
|
|
|
|
| 219 |
# ββββββββββββββββ Launch App ββββββββββββββββ
|
| 220 |
if __name__ == "__main__":
|
| 221 |
space_host = os.getenv("SPACE_HOST")
|
|
|
|
| 234 |
print("βΉοΈ SPACE_ID not found")
|
| 235 |
|
| 236 |
print("Launching Gradio Interface...")
|
| 237 |
+
demo.launch(debug=True, server_name="0.0.0.0", server_port=7860)
|