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

update codes

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -5,7 +5,8 @@ import inspect
5
  import pandas as pd
6
  import time
7
 
8
- from smolagents import ApiModel
 
9
 
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -14,27 +15,36 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
15
  class BasicAgent:
16
  def __init__(self):
17
- # Hugging Face ํ™˜๊ฒฝ์—์„œ ๋กœ๊ทธ์ธ ํ† ํฐ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์ธ์ฆ ๋ฉˆ์ถค ํ˜„์ƒ ํ•ด๊ฒฐ
18
- self.model = ApiModel(
19
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
20
- api_key=os.getenv("HF_TOKEN")
21
  )
22
- print("BasicAgent initialized.")
23
 
24
  def __call__(self, question: str) -> str:
25
  print(f"Agent received question (first 50 chars): {question[:50]}...")
26
 
27
  try:
28
- # ์›๋ณธ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ฆ‰์‹œ ์ถ”๋ก ํ•˜์—ฌ ๊ฒฐ๊ณผ ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
29
- messages = [{"role": "user", "content": question + "\n\nProvide the final answer as briefly as possible at the very end."}]
30
- response = self.model(messages)
 
 
31
 
32
- final_answer = response.content.strip()
 
 
 
 
 
 
 
33
  print(f"Agent returning answer: {final_answer}")
34
  return final_answer
35
 
36
  except Exception as e:
37
- print(f"Error running model: {e}")
38
  return "unknown"
39
 
40
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
5
  import pandas as pd
6
  import time
7
 
8
+ # smolagents์˜ ๋ณต์žกํ•œ ์ถ”์ƒ ์—์ด์ „ํŠธ ๊ฐ์ฒด๋ฅผ ์“ฐ์ง€ ์•Š๊ณ , ๊ฐ€์žฅ ๋กœ์šฐ๋ ˆ๋ฒจ์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋งŒ ์‚ฌ์šฉ
9
+ from huggingface_hub import InferenceClient
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
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]}...")
27
 
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=512,
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 via InferenceClient: {e}")
48
  return "unknown"
49
 
50
  def run_and_submit_all( profile: gr.OAuthProfile | None):