Spaces:
Sleeping
Sleeping
Commit
·
133c3b8
1
Parent(s):
81917a3
Questions preview
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
@@ -8,24 +9,30 @@ import pandas as pd
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
fixed_answer = "This is a default answer."
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 28 |
-
space_id = os.getenv("
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
username= f"{profile.username}"
|
|
@@ -40,7 +47,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -73,7 +80,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 76 |
for item in questions_data:
|
|
|
|
|
|
|
|
|
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
| 79 |
if not task_id or question_text is None:
|
|
@@ -86,11 +97,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error running agent on task {task_id}: {e}")
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 89 |
|
| 90 |
if not answers_payload:
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
-
|
| 94 |
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
|
@@ -138,7 +150,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 138 |
print(status_message)
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return status_message, results_df
|
| 141 |
-
|
| 142 |
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 4 |
import requests
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
|
|
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
#llm = HuggingFaceInferenceAPI(model_name="meta-llama/Llama-3.2-3B-Instruct")
|
| 13 |
+
|
| 14 |
+
# --- Final Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
+
class FinalAgent:
|
| 17 |
def __init__(self):
|
| 18 |
+
print("FinalAgent initialized.")
|
| 19 |
def __call__(self, question: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
fixed_answer = "This is a default answer."
|
| 26 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 27 |
return fixed_answer
|
| 28 |
|
| 29 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 30 |
"""
|
| 31 |
+
Fetches all questions, runs the FinalAgent on them, submits all answers,
|
| 32 |
and displays the results.
|
| 33 |
"""
|
| 34 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 35 |
+
space_id = os.getenv("TheZakynthian/Final_Assignment_Template") # Get the SPACE_ID for sending link to the code
|
| 36 |
|
| 37 |
if profile:
|
| 38 |
username= f"{profile.username}"
|
|
|
|
| 47 |
|
| 48 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 49 |
try:
|
| 50 |
+
agent = FinalAgent()
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Error instantiating agent: {e}")
|
| 53 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 80 |
results_log = []
|
| 81 |
answers_payload = []
|
| 82 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 83 |
+
|
| 84 |
for item in questions_data:
|
| 85 |
+
print(item.get("task_id"))
|
| 86 |
+
print(item.get("question"))
|
| 87 |
+
"""
|
| 88 |
task_id = item.get("task_id")
|
| 89 |
question_text = item.get("question")
|
| 90 |
if not task_id or question_text is None:
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
print(f"Error running agent on task {task_id}: {e}")
|
| 99 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 100 |
+
"""
|
| 101 |
|
| 102 |
if not answers_payload:
|
| 103 |
print("Agent did not produce any answers to submit.")
|
| 104 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 105 |
+
"""
|
| 106 |
# 4. Prepare Submission
|
| 107 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 108 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
|
|
|
| 150 |
print(status_message)
|
| 151 |
results_df = pd.DataFrame(results_log)
|
| 152 |
return status_message, results_df
|
| 153 |
+
"""
|
| 154 |
|
| 155 |
# --- Build Gradio Interface using Blocks ---
|
| 156 |
with gr.Blocks() as demo:
|