Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
|
|
|
| 7 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
@@ -13,27 +14,27 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
| 16 |
-
def __init__(self
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
-
# Initialize
|
| 19 |
-
|
| 20 |
-
self.HF_API_TOKEN = HF_API_TOKEN
|
| 21 |
-
model = HfApiModel(model_id="openai-community/gpt2", api_key=self.HF_API_TOKEN)
|
| 22 |
|
| 23 |
-
# Initialize the search tool
|
| 24 |
search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 25 |
# Initialize Agent
|
| 26 |
self.agent = CodeAgent(
|
| 27 |
-
model
|
| 28 |
tools=[search_tool]
|
| 29 |
)
|
|
|
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 32 |
-
|
| 33 |
-
print(f"Agent returning fixed answer: {
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None
|
| 37 |
"""
|
| 38 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 39 |
and displays the results.
|
|
@@ -54,7 +55,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, HF_API_TOKEN : str):
|
|
| 54 |
|
| 55 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 56 |
try:
|
| 57 |
-
agent = BasicAgent(
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|
|
@@ -172,9 +173,7 @@ with gr.Blocks() as demo:
|
|
| 172 |
)
|
| 173 |
|
| 174 |
gr.LoginButton()
|
| 175 |
-
|
| 176 |
-
HF_API_TOKEN_box = gr.Textbox(label="HF_API_TOKEN", type="password", placeholder="sk-...", lines=1)
|
| 177 |
-
|
| 178 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 179 |
|
| 180 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
@@ -183,7 +182,6 @@ with gr.Blocks() as demo:
|
|
| 183 |
|
| 184 |
run_button.click(
|
| 185 |
fn=run_and_submit_all,
|
| 186 |
-
inputs=[HF_API_TOKEN_box],
|
| 187 |
|
| 188 |
|
| 189 |
outputs=[status_output, results_table]
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
| 7 |
+
from transformers import pipeline
|
| 8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
+
def __init__(self):
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
+
# Initialize a small local model instead of API model
|
| 20 |
+
self.model = pipeline("text-generation", model="gpt2")
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Initialize the search tool (you can keep DuckDuckGoSearchTool)
|
| 23 |
search_tool = DuckDuckGoSearchTool()
|
| 24 |
+
|
| 25 |
# Initialize Agent
|
| 26 |
self.agent = CodeAgent(
|
| 27 |
+
model=self.model,
|
| 28 |
tools=[search_tool]
|
| 29 |
)
|
| 30 |
+
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 33 |
+
generated_text = self.model(question, max_length=200, num_return_sequences=1)[0]['generated_text']
|
| 34 |
+
print(f"Agent returning fixed answer: {generated_text}")
|
| 35 |
+
return generated_text
|
| 36 |
|
| 37 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 38 |
"""
|
| 39 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 40 |
and displays the results.
|
|
|
|
| 55 |
|
| 56 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 57 |
try:
|
| 58 |
+
agent = BasicAgent()
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error instantiating agent: {e}")
|
| 61 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
gr.LoginButton()
|
| 176 |
+
|
|
|
|
|
|
|
| 177 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 178 |
|
| 179 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 182 |
|
| 183 |
run_button.click(
|
| 184 |
fn=run_and_submit_all,
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
outputs=[status_output, results_table]
|