Kai Izumoto commited on
Commit
e17f2fc
·
verified ·
1 Parent(s): 44859d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -6
app.py CHANGED
@@ -169,14 +169,25 @@ def call_model(client: InferenceClient, system: str, user: str, is_python: bool,
169
  models_to_try = [primary_model] + FALLBACK_MODELS
170
 
171
  logging.info(f"Calling model for {'Python' if is_python else 'Other'} project. Primary: {primary_model}")
 
172
 
173
  messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
174
 
 
 
 
 
 
 
 
 
 
175
  last_exception = None
176
  for model_name in models_to_try:
177
  # First attempt: non-streaming call (more reliable across client versions/models)
178
  try:
179
- resp = client.chat_completion(messages, model=model_name, stream=False, **settings)
 
180
  # resp can be dict-like or string; try multiple extraction methods
181
  response_text = ""
182
  try:
@@ -203,26 +214,35 @@ def call_model(client: InferenceClient, system: str, user: str, is_python: bool,
203
  write_error_log(e, f"Non-stream parsing failed for model {model_name}")
204
 
205
  if response_text and response_text.strip():
 
206
  return response_text
 
 
207
  except Exception as e:
208
  # Save and try streaming fallback below
209
  last_exception = e
210
  write_error_log(e, f"Non-stream model {model_name} failed, attempting stream fallback")
 
211
  # fall through to streaming attempt
212
 
213
  # Streaming fallback (older code path)
214
  try:
215
- stream = client.chat_completion(messages, model=model_name, stream=True, **settings)
 
216
  response = "".join(piece for chunk in stream if (piece := extract_chunk_content(chunk)))
217
  if response.strip():
 
218
  return response
 
 
219
  except Exception as e:
220
  last_exception = e
221
  write_error_log(e, f"Streaming model {model_name} failed")
 
222
  time.sleep(1) # basic backoff and continue to next model
223
  continue
224
 
225
- logging.error(f"All models failed. Last error: {last_exception}")
226
  return f"<<ERROR: All models failed. Last error: {sanitize_log_message(str(last_exception))}>>"
227
 
228
  # ---------- Robust parsing ----------
@@ -418,7 +438,12 @@ def import_project(zip_file) -> Dict[str, str]:
418
  class CodeGenController:
419
  def __init__(self, token: str, goal: str, instructions: str, settings: Dict, max_iters: int, infinite_mode: bool, is_python: bool):
420
  self.token = token
421
- self.client = InferenceClient(token=token)
 
 
 
 
 
422
  self.goal = goal
423
  self.instructions = instructions
424
  self.settings = settings
@@ -427,6 +452,8 @@ class CodeGenController:
427
  self.is_python = is_python
428
  self.model_name = PYTHON_MODEL if is_python else OTHER_MODEL
429
 
 
 
430
  self.history: List[Dict] = []
431
  self.current_files: Dict[str, str] = {}
432
  self.current_code: str = ""
@@ -705,15 +732,23 @@ def create_ui():
705
  def start_gen(goal, init_code, instructions, hf_tok, inf_mode, max_it, temp, top, max_tok):
706
  token = get_token_from_env_or_manual(hf_tok)
707
  if not token:
708
- yield ("ERROR: No HF token found", "", "", "", {}, "", "", "", "", {}, "", None, "", {})
 
 
709
  return
710
 
 
 
711
  settings = {"temperature": temp, "top_p": top, "max_new_tokens": max_tok}
712
- controller = CodeGenController(token, goal, instructions, settings, int(max_it), inf_mode, detect_language(goal, init_code))
 
 
 
713
 
714
  if init_code and init_code.strip():
715
  controller.current_files = {"main.py": init_code}
716
  controller.current_code = init_code
 
717
 
718
  yield from controller.run_loop()
719
 
 
169
  models_to_try = [primary_model] + FALLBACK_MODELS
170
 
171
  logging.info(f"Calling model for {'Python' if is_python else 'Other'} project. Primary: {primary_model}")
172
+ logging.info(f"Settings: {settings}")
173
 
174
  messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
175
 
176
+ # Clean up settings - remove any invalid parameters
177
+ valid_settings = {
178
+ "temperature": settings.get("temperature", 0.5),
179
+ "top_p": settings.get("top_p", 0.9),
180
+ "max_tokens": settings.get("max_new_tokens", settings.get("max_tokens", 4096))
181
+ }
182
+
183
+ logging.info(f"Using cleaned settings: {valid_settings}")
184
+
185
  last_exception = None
186
  for model_name in models_to_try:
187
  # First attempt: non-streaming call (more reliable across client versions/models)
188
  try:
189
+ logging.info(f"Attempting non-streaming call to {model_name}")
190
+ resp = client.chat_completion(messages, model=model_name, stream=False, **valid_settings)
191
  # resp can be dict-like or string; try multiple extraction methods
192
  response_text = ""
193
  try:
 
214
  write_error_log(e, f"Non-stream parsing failed for model {model_name}")
215
 
216
  if response_text and response_text.strip():
217
+ logging.info(f"✓ Successfully got response from {model_name} ({len(response_text)} chars)")
218
  return response_text
219
+ else:
220
+ logging.warning(f"Non-streaming returned empty response from {model_name}")
221
  except Exception as e:
222
  # Save and try streaming fallback below
223
  last_exception = e
224
  write_error_log(e, f"Non-stream model {model_name} failed, attempting stream fallback")
225
+ logging.error(f"Non-stream error for {model_name}: {str(e)[:200]}")
226
  # fall through to streaming attempt
227
 
228
  # Streaming fallback (older code path)
229
  try:
230
+ logging.info(f"Attempting streaming call to {model_name}")
231
+ stream = client.chat_completion(messages, model=model_name, stream=True, **valid_settings)
232
  response = "".join(piece for chunk in stream if (piece := extract_chunk_content(chunk)))
233
  if response.strip():
234
+ logging.info(f"✓ Successfully got streaming response from {model_name} ({len(response)} chars)")
235
  return response
236
+ else:
237
+ logging.warning(f"Streaming returned empty response from {model_name}")
238
  except Exception as e:
239
  last_exception = e
240
  write_error_log(e, f"Streaming model {model_name} failed")
241
+ logging.error(f"Streaming error for {model_name}: {str(e)[:200]}")
242
  time.sleep(1) # basic backoff and continue to next model
243
  continue
244
 
245
+ logging.error(f" ALL MODELS FAILED. Last error: {last_exception}")
246
  return f"<<ERROR: All models failed. Last error: {sanitize_log_message(str(last_exception))}>>"
247
 
248
  # ---------- Robust parsing ----------
 
438
  class CodeGenController:
439
  def __init__(self, token: str, goal: str, instructions: str, settings: Dict, max_iters: int, infinite_mode: bool, is_python: bool):
440
  self.token = token
441
+ try:
442
+ self.client = InferenceClient(token=token)
443
+ logging.info("✓ InferenceClient initialized successfully")
444
+ except Exception as e:
445
+ logging.error(f"Failed to initialize InferenceClient: {e}")
446
+ raise
447
  self.goal = goal
448
  self.instructions = instructions
449
  self.settings = settings
 
452
  self.is_python = is_python
453
  self.model_name = PYTHON_MODEL if is_python else OTHER_MODEL
454
 
455
+ logging.info(f"Controller initialized for {'Python' if is_python else 'Other'} with model: {self.model_name}")
456
+
457
  self.history: List[Dict] = []
458
  self.current_files: Dict[str, str] = {}
459
  self.current_code: str = ""
 
732
  def start_gen(goal, init_code, instructions, hf_tok, inf_mode, max_it, temp, top, max_tok):
733
  token = get_token_from_env_or_manual(hf_tok)
734
  if not token:
735
+ error_msg = "ERROR: No HF token found. Please provide a Hugging Face token."
736
+ logging.error(error_msg)
737
+ yield (error_msg, "", "", "", {}, "", "", "", "", {}, "", None, "", {})
738
  return
739
 
740
+ logging.info(f"Starting generation with token: {token[:10]}... (length: {len(token)})")
741
+
742
  settings = {"temperature": temp, "top_p": top, "max_new_tokens": max_tok}
743
+ is_python_project = detect_language(goal, init_code)
744
+ logging.info(f"Detected project type: {'Python' if is_python_project else 'Other'}")
745
+
746
+ controller = CodeGenController(token, goal, instructions, settings, int(max_it), inf_mode, is_python_project)
747
 
748
  if init_code and init_code.strip():
749
  controller.current_files = {"main.py": init_code}
750
  controller.current_code = init_code
751
+ logging.info("Using provided initial code")
752
 
753
  yield from controller.run_loop()
754