Spaces:
Sleeping
Sleeping
Qscar KIM commited on
Commit ยท
1cae44e
1
Parent(s): 359e474
update codes
Browse files
app.py
CHANGED
|
@@ -5,8 +5,8 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
from
|
| 10 |
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -14,13 +14,20 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
-
def __init__(self
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
@@ -28,23 +35,16 @@ class BasicAgent:
|
|
| 28 |
try:
|
| 29 |
refined_prompt = (
|
| 30 |
f"{question}\n\n"
|
| 31 |
-
f"
|
| 32 |
-
f"No explanations in the last line."
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# ์์ด์ ํธ ํ๋ ์์ํฌ์ ๋ฌด๊ฑฐ์ด ์์คํ
ํ๋กฌํํธ ์์ด ์ฆ์ ์ถ๋ก ์ํ
|
| 36 |
-
response = self.client.text_generation(
|
| 37 |
-
refined_prompt,
|
| 38 |
-
max_new_tokens=256,
|
| 39 |
-
clean_up_tokenization_spaces=True
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
print(f"Agent returning answer: {final_answer}")
|
| 44 |
return final_answer
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"Error running
|
| 48 |
return "unknown"
|
| 49 |
|
| 50 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -55,10 +55,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 55 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 56 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if profile:
|
| 60 |
-
username
|
| 61 |
-
hf_token = profile.token # ๋ก๊ทธ์ธํ ์ ์ ์ ์ธ์
ํ ํฐ์ ํ๋ณด
|
| 62 |
print(f"User logged in: {username}")
|
| 63 |
else:
|
| 64 |
print("User not logged in.")
|
|
@@ -68,13 +67,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 68 |
questions_url = f"{api_url}/questions"
|
| 69 |
submit_url = f"{api_url}/submit"
|
| 70 |
|
| 71 |
-
# 1. Instantiate Agent (
|
| 72 |
try:
|
| 73 |
-
agent = BasicAgent(
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error instantiating agent: {e}")
|
| 76 |
return f"Error initializing agent: {e}", None
|
| 77 |
-
|
| 78 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 79 |
print(agent_code)
|
| 80 |
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import time
|
| 7 |
|
| 8 |
+
# Unit 4 ๊ต์ก ๋ชฉ์ ์ ๋ถํฉํ๋ ๊ณต์ smolagents ์ปดํฌ๋ํธ ์ํฌํธ
|
| 9 |
+
from smolagents import CodeAgent, InferenceClientModel
|
| 10 |
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
# ์ต์ smolagents ๋ช
์ธ์ ๋ฐ๋ฅธ Hugging Face Inference API ๋ชจ๋ธ ์ ์
|
| 19 |
+
# ๋ณ๋๋ก ํ ํฐ์ ์ธ์๋ก ์ฃผ์ง ์์๋ ์์คํ
ํ๊ฒฝ ๋ณ์์ HF_TOKEN์ ์๋์ผ๋ก ๊ฐ์ ธ์ต๋๋ค.
|
| 20 |
+
self.model = InferenceClientModel(
|
| 21 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 22 |
)
|
| 23 |
+
|
| 24 |
+
# ๊ฐ์ ๋ชฉ์ ์ ๋ง์ถ์ด ์ค์ค๋ก ๊ณํ์ ์ธ์ฐ๊ณ ํ์ด์ฌ ์ฝ๋๋ฅผ ์คํํ ์ ์๋ ์์จ ์์ด์ ํธ ๊ตฌ์ฑ
|
| 25 |
+
self.agent = CodeAgent(
|
| 26 |
+
tools=[],
|
| 27 |
+
model=self.model,
|
| 28 |
+
additional_authorized_imports=["pandas", "numpy", "json", "math", "re"]
|
| 29 |
+
)
|
| 30 |
+
print("BasicAgent initialized with smolagents CodeAgent.")
|
| 31 |
|
| 32 |
def __call__(self, question: str) -> str:
|
| 33 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 35 |
try:
|
| 36 |
refined_prompt = (
|
| 37 |
f"{question}\n\n"
|
| 38 |
+
f"Solve the task step by step. Provide the final short answer clearly at the very end."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
+
# ์์จ ์ถ๋ก ๋ฐ ์คํ ๋ฃจํ ๊ฐ๋
|
| 41 |
+
result = self.agent.run(refined_prompt)
|
| 42 |
+
final_answer = str(result).strip()
|
| 43 |
print(f"Agent returning answer: {final_answer}")
|
| 44 |
return final_answer
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"Error running agent: {e}")
|
| 48 |
return "unknown"
|
| 49 |
|
| 50 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 55 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 56 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 57 |
|
| 58 |
+
# ์๋ฌ๋ฅผ ์ผ์ผํค๋ profile.token ์ ๊ฑฐ ๋ฐ ์๋์ ํ
ํ๋ฆฟ ๊ฒ์ฆ ๋ก์ง์ผ๋ก ํ์
|
| 59 |
if profile:
|
| 60 |
+
username= f"{profile.username}"
|
|
|
|
| 61 |
print(f"User logged in: {username}")
|
| 62 |
else:
|
| 63 |
print("User not logged in.")
|
|
|
|
| 67 |
questions_url = f"{api_url}/questions"
|
| 68 |
submit_url = f"{api_url}/submit"
|
| 69 |
|
| 70 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 71 |
try:
|
| 72 |
+
agent = BasicAgent()
|
| 73 |
except Exception as e:
|
| 74 |
print(f"Error instantiating agent: {e}")
|
| 75 |
return f"Error initializing agent: {e}", None
|
| 76 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( useful for others so please keep it public)
|
| 77 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 78 |
print(agent_code)
|
| 79 |
|