Imnotchis commited on
Commit
7b5bc4c
·
verified ·
1 Parent(s): cafb27b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -47,25 +47,28 @@ class BasicAgent:
47
  def __init__(self):
48
  print("BasicAgent initialized.")
49
 
50
- # 1. Kiểm tra nghiêm ngặt HF_TOKEN để ngăn chặn lỗi Missing credentials
51
- hf_token = os.environ.get("HF_TOKEN")
52
- if not hf_token:
53
  raise ValueError(
54
- "\n❌ LỖI NGHIÊM TRỌNG: Không tìm thấy HF_TOKEN trong môi trường Space!\n"
55
- "👉 Cách khắc phục: Vào tab Settings của Space -> Variables and secrets -> "
56
- "Tạo Secret mới tên là 'HF_TOKEN' và dán mã token của bạn vào, sau đó bấm Restart Space."
 
 
 
57
  )
58
  else:
59
- print(f"✅ Đã nhận HF_TOKEN thành công (Độ dài: {len(hf_token)} ký tự).")
60
 
61
  search_tool = DuckDuckGoSearchTool()
62
  visit_tool = VisitWebpageTool()
63
 
64
- # 2. Setup LLM sử dụng API miễn phí của Hugging Face qua cổng OpenAI-compatible
65
  self.model = OpenAIServerModel(
66
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
67
- api_base="https://api-inference.huggingface.co/v1/",
68
- api_key=HF_TOKEN
69
  )
70
 
71
  # Setup CodeAgent
@@ -139,6 +142,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
139
  print(f"Skipping item with missing task_id or question: {item}")
140
  continue
141
 
 
142
  local_file_path = None
143
  if file_name and str(file_name).strip() and str(file_name) != "None":
144
  file_url = f"{api_url}/files/{task_id}"
@@ -175,7 +179,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
175
  "Submitted Answer": f"AGENT ERROR: {e}"
176
  })
177
 
178
- # Nghỉ 2 giây sau mỗi câu hỏi để tránh lỗi Rate Limit 429
179
  time.sleep(2)
180
 
181
  if not answers_payload:
 
47
  def __init__(self):
48
  print("BasicAgent initialized.")
49
 
50
+ # 1. Kiểm tra nghiêm ngặt GROQ_API_KEY
51
+ groq_key = os.environ.get("GROQ_API_KEY")
52
+ if not groq_key:
53
  raise ValueError(
54
+ "\n❌ LỖI NGHIÊM TRỌNG: Không tìm thấy GROQ_API_KEY trong môi trường Space!\n"
55
+ "👉 Cách khắc phục:\n"
56
+ " 1. Vào trang https://console.groq.com/keys để tạo API Key miễn phí.\n"
57
+ " 2. Vào tab Settings của Space -> Variables and secrets -> New secret.\n"
58
+ " 3. Đặt Name là 'GROQ_API_KEY', dán mã key của bạn vào Value rồi Save.\n"
59
+ " 4. Bấm Factory Reboot (ở mục Danger Zone) để Space nạp biến mới."
60
  )
61
  else:
62
+ print(f"✅ Đã nhận GROQ_API_KEY thành công (Độ dài: {len(groq_key)} ký tự).")
63
 
64
  search_tool = DuckDuckGoSearchTool()
65
  visit_tool = VisitWebpageTool()
66
 
67
+ # 2. Setup LLM sử dụng API siêu tốc của Groq (Mô hình Llama 3.3 70B)
68
  self.model = OpenAIServerModel(
69
+ model_id="llama-3.3-70b-versatile",
70
+ api_base="https://api.groq.com/openai/v1",
71
+ api_key=groq_key
72
  )
73
 
74
  # Setup CodeAgent
 
142
  print(f"Skipping item with missing task_id or question: {item}")
143
  continue
144
 
145
+ # Xử lý tải file đính kèm với hàm retry chống lỗi 429
146
  local_file_path = None
147
  if file_name and str(file_name).strip() and str(file_name) != "None":
148
  file_url = f"{api_url}/files/{task_id}"
 
179
  "Submitted Answer": f"AGENT ERROR: {e}"
180
  })
181
 
182
+ # Nghỉ 2 giây sau mỗi câu hỏi để tránh bị máy chủ chấm điểm chặn IP
183
  time.sleep(2)
184
 
185
  if not answers_payload: