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

update codes

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -5,7 +5,7 @@ import inspect
5
  import pandas as pd
6
  import time
7
 
8
- # smolagents์˜ ๋ณต์žกํ•œ ์ถ”์ƒ ์—์ด์ „ํŠธ ๊ฐ์ฒด๋ฅผ ์“ฐ์ง€ ์•Š๊ณ , ๊ฐ€์žฅ ๋กœ์šฐ๋ ˆ๋ฒจ์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋งŒ ์‚ฌ์šฉ
9
  from huggingface_hub import InferenceClient
10
 
11
  # --- Constants ---
@@ -14,13 +14,13 @@ 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
- # Hugging Face Spaces ํ™˜๊ฒฝ์— ๊ธฐ๋ณธ ์ œ๊ณต๋˜๋Š” HF_TOKEN ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ
19
  self.client = InferenceClient(
20
  model="Qwen/Qwen2.5-Coder-32B-Instruct",
21
- token=os.getenv("HF_TOKEN")
22
  )
23
- print("BasicAgent initialized with native InferenceClient.")
24
 
25
  def __call__(self, question: str) -> str:
26
  print(f"Agent received question (first 50 chars): {question[:50]}...")
@@ -32,10 +32,10 @@ class BasicAgent:
32
  f"No explanations in the last line."
33
  )
34
 
35
- # ์—์ด์ „ํŠธ ๋ฃจํ”„ ์—†์ด ๋‹จ๋ฐœ์„ฑ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ๊ตฌ์กฐ์  ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
36
  response = self.client.text_generation(
37
  refined_prompt,
38
- max_new_tokens=512,
39
  clean_up_tokenization_spaces=True
40
  )
41
 
@@ -44,7 +44,7 @@ class BasicAgent:
44
  return final_answer
45
 
46
  except Exception as e:
47
- print(f"Error running model via InferenceClient: {e}")
48
  return "unknown"
49
 
50
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -55,8 +55,10 @@ 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
  if profile:
59
- username= f"{profile.username}"
 
60
  print(f"User logged in: {username}")
61
  else:
62
  print("User not logged in.")
@@ -66,13 +68,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
66
  questions_url = f"{api_url}/questions"
67
  submit_url = f"{api_url}/submit"
68
 
69
- # 1. Instantiate Agent ( modify this part to create your agent)
70
  try:
71
- agent = BasicAgent()
72
  except Exception as e:
73
  print(f"Error instantiating agent: {e}")
74
  return f"Error initializing agent: {e}", None
75
- # 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)
76
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
77
  print(agent_code)
78
 
 
5
  import pandas as pd
6
  import time
7
 
8
+ # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถฉ๋Œ๊ณผ ์—์ด์ „ํŠธ ๋ฃจํ”„ ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋ช…ํ™•ํ•œ ์ธํผ๋Ÿฐ์Šค ํด๋ผ์ด์–ธํŠธ๋งŒ ์‚ฌ์šฉ
9
  from huggingface_hub import InferenceClient
10
 
11
  # --- Constants ---
 
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]}...")
 
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
 
 
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
  # --- 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
  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