Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,20 @@ import os
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
import google.generativeai as genai
|
| 8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
|
|
|
| 9 |
|
| 10 |
-
# System prompt
|
| 11 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 12 |
-
Report your thoughts, and finish your answer with just the answer — no prefixes like
|
| 13 |
Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
| 14 |
If you're asked for a number, don’t use commas or units like $ or %, unless specified.
|
| 15 |
If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
|
| 16 |
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
-
# Generation result wrapper
|
| 20 |
-
class GenerationResult:
|
| 21 |
-
def __init__(self, content, token_usage=None, input_tokens=0, output_tokens=0):
|
| 22 |
-
self.content = content
|
| 23 |
-
self.token_usage = token_usage or {}
|
| 24 |
-
self.input_tokens = input_tokens
|
| 25 |
-
self.output_tokens = output_tokens
|
| 26 |
-
|
| 27 |
# Gemini model wrapper
|
| 28 |
class GeminiFlashModel:
|
| 29 |
def __init__(self, model_id="gemini-1.5-flash", api_key=None):
|
|
@@ -46,32 +40,42 @@ class GeminiFlashModel:
|
|
| 46 |
|
| 47 |
try:
|
| 48 |
response = self.model.generate_content(prompt)
|
| 49 |
-
return
|
| 50 |
content=response.text.strip(),
|
| 51 |
-
token_usage={},
|
| 52 |
input_tokens=0,
|
| 53 |
-
output_tokens=0
|
|
|
|
| 54 |
)
|
| 55 |
except Exception as e:
|
| 56 |
-
return
|
| 57 |
content=f"GENERATION ERROR: {e}",
|
| 58 |
-
token_usage={},
|
| 59 |
input_tokens=0,
|
| 60 |
-
output_tokens=0
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Agent
|
| 64 |
class MyAgent:
|
| 65 |
def __init__(self):
|
| 66 |
-
self.model = GeminiFlashModel()
|
| 67 |
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
|
| 68 |
|
| 69 |
-
def __call__(self, question: str):
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
| 74 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
| 75 |
space_id = os.getenv("SPACE_ID")
|
| 76 |
|
| 77 |
if profile:
|
|
@@ -105,32 +109,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 105 |
if not task_id or question_text is None:
|
| 106 |
continue
|
| 107 |
|
|
|
|
| 108 |
try:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
elif isinstance(result, dict) and "content" in result:
|
| 114 |
-
submitted_answer = result["content"]
|
| 115 |
-
else:
|
| 116 |
-
submitted_answer = str(result)
|
| 117 |
-
|
| 118 |
-
answers_payload.append({
|
| 119 |
-
"task_id": task_id,
|
| 120 |
-
"submitted_answer": submitted_answer
|
| 121 |
-
})
|
| 122 |
-
|
| 123 |
-
results_log.append({
|
| 124 |
-
"Task ID": task_id,
|
| 125 |
-
"Question": question_text,
|
| 126 |
-
"Submitted Answer": submitted_answer
|
| 127 |
-
})
|
| 128 |
-
|
| 129 |
except Exception as e:
|
|
|
|
|
|
|
| 130 |
results_log.append({
|
| 131 |
"Task ID": task_id,
|
| 132 |
"Question": question_text,
|
| 133 |
-
"Submitted Answer":
|
| 134 |
})
|
| 135 |
|
| 136 |
if not answers_payload:
|
|
@@ -157,7 +148,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 157 |
except Exception as e:
|
| 158 |
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
| 159 |
|
| 160 |
-
# UI setup
|
| 161 |
with gr.Blocks() as demo:
|
| 162 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 163 |
gr.Markdown("""
|
|
@@ -183,3 +174,4 @@ if __name__ == "__main__":
|
|
| 183 |
|
| 184 |
|
| 185 |
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
+
|
| 8 |
import google.generativeai as genai
|
| 9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 10 |
+
from smolagents.model.base import ModelOutput # import ModelOutput if available
|
| 11 |
|
| 12 |
+
# System prompt used by the agent
|
| 13 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 14 |
+
Report your thoughts, and finish your answer with just the answer — no prefixes like "FINAL ANSWER:".
|
| 15 |
Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
| 16 |
If you're asked for a number, don’t use commas or units like $ or %, unless specified.
|
| 17 |
If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
|
| 18 |
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Gemini model wrapper
|
| 22 |
class GeminiFlashModel:
|
| 23 |
def __init__(self, model_id="gemini-1.5-flash", api_key=None):
|
|
|
|
| 40 |
|
| 41 |
try:
|
| 42 |
response = self.model.generate_content(prompt)
|
| 43 |
+
return ModelOutput(
|
| 44 |
content=response.text.strip(),
|
|
|
|
| 45 |
input_tokens=0,
|
| 46 |
+
output_tokens=0,
|
| 47 |
+
token_usage={}
|
| 48 |
)
|
| 49 |
except Exception as e:
|
| 50 |
+
return ModelOutput(
|
| 51 |
content=f"GENERATION ERROR: {e}",
|
|
|
|
| 52 |
input_tokens=0,
|
| 53 |
+
output_tokens=0,
|
| 54 |
+
token_usage={}
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Agent wrapper
|
| 58 |
class MyAgent:
|
| 59 |
def __init__(self):
|
| 60 |
+
self.model = GeminiFlashModel(model_id="gemini-1.5-flash")
|
| 61 |
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
|
| 62 |
|
| 63 |
+
def __call__(self, question: str) -> str:
|
| 64 |
+
result = self.agent.run(question)
|
| 65 |
+
print(f"[DEBUG] Agent run result type: {type(result)}; value: {result}")
|
| 66 |
|
| 67 |
+
# Return string content only
|
| 68 |
+
if hasattr(result, "content"):
|
| 69 |
+
return result.content
|
| 70 |
+
elif isinstance(result, dict):
|
| 71 |
+
return result.get("content", str(result))
|
| 72 |
+
else:
|
| 73 |
+
return str(result)
|
| 74 |
|
| 75 |
+
# Main evaluation function
|
| 76 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 77 |
+
print("Starting run_and_submit_all...")
|
| 78 |
+
|
| 79 |
space_id = os.getenv("SPACE_ID")
|
| 80 |
|
| 81 |
if profile:
|
|
|
|
| 109 |
if not task_id or question_text is None:
|
| 110 |
continue
|
| 111 |
|
| 112 |
+
print(f"Running agent on question: {question_text}")
|
| 113 |
try:
|
| 114 |
+
submitted_answer = agent(question_text)
|
| 115 |
+
print(f"Agent answer: {submitted_answer} (type: {type(submitted_answer)})")
|
| 116 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 117 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
+
error_msg = f"AGENT ERROR: {e}"
|
| 120 |
+
print(error_msg)
|
| 121 |
results_log.append({
|
| 122 |
"Task ID": task_id,
|
| 123 |
"Question": question_text,
|
| 124 |
+
"Submitted Answer": error_msg
|
| 125 |
})
|
| 126 |
|
| 127 |
if not answers_payload:
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
| 150 |
|
| 151 |
+
# Gradio UI setup
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 154 |
gr.Markdown("""
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
+
|