akhaliq HF Staff commited on
Commit
63db11a
·
1 Parent(s): 7771b05

clean up code

Browse files
backend_api.py CHANGED
@@ -12,6 +12,7 @@ from datetime import datetime
12
  import secrets
13
  import base64
14
  import urllib.parse
 
15
 
16
  # Import only what we need, avoiding Gradio UI imports
17
  import sys
@@ -373,6 +374,137 @@ async def auth_status(authorization: Optional[str] = Header(None)):
373
  )
374
 
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  @app.post("/api/generate")
377
  async def generate_code(
378
  request: CodeGenerationRequest,
@@ -482,6 +614,9 @@ async def generate_code(
482
  })
483
  yield f"data: {event_data}\n\n"
484
 
 
 
 
485
  # Send completion event (optimized - no timestamp in hot path)
486
  completion_data = json.dumps({
487
  "type": "complete",
 
12
  import secrets
13
  import base64
14
  import urllib.parse
15
+ import re
16
 
17
  # Import only what we need, avoiding Gradio UI imports
18
  import sys
 
374
  )
375
 
376
 
377
+ def cleanup_generated_code(code: str, language: str) -> str:
378
+ """Remove LLM explanatory text and extract only the actual code"""
379
+ try:
380
+ original_code = code
381
+
382
+ # Special handling for ComfyUI JSON
383
+ if language == "comfyui":
384
+ # Try to parse as JSON first
385
+ try:
386
+ json.loads(code)
387
+ return code # If it parses, return as-is
388
+ except json.JSONDecodeError:
389
+ pass
390
+
391
+ # Find the last } in the code
392
+ last_brace = code.rfind('}')
393
+ if last_brace != -1:
394
+ # Extract everything up to and including the last }
395
+ potential_json = code[:last_brace + 1]
396
+
397
+ # Try to find where the JSON actually starts
398
+ json_start = 0
399
+ if '```json' in potential_json:
400
+ match = re.search(r'```json\s*\n', potential_json)
401
+ if match:
402
+ json_start = match.end()
403
+ elif '```' in potential_json:
404
+ match = re.search(r'```\s*\n', potential_json)
405
+ if match:
406
+ json_start = match.end()
407
+
408
+ # Extract the JSON
409
+ cleaned_json = potential_json[json_start:].strip()
410
+ cleaned_json = re.sub(r'```\s*$', '', cleaned_json).strip()
411
+
412
+ # Validate
413
+ try:
414
+ json.loads(cleaned_json)
415
+ return cleaned_json
416
+ except json.JSONDecodeError:
417
+ pass
418
+
419
+ # General cleanup for code languages
420
+ # Remove markdown code blocks and extract code
421
+ if '```' in code:
422
+ # Pattern to match code blocks with language specifiers
423
+ patterns = [
424
+ r'```(?:html|HTML)\s*\n([\s\S]+?)(?:\n```|$)',
425
+ r'```(?:python|py|Python)\s*\n([\s\S]+?)(?:\n```|$)',
426
+ r'```(?:javascript|js|jsx|JavaScript)\s*\n([\s\S]+?)(?:\n```|$)',
427
+ r'```(?:typescript|ts|tsx|TypeScript)\s*\n([\s\S]+?)(?:\n```|$)',
428
+ r'```\s*\n([\s\S]+?)(?:\n```|$)', # Generic code block
429
+ ]
430
+
431
+ for pattern in patterns:
432
+ match = re.search(pattern, code, re.IGNORECASE)
433
+ if match:
434
+ code = match.group(1).strip()
435
+ break
436
+
437
+ # Remove common LLM explanatory patterns
438
+ # Remove lines that start with explanatory text
439
+ lines = code.split('\n')
440
+ cleaned_lines = []
441
+ in_code = False
442
+
443
+ for line in lines:
444
+ stripped = line.strip()
445
+
446
+ # Skip common explanatory patterns at the start
447
+ if not in_code and (
448
+ stripped.lower().startswith('here') or
449
+ stripped.lower().startswith('this') or
450
+ stripped.lower().startswith('the above') or
451
+ stripped.lower().startswith('note:') or
452
+ stripped.lower().startswith('explanation:') or
453
+ stripped.lower().startswith('to use') or
454
+ stripped.lower().startswith('usage:') or
455
+ stripped.lower().startswith('instructions:') or
456
+ stripped.startswith('===') and '===' in stripped # Section markers
457
+ ):
458
+ continue
459
+
460
+ # Once we hit actual code, we're in
461
+ if stripped and not stripped.startswith('#') and not stripped.startswith('//'):
462
+ in_code = True
463
+
464
+ cleaned_lines.append(line)
465
+
466
+ code = '\n'.join(cleaned_lines).strip()
467
+
468
+ # Remove trailing explanatory text after the code ends
469
+ # For HTML: remove everything after final closing tag
470
+ if language == "html":
471
+ # Find last </html> or </body> or </div> at root level
472
+ last_html = code.rfind('</html>')
473
+ last_body = code.rfind('</body>')
474
+ last_tag = max(last_html, last_body)
475
+ if last_tag != -1:
476
+ # Check if there's significant text after
477
+ after_tag = code[last_tag + 7:].strip() # +7 for </html> length
478
+ if after_tag and len(after_tag) > 100: # Significant explanatory text
479
+ code = code[:last_tag + 7].strip()
480
+
481
+ # For Python: remove text after the last function/class definition or code block
482
+ elif language in ["gradio", "streamlit"]:
483
+ # Find the last line that looks like actual code (not comments or blank)
484
+ lines = code.split('\n')
485
+ last_code_line = -1
486
+ for i in range(len(lines) - 1, -1, -1):
487
+ stripped = lines[i].strip()
488
+ if stripped and not stripped.startswith('#') and not stripped.startswith('"""') and not stripped.startswith("'''"):
489
+ # This looks like actual code
490
+ last_code_line = i
491
+ break
492
+
493
+ if last_code_line != -1 and last_code_line < len(lines) - 5:
494
+ # If there are more than 5 lines after last code, likely explanatory
495
+ code = '\n'.join(lines[:last_code_line + 1])
496
+
497
+ # Return cleaned code or original if cleaning made it too short
498
+ if len(code) > 50:
499
+ return code
500
+ else:
501
+ return original_code
502
+
503
+ except Exception as e:
504
+ print(f"[Code Cleanup] Error for {language}: {e}")
505
+ return code
506
+
507
+
508
  @app.post("/api/generate")
509
  async def generate_code(
510
  request: CodeGenerationRequest,
 
614
  })
615
  yield f"data: {event_data}\n\n"
616
 
617
+ # Clean up generated code (remove LLM explanatory text and markdown)
618
+ generated_code = cleanup_generated_code(generated_code, language)
619
+
620
  # Send completion event (optimized - no timestamp in hot path)
621
  completion_data = json.dumps({
622
  "type": "complete",
frontend/src/components/LandingPage.tsx CHANGED
@@ -274,7 +274,7 @@ export default function LandingPage({
274
  Build with AnyCoder
275
  </h2>
276
  <p className="text-lg md:text-xl text-[#86868b] font-normal">
277
- Create apps and websites by chatting with AI
278
  </p>
279
  </div>
280
 
 
274
  Build with AnyCoder
275
  </h2>
276
  <p className="text-lg md:text-xl text-[#86868b] font-normal">
277
+ Create apps with AI
278
  </p>
279
  </div>
280