Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,69 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
from
|
| 6 |
|
| 7 |
-
# Constants
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 9 |
|
| 10 |
-
# Basic Agent Definition
|
| 11 |
class BasicAgent:
|
| 12 |
-
def __init__(self,
|
| 13 |
-
print("BasicAgent
|
| 14 |
-
self.
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
response = self.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
print(f"Error
|
| 26 |
-
return f"Error: {e}"
|
| 27 |
|
| 28 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None,
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if profile:
|
| 32 |
-
username
|
| 33 |
print(f"User logged in: {username}")
|
| 34 |
else:
|
| 35 |
print("User not logged in.")
|
|
@@ -41,10 +75,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, token_input: str):
|
|
| 41 |
|
| 42 |
# 1. Instantiate Agent
|
| 43 |
try:
|
| 44 |
-
agent = BasicAgent(
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error instantiating agent: {e}")
|
| 47 |
return f"Error initializing agent: {e}", None
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# 2. Fetch Questions
|
| 50 |
print(f"Fetching questions from: {questions_url}")
|
|
@@ -53,15 +90,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, token_input: str):
|
|
| 53 |
response.raise_for_status()
|
| 54 |
questions_data = response.json()
|
| 55 |
if not questions_data:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
print(f"Fetched {len(questions_data)} questions.")
|
| 59 |
except requests.exceptions.RequestException as e:
|
| 60 |
print(f"Error fetching questions: {e}")
|
| 61 |
return f"Error fetching questions: {e}", None
|
| 62 |
except requests.exceptions.JSONDecodeError as e:
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 67 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -81,19 +119,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, token_input: str):
|
|
| 81 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 82 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
if not answers_payload:
|
| 88 |
print("Agent did not produce any answers to submit.")
|
| 89 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 90 |
|
| 91 |
# 4. Prepare Submission
|
| 92 |
-
submission_data = {
|
| 93 |
-
"username": username.strip(),
|
| 94 |
-
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 95 |
-
"answers": answers_payload
|
| 96 |
-
}
|
| 97 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 98 |
print(status_update)
|
| 99 |
|
|
@@ -113,6 +147,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, token_input: str):
|
|
| 113 |
print("Submission successful.")
|
| 114 |
results_df = pd.DataFrame(results_log)
|
| 115 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
except requests.exceptions.RequestException as e:
|
| 117 |
status_message = f"Submission Failed: Network error - {e}"
|
| 118 |
print(status_message)
|
|
@@ -124,30 +174,63 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, token_input: str):
|
|
| 124 |
results_df = pd.DataFrame(results_log)
|
| 125 |
return status_message, results_df
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("#
|
| 130 |
gr.Markdown(
|
| 131 |
"""
|
| 132 |
**Instructions:**
|
| 133 |
-
1.
|
| 134 |
-
2.
|
| 135 |
-
3.
|
| 136 |
4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
|
|
|
|
|
|
| 137 |
"""
|
| 138 |
)
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
gr.LoginButton()
|
| 141 |
-
|
| 142 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 143 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 144 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 145 |
|
| 146 |
run_button.click(
|
| 147 |
fn=run_and_submit_all,
|
| 148 |
-
inputs=[gr.OAuthProfile(),
|
| 149 |
outputs=[status_output, results_table]
|
| 150 |
)
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
+
DEFAULT_HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.1" # Free model on Hugging Face
|
| 11 |
|
| 12 |
+
# --- Basic Agent Definition ---
|
| 13 |
class BasicAgent:
|
| 14 |
+
def __init__(self, hf_token=None, model_name=DEFAULT_HF_MODEL):
|
| 15 |
+
print("Initializing BasicAgent with LLM...")
|
| 16 |
+
self.hf_token = hf_token
|
| 17 |
+
self.model_name = model_name
|
| 18 |
+
self.llm = None
|
| 19 |
+
self.tokenizer = None
|
| 20 |
+
|
| 21 |
+
if hf_token:
|
| 22 |
+
try:
|
| 23 |
+
print(f"Loading model: {model_name}")
|
| 24 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
|
| 25 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_token)
|
| 26 |
+
self.llm = pipeline(
|
| 27 |
+
"text-generation",
|
| 28 |
+
model=self.model,
|
| 29 |
+
tokenizer=self.tokenizer,
|
| 30 |
+
device_map="auto"
|
| 31 |
+
)
|
| 32 |
+
print("Model loaded successfully")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error loading model: {e}")
|
| 35 |
+
raise Exception(f"Could not load model: {e}")
|
| 36 |
+
else:
|
| 37 |
+
print("No HF token provided - agent will use default answers")
|
| 38 |
+
|
| 39 |
def __call__(self, question: str) -> str:
|
| 40 |
+
if not self.llm:
|
| 41 |
+
return "This is a default answer (no LLM initialized)"
|
| 42 |
+
|
| 43 |
try:
|
| 44 |
+
print(f"Generating answer for question: {question[:50]}...")
|
| 45 |
+
response = self.llm(
|
| 46 |
+
question,
|
| 47 |
+
max_new_tokens=150,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
top_p=0.9
|
| 51 |
+
)
|
| 52 |
+
return response[0]['generated_text']
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"Error generating answer: {e}")
|
| 55 |
+
return f"Error generating answer: {e}"
|
| 56 |
|
| 57 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None, hf_token: str):
|
| 58 |
+
"""
|
| 59 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 60 |
+
and displays the results.
|
| 61 |
+
"""
|
| 62 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 63 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 64 |
|
| 65 |
if profile:
|
| 66 |
+
username= f"{profile.username}"
|
| 67 |
print(f"User logged in: {username}")
|
| 68 |
else:
|
| 69 |
print("User not logged in.")
|
|
|
|
| 75 |
|
| 76 |
# 1. Instantiate Agent
|
| 77 |
try:
|
| 78 |
+
agent = BasicAgent(hf_token=hf_token)
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error instantiating agent: {e}")
|
| 81 |
return f"Error initializing agent: {e}", None
|
| 82 |
+
|
| 83 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 84 |
+
print(agent_code)
|
| 85 |
|
| 86 |
# 2. Fetch Questions
|
| 87 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 90 |
response.raise_for_status()
|
| 91 |
questions_data = response.json()
|
| 92 |
if not questions_data:
|
| 93 |
+
print("Fetched questions list is empty.")
|
| 94 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 95 |
print(f"Fetched {len(questions_data)} questions.")
|
| 96 |
except requests.exceptions.RequestException as e:
|
| 97 |
print(f"Error fetching questions: {e}")
|
| 98 |
return f"Error fetching questions: {e}", None
|
| 99 |
except requests.exceptions.JSONDecodeError as e:
|
| 100 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 101 |
+
print(f"Response text: {response.text[:500]}")
|
| 102 |
+
return f"Error decoding server response for questions: {e}", None
|
| 103 |
except Exception as e:
|
| 104 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 105 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 119 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 120 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 121 |
except Exception as e:
|
| 122 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 123 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 124 |
|
| 125 |
if not answers_payload:
|
| 126 |
print("Agent did not produce any answers to submit.")
|
| 127 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 128 |
|
| 129 |
# 4. Prepare Submission
|
| 130 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 132 |
print(status_update)
|
| 133 |
|
|
|
|
| 147 |
print("Submission successful.")
|
| 148 |
results_df = pd.DataFrame(results_log)
|
| 149 |
return final_status, results_df
|
| 150 |
+
except requests.exceptions.HTTPError as e:
|
| 151 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 152 |
+
try:
|
| 153 |
+
error_json = e.response.json()
|
| 154 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 155 |
+
except requests.exceptions.JSONDecodeError:
|
| 156 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 157 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 158 |
+
print(status_message)
|
| 159 |
+
results_df = pd.DataFrame(results_log)
|
| 160 |
+
return status_message, results_df
|
| 161 |
+
except requests.exceptions.Timeout:
|
| 162 |
+
status_message = "Submission Failed: The request timed out."
|
| 163 |
+
print(status_message)
|
| 164 |
+
results_df = pd.DataFrame(results_log)
|
| 165 |
+
return status_message, results_df
|
| 166 |
except requests.exceptions.RequestException as e:
|
| 167 |
status_message = f"Submission Failed: Network error - {e}"
|
| 168 |
print(status_message)
|
|
|
|
| 174 |
results_df = pd.DataFrame(results_log)
|
| 175 |
return status_message, results_df
|
| 176 |
|
| 177 |
+
|
| 178 |
+
# --- Build Gradio Interface using Blocks ---
|
| 179 |
with gr.Blocks() as demo:
|
| 180 |
+
gr.Markdown("# LLM Agent Evaluation Runner")
|
| 181 |
gr.Markdown(
|
| 182 |
"""
|
| 183 |
**Instructions:**
|
| 184 |
+
1. Get your Hugging Face API token from [your settings](https://huggingface.co/settings/tokens)
|
| 185 |
+
2. Enter your token below (it will be used only during this session)
|
| 186 |
+
3. Log in to your Hugging Face account
|
| 187 |
4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
**Note:** The first run will take longer as it downloads the model.
|
| 191 |
"""
|
| 192 |
)
|
| 193 |
|
| 194 |
+
with gr.Row():
|
| 195 |
+
hf_token_input = gr.Textbox(
|
| 196 |
+
label="Hugging Face API Token",
|
| 197 |
+
type="password",
|
| 198 |
+
placeholder="Enter your HF API token here (required for LLM)",
|
| 199 |
+
info="Get your token from https://huggingface.co/settings/tokens"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
gr.LoginButton()
|
| 203 |
+
|
| 204 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 205 |
+
|
| 206 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 207 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 208 |
|
| 209 |
run_button.click(
|
| 210 |
fn=run_and_submit_all,
|
| 211 |
+
inputs=[gr.OAuthProfile(), hf_token_input],
|
| 212 |
outputs=[status_output, results_table]
|
| 213 |
)
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 217 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 218 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 219 |
+
|
| 220 |
+
if space_host_startup:
|
| 221 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 222 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 223 |
+
else:
|
| 224 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 225 |
+
|
| 226 |
+
if space_id_startup:
|
| 227 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 228 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 229 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 230 |
+
else:
|
| 231 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 232 |
+
|
| 233 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 234 |
+
|
| 235 |
+
print("Launching Gradio Interface for LLM Agent Evaluation...")
|
| 236 |
+
demo.launch(debug=True, share=False)
|