Update app.py
Browse files
app.py
CHANGED
|
@@ -3,64 +3,11 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from
|
| 7 |
-
from io import BytesIO
|
| 8 |
-
import google.generativeai as genai
|
| 9 |
|
| 10 |
-
# (Keep Constants as is)
|
| 11 |
-
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
-
# --- Basic Agent Definition ---
|
| 15 |
-
class BasicAgent:
|
| 16 |
-
def __init__(self):
|
| 17 |
-
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 18 |
-
self.model = genai.GenerativeModel("gemini-1.5-pro")
|
| 19 |
-
self.files_base_url = "https://agents-course-unit4-scoring.hf.space/files"
|
| 20 |
-
print("Gemini-powered BasicAgent initialized.")
|
| 21 |
-
|
| 22 |
-
def __call__(self, question_data: dict) -> str:
|
| 23 |
-
question_text = question_data.get("question", "")
|
| 24 |
-
task_id = question_data.get("task_id", "")
|
| 25 |
-
file_names = question_data.get("file_names", [])
|
| 26 |
-
|
| 27 |
-
# Takeshi Kojima-style Research Advisory Prompt (Enhanced)
|
| 28 |
-
system_prompt = (
|
| 29 |
-
"You are part of an advanced research laboratory composed of intelligent agents who specialize in integrating complex knowledge to solve unknown challenges.\n"
|
| 30 |
-
"Within this lab, there are experts who analyze visual information, interpret documents, and reason through past knowledge to deduce accurate conclusions.\n"
|
| 31 |
-
"This task is highly sophisticated and may require a combination of inference, retrieval, and visual understanding.\n"
|
| 32 |
-
"Approach it collaboratively, methodically, and with precision.\n\n"
|
| 33 |
-
"There is no need to rush.\n"
|
| 34 |
-
"This is an experiment, an investigation, and a pursuit of understanding.\n"
|
| 35 |
-
"Your research team’s calm and thorough process will guide you to the correct answer.\n\n"
|
| 36 |
-
"---\n"
|
| 37 |
-
"However, your final answer must be in English and strictly follow the required format described in the question.\n"
|
| 38 |
-
"You must return only the answer exactly as requested. Do not rephrase, repeat the question, or provide any explanation.\n"
|
| 39 |
-
"If the question asks for a list or a name, return that only.\n\n"
|
| 40 |
-
f"[Problem]\n{question_text}"
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
if file_names:
|
| 45 |
-
file_url = f"{self.files_base_url}/{task_id}/{file_names[0]}"
|
| 46 |
-
response = requests.get(file_url, timeout=10)
|
| 47 |
-
response.raise_for_status()
|
| 48 |
-
image = Image.open(BytesIO(response.content))
|
| 49 |
-
gemini_response = self.model.generate_content([system_prompt, image])
|
| 50 |
-
else:
|
| 51 |
-
gemini_response = self.model.generate_content(system_prompt)
|
| 52 |
-
|
| 53 |
-
return gemini_response.text.strip()
|
| 54 |
-
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"Error generating answer: {e}")
|
| 57 |
-
return f"Error generating answer: {e}"
|
| 58 |
-
|
| 59 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 60 |
-
"""
|
| 61 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 62 |
-
and displays the results.
|
| 63 |
-
"""
|
| 64 |
space_id = os.getenv("SPACE_ID")
|
| 65 |
|
| 66 |
if profile:
|
|
@@ -75,7 +22,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 75 |
submit_url = f"{api_url}/submit"
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
agent =
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error instantiating agent: {e}")
|
| 81 |
return f"Error initializing agent: {e}", None
|
|
@@ -171,19 +118,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 171 |
|
| 172 |
# --- Build Gradio Interface using Blocks ---
|
| 173 |
with gr.Blocks() as demo:
|
| 174 |
-
gr.Markdown("#
|
| 175 |
gr.Markdown(
|
| 176 |
"""
|
| 177 |
**Instructions:**
|
| 178 |
|
| 179 |
-
1.
|
| 180 |
-
2.
|
| 181 |
-
3.
|
| 182 |
-
|
| 183 |
-
---
|
| 184 |
-
**Disclaimers:**
|
| 185 |
-
Once clicking on the "submit" button, it can take quite some time (this is the time for the agent to go through all the questions).
|
| 186 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a separate action or even to answer the questions in async.
|
| 187 |
"""
|
| 188 |
)
|
| 189 |
|
|
@@ -198,23 +140,5 @@ with gr.Blocks() as demo:
|
|
| 198 |
)
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
-
print("
|
| 202 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 203 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 204 |
-
|
| 205 |
-
if space_host_startup:
|
| 206 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 207 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 208 |
-
else:
|
| 209 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 210 |
-
|
| 211 |
-
if space_id_startup:
|
| 212 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 213 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 214 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 215 |
-
else:
|
| 216 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 217 |
-
|
| 218 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 219 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 220 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from agent import Agent # Importing custom Agent
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
space_id = os.getenv("SPACE_ID")
|
| 12 |
|
| 13 |
if profile:
|
|
|
|
| 22 |
submit_url = f"{api_url}/submit"
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
agent = Agent()
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Error instantiating agent: {e}")
|
| 28 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 118 |
|
| 119 |
# --- Build Gradio Interface using Blocks ---
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
+
gr.Markdown("# Advanced Agent Evaluation Runner")
|
| 122 |
gr.Markdown(
|
| 123 |
"""
|
| 124 |
**Instructions:**
|
| 125 |
|
| 126 |
+
1. Clone this space and modify the agent logic in `agent.py`.
|
| 127 |
+
2. Log in to Hugging Face with the button below.
|
| 128 |
+
3. Click 'Run Evaluation & Submit All Answers' to begin.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
"""
|
| 130 |
)
|
| 131 |
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
print("Launching Gradio Interface for Advanced Agent Evaluation...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
demo.launch(debug=True, share=False)
|