Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,99 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
-
class BasicAgent:
|
| 19 |
-
def __init__(self):
|
| 20 |
|
| 21 |
-
webTool = DuckDuckGoSearchTool()
|
| 22 |
-
token = os.getenv("HF_TOKEN")
|
| 23 |
-
model = InferenceClientModel("Qwen/Qwen2.5-72B-Instruct", token=token)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
print("BasicAgent initialized.")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
answer = self.agent.run(
|
| 37 |
-
"You must answer the question exactly, with a single word. For example: '4' or 'Paris'. The question is: "
|
| 38 |
-
+ question
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
print(f"Agent returning fixed answer: {answer}")
|
| 42 |
-
return answer
|
| 43 |
|
| 44 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 47 |
and displays the results.
|
| 48 |
"""
|
| 49 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 50 |
-
space_id = os.getenv("SPACE_ID")
|
| 51 |
|
| 52 |
if profile:
|
| 53 |
-
username= f"{profile.username}"
|
| 54 |
print(f"User logged in: {username}")
|
| 55 |
else:
|
| 56 |
print("User not logged in.")
|
|
@@ -62,7 +105,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 62 |
|
| 63 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 64 |
try:
|
| 65 |
-
agent =
|
| 66 |
except Exception as e:
|
| 67 |
print(f"Error instantiating agent: {e}")
|
| 68 |
return f"Error initializing agent: {e}", None
|
|
@@ -77,16 +120,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 77 |
response.raise_for_status()
|
| 78 |
questions_data = response.json()
|
| 79 |
if not questions_data:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
print(f"Fetched {len(questions_data)} questions.")
|
| 83 |
except requests.exceptions.RequestException as e:
|
| 84 |
print(f"Error fetching questions: {e}")
|
| 85 |
return f"Error fetching questions: {e}", None
|
| 86 |
except requests.exceptions.JSONDecodeError as e:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
except Exception as e:
|
| 91 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 92 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -99,26 +142,68 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 99 |
task_id = item.get("task_id")
|
| 100 |
question_text = item.get("question")
|
| 101 |
question_file = item.get("file_name")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
if not task_id or question_text is None:
|
| 106 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 107 |
continue
|
| 108 |
try:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if not answers_payload:
|
| 117 |
print("Agent did not produce any answers to submit.")
|
| 118 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 119 |
|
| 120 |
-
# 4. Prepare Submission
|
| 121 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 123 |
print(status_update)
|
| 124 |
|
|
@@ -136,6 +221,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 136 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 137 |
)
|
| 138 |
print("Submission successful.")
|
|
|
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return final_status, results_df
|
| 141 |
except requests.exceptions.HTTPError as e:
|
|
@@ -188,20 +274,19 @@ with gr.Blocks() as demo:
|
|
| 188 |
|
| 189 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 190 |
|
| 191 |
-
status_output = gr.Textbox(
|
|
|
|
|
|
|
| 192 |
# Removed max_rows=10 from DataFrame constructor
|
| 193 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 194 |
|
| 195 |
-
run_button.click(
|
| 196 |
-
fn=run_and_submit_all,
|
| 197 |
-
outputs=[status_output, results_table]
|
| 198 |
-
)
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 202 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 203 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 204 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 205 |
|
| 206 |
if space_host_startup:
|
| 207 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -209,14 +294,18 @@ if __name__ == "__main__":
|
|
| 209 |
else:
|
| 210 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 211 |
|
| 212 |
-
if space_id_startup:
|
| 213 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 214 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 215 |
-
print(
|
|
|
|
|
|
|
| 216 |
else:
|
| 217 |
-
print(
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 220 |
|
| 221 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 222 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from agent import Agent
|
| 7 |
+
import speech_recognition as sr
|
| 8 |
+
from dotenv import load_dotenv
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
+
api_url = DEFAULT_API_URL
|
| 14 |
|
| 15 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def get_questions():
|
| 19 |
+
questions_url = f"{api_url}/questions"
|
| 20 |
+
response = requests.get(questions_url, timeout=15)
|
| 21 |
+
response.raise_for_status()
|
| 22 |
|
| 23 |
+
return response.json()
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
+
def file_exists_check(file_path):
|
| 27 |
+
"""Check if a file exists at the given path."""
|
| 28 |
+
return os.path.exists(file_path)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def download_file(task_id, target_filename):
|
| 32 |
+
file_exists = file_exists_check(target_filename)
|
| 33 |
+
|
| 34 |
+
if file_exists:
|
| 35 |
+
print(f"Skipping download {target_filename}")
|
| 36 |
+
return target_filename
|
| 37 |
+
|
| 38 |
+
questions_url = f"{api_url}/files/{task_id}"
|
| 39 |
+
response = requests.get(questions_url, timeout=15)
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
|
| 42 |
+
# Save the file content to "file.mp3"
|
| 43 |
+
with open(target_filename, "wb") as file:
|
| 44 |
+
file.write(response.content)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def extract_data_from_excel(filename):
|
| 48 |
+
target_file = "temp.json"
|
| 49 |
+
# Read the Excel file
|
| 50 |
+
df = pd.read_excel(filename)
|
| 51 |
+
|
| 52 |
+
# Convert to JSON and save
|
| 53 |
+
df.to_json(target_file, orient="records", indent=2)
|
| 54 |
+
|
| 55 |
+
print(f"Converted {filename} to {target_file}")
|
| 56 |
+
|
| 57 |
+
with open(target_file, "r") as file:
|
| 58 |
+
return file.read()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def extract_code_from_file(filename):
|
| 62 |
+
with open(filename, "r") as file:
|
| 63 |
+
return file.read()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def convert_from_mp3_to_wav(sourceFile, targetFile):
|
| 67 |
+
# convert mp3 file to wav
|
| 68 |
+
sound = AudioSegment.from_mp3(sourceFile)
|
| 69 |
+
sound.export(targetFile, format="wav")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def read_audio(filename):
|
| 73 |
+
# use the audio file as the audio source
|
| 74 |
+
r = sr.Recognizer()
|
| 75 |
+
with sr.AudioFile(filename) as source:
|
| 76 |
+
audio = r.record(source) # read the entire audio file
|
| 77 |
+
|
| 78 |
+
return r.recognize_google(audio)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def transcribe(filename):
|
| 82 |
+
wav_filename = "transcript.wav"
|
| 83 |
+
convert_from_mp3_to_wav(filename, wav_filename)
|
| 84 |
+
return read_audio(wav_filename)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 88 |
"""
|
| 89 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 90 |
and displays the results.
|
| 91 |
"""
|
| 92 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 93 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 94 |
|
| 95 |
if profile:
|
| 96 |
+
username = f"{profile.username}"
|
| 97 |
print(f"User logged in: {username}")
|
| 98 |
else:
|
| 99 |
print("User not logged in.")
|
|
|
|
| 105 |
|
| 106 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 107 |
try:
|
| 108 |
+
agent = Agent()
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Error instantiating agent: {e}")
|
| 111 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 120 |
response.raise_for_status()
|
| 121 |
questions_data = response.json()
|
| 122 |
if not questions_data:
|
| 123 |
+
print("Fetched questions list is empty.")
|
| 124 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 125 |
print(f"Fetched {len(questions_data)} questions.")
|
| 126 |
except requests.exceptions.RequestException as e:
|
| 127 |
print(f"Error fetching questions: {e}")
|
| 128 |
return f"Error fetching questions: {e}", None
|
| 129 |
except requests.exceptions.JSONDecodeError as e:
|
| 130 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 131 |
+
print(f"Response text: {response.text[:500]}")
|
| 132 |
+
return f"Error decoding server response for questions: {e}", None
|
| 133 |
except Exception as e:
|
| 134 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 135 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 142 |
task_id = item.get("task_id")
|
| 143 |
question_text = item.get("question")
|
| 144 |
question_file = item.get("file_name")
|
| 145 |
+
has_file = question_file != ""
|
| 146 |
+
is_mp3 = question_file.endswith(".mp3")
|
| 147 |
+
is_py = question_file.endswith(".py")
|
| 148 |
+
is_excel = question_file.endswith(".xlsx")
|
| 149 |
+
|
| 150 |
if not task_id or question_text is None:
|
| 151 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 152 |
continue
|
| 153 |
try:
|
| 154 |
+
if has_file and is_mp3:
|
| 155 |
+
download_file(task_id, question_file)
|
| 156 |
+
transcript = transcribe(question_file)
|
| 157 |
+
question_text += f"\nThe questions has an audio transcript. It goes like this: {transcript}"
|
| 158 |
+
if has_file and is_py:
|
| 159 |
+
download_file(task_id, question_file)
|
| 160 |
+
code = extract_code_from_file(question_file)
|
| 161 |
+
question_text += f"\nThe questions contains python code: {code}"
|
| 162 |
+
if has_file and is_excel:
|
| 163 |
+
download_file(task_id, question_file)
|
| 164 |
+
data = extract_data_from_excel(question_file)
|
| 165 |
+
question_text += f"\nThe questions contains data: {data}"
|
| 166 |
+
|
| 167 |
+
raw_answer = agent(question_text)
|
| 168 |
+
|
| 169 |
+
if isinstance(raw_answer, str) and "AGENT ERROR" in raw_answer:
|
| 170 |
+
print("retry")
|
| 171 |
+
raw_answer = agent(question_text)
|
| 172 |
+
|
| 173 |
+
# if isinstance(raw_answer, str):
|
| 174 |
+
# submitted_answer = raw_answer.split("</think>")[-1].strip()
|
| 175 |
+
# else:
|
| 176 |
+
# submitted_answer = raw_answer
|
| 177 |
+
answers_payload.append(
|
| 178 |
+
{"task_id": task_id, "submitted_answer": raw_answer}
|
| 179 |
+
)
|
| 180 |
+
results_log.append(
|
| 181 |
+
{
|
| 182 |
+
"Task ID": task_id,
|
| 183 |
+
"Question": question_text,
|
| 184 |
+
"Submitted Answer": raw_answer,
|
| 185 |
+
}
|
| 186 |
+
)
|
| 187 |
except Exception as e:
|
| 188 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 189 |
+
results_log.append(
|
| 190 |
+
{
|
| 191 |
+
"Task ID": task_id,
|
| 192 |
+
"Question": question_text,
|
| 193 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 194 |
+
}
|
| 195 |
+
)
|
| 196 |
|
| 197 |
if not answers_payload:
|
| 198 |
print("Agent did not produce any answers to submit.")
|
| 199 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 200 |
|
| 201 |
+
# 4. Prepare Submission
|
| 202 |
+
submission_data = {
|
| 203 |
+
"username": username.strip(),
|
| 204 |
+
"agent_code": agent_code,
|
| 205 |
+
"answers": answers_payload,
|
| 206 |
+
}
|
| 207 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 208 |
print(status_update)
|
| 209 |
|
|
|
|
| 221 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 222 |
)
|
| 223 |
print("Submission successful.")
|
| 224 |
+
print(final_status)
|
| 225 |
results_df = pd.DataFrame(results_log)
|
| 226 |
return final_status, results_df
|
| 227 |
except requests.exceptions.HTTPError as e:
|
|
|
|
| 274 |
|
| 275 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 276 |
|
| 277 |
+
status_output = gr.Textbox(
|
| 278 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
| 279 |
+
)
|
| 280 |
# Removed max_rows=10 from DataFrame constructor
|
| 281 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 282 |
|
| 283 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
if __name__ == "__main__":
|
| 286 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 287 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 288 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 289 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 290 |
|
| 291 |
if space_host_startup:
|
| 292 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 294 |
else:
|
| 295 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 296 |
|
| 297 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 298 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 299 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 300 |
+
print(
|
| 301 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
| 302 |
+
)
|
| 303 |
else:
|
| 304 |
+
print(
|
| 305 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
| 306 |
+
)
|
| 307 |
|
| 308 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 309 |
|
| 310 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 311 |
+
demo.launch(debug=True, share=False)
|