Qscar KIM commited on
Commit
1cae44e
ยท
1 Parent(s): 359e474

update codes

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -5,8 +5,8 @@ import inspect
5
  import pandas as pd
6
  import time
7
 
8
- # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถฉ๋Œ๊ณผ ์—์ด์ „ํŠธ ๋ฃจํ”„ ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋ช…ํ™•ํ•œ ์ธํผ๋Ÿฐ์Šค ํด๋ผ์ด์–ธํŠธ๋งŒ ์‚ฌ์šฉ
9
- from huggingface_hub import InferenceClient
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, hf_token: str):
18
- # ์œ ์ € ๋กœ๊ทธ์ธ์œผ๋กœ ๋ฐ›์•„์˜จ ํ† ํฐ์„ ๋‹ค์ด๋ ‰ํŠธ๋กœ ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์ธ์ฆ ์‹คํŒจ๋ฅผ ์›์ฒœ ์ฐจ๋‹จ
19
- self.client = InferenceClient(
20
- model="Qwen/Qwen2.5-Coder-32B-Instruct",
21
- token=hf_token
22
  )
23
- print("BasicAgent initialized with authenticated InferenceClient.")
 
 
 
 
 
 
 
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"Provide the final answer as briefly as possible at the very end. "
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
- final_answer = response.strip()
 
43
  print(f"Agent returning answer: {final_answer}")
44
  return final_answer
45
 
46
  except Exception as e:
47
- print(f"Error running model: {e}")
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
- # ๐ŸŒŸ [์ค‘์š”] ์œ ์ € ๋กœ๊ทธ์ธ ์ •๋ณด์—์„œ ์ง„์งœ ํ† ํฐ(Oauth Token)์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค.
59
  if profile:
60
- username = f"{profile.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(hf_token=hf_token)
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