openfree commited on
Commit
a93f9ef
·
verified ·
1 Parent(s): 1b8cc6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -109
app.py CHANGED
@@ -528,110 +528,7 @@ def generate_space_name():
528
  letters = string.ascii_lowercase
529
  return ''.join(random.choice(letters) for i in range(6))
530
 
531
- # 배포 함수 단순화
532
- def deploy_to_vercel(code: str):
533
- """
534
- Vercel에 배포하는 함수 - 직접 HTML 결과 반환
535
- """
536
- try:
537
- if not code or code.strip() == '':
538
- return """
539
- <div style="padding: 15px; background: #ffebee; border-left: 5px solid #f44336; margin-bottom: 15px; border-radius: 4px;">
540
- <strong style="color: #d32f2f;">⚠️ 배포 실패</strong>
541
- <p style="margin: 5px 0 0 0;">배포할 코드가 없습니다. 먼저 게임 코드를 생성해주세요.</p>
542
- </div>
543
- """
544
-
545
- token = "A8IFZmgW2cqA4yUNlLPnci0N" # 실제 토큰 필요
546
- if not token:
547
- return """
548
- <div style="padding: 15px; background: #ffebee; border-left: 5px solid #f44336; margin-bottom: 15px; border-radius: 4px;">
549
- <strong style="color: #d32f2f;">⚠️ 배포 실패</strong>
550
- <p style="margin: 5px 0 0 0;">Vercel 토큰이 설정되지 않았습니다.</p>
551
- </div>
552
- """
553
 
554
- # 코드 전처리
555
- clean_code = remove_code_block(code)
556
-
557
- project_name = ''.join(random.choice(string.ascii_lowercase) for i in range(6))
558
- deploy_url = "https://api.vercel.com/v13/deployments"
559
- headers = {
560
- "Authorization": f"Bearer {token}",
561
- "Content-Type": "application/json"
562
- }
563
- package_json = {
564
- "name": project_name,
565
- "version": "1.0.0",
566
- "private": True,
567
- "dependencies": {
568
- "vite": "^5.0.0"
569
- },
570
- "scripts": {
571
- "dev": "vite",
572
- "build": "echo 'No build needed' && mkdir -p dist && cp index.html dist/",
573
- "preview": "vite preview"
574
- }
575
- }
576
- files = [
577
- {
578
- "file": "index.html",
579
- "data": clean_code
580
- },
581
- {
582
- "file": "package.json",
583
- "data": json.dumps(package_json, indent=2)
584
- }
585
- ]
586
- project_settings = {
587
- "buildCommand": "npm run build",
588
- "outputDirectory": "dist",
589
- "installCommand": "npm install",
590
- "framework": None
591
- }
592
- deploy_data = {
593
- "name": project_name,
594
- "files": files,
595
- "target": "production",
596
- "projectSettings": project_settings
597
- }
598
-
599
- # Vercel API 호출
600
- deploy_response = requests.post(deploy_url, headers=headers, json=deploy_data)
601
- if deploy_response.status_code != 200:
602
- return f"""
603
- <div style="padding: 15px; background: #ffebee; border-left: 5px solid #f44336; margin-bottom: 15px; border-radius: 4px;">
604
- <strong style="color: #d32f2f;">⚠️ 배포 실패</strong>
605
- <p style="margin: 5px 0 0 0;">API 오류: {deploy_response.text}</p>
606
- </div>
607
- """
608
-
609
- deployment_url = f"https://{project_name}.vercel.app"
610
- time.sleep(5)
611
-
612
- # 성공 시 HTML로 결과 직접 반환 (게임 링크 + 복사 버튼)
613
- return f"""
614
- <div style="padding: 15px; background: #e8f5e9; border-left: 5px solid #4caf50; margin-bottom: 15px; border-radius: 4px;">
615
- <strong style="color: #2e7d32;">✅ 배포 완료!</strong>
616
- <p style="margin: 5px 0;">게임이 성공적으로 배포되었습니다.</p>
617
- <div style="background: #f5f5f5; padding: 10px; border-radius: 4px; margin-top: 10px; display: flex; align-items: center; justify-content: space-between;">
618
- <a href="{deployment_url}" target="_blank" style="color: #1976d2; text-decoration: none; word-break: break-all; flex: 1;">
619
- {deployment_url}
620
- </a>
621
- <button onclick="navigator.clipboard.writeText('{deployment_url}').then(() => this.textContent = '복사됨!', setTimeout(() => this.textContent = '복사', 1000))"
622
- style="background: #1976d2; color: white; border: none; border-radius: 4px; padding: 5px 10px; margin-left: 10px; cursor: pointer;">
623
- 복사
624
- </button>
625
- </div>
626
- </div>
627
- """
628
- except Exception as e:
629
- return f"""
630
- <div style="padding: 15px; background: #ffebee; border-left: 5px solid #f44336; margin-bottom: 15px; border-radius: 4px;">
631
- <strong style="color: #d32f2f;">⚠️ 시스템 오류</strong>
632
- <p style="margin: 5px 0 0 0;">배포 중 오류 발생: {str(e)}</p>
633
- </div>
634
- """
635
 
636
 
637
  def remove_code_block(text):
@@ -978,9 +875,6 @@ class Demo:
978
  def clear_history(self):
979
  return []
980
 
981
- # ------------------------
982
- # 7) Gradio / Modelscope UI 빌드
983
- # ------------------------
984
 
985
  # ------------------------
986
  # 7) Gradio / Modelscope UI 빌드
@@ -1564,10 +1458,13 @@ with gr.Blocks(css_paths=["app.css"], theme=theme) as demo:
1564
  # 배포 버튼 클릭 - JavaScript 트리거 및 상태 업데이트
1565
 
1566
  deploy_btn.click(
1567
- fn=lambda code: deploy_to_vercel(code), # 직접 HTML 반환
1568
- inputs=[code_output],
1569
- outputs=[deploy_result_container] # 결과 컨테이너에 직접 출력
 
1570
  )
 
 
1571
  # ------------------------
1572
  # 8) 실제 실행
1573
  # ------------------------
 
528
  letters = string.ascii_lowercase
529
  return ''.join(random.choice(letters) for i in range(6))
530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532
 
533
 
534
  def remove_code_block(text):
 
875
  def clear_history(self):
876
  return []
877
 
 
 
 
878
 
879
  # ------------------------
880
  # 7) Gradio / Modelscope UI 빌드
 
1458
  # 배포 버튼 클릭 - JavaScript 트리거 및 상태 업데이트
1459
 
1460
  deploy_btn.click(
1461
+
1462
+ fn=handle_deploy,
1463
+ inputs=[code_output, deploy_status], # handle_deploy가 code, deploy_status 2개 받음
1464
+ outputs=[deploy_result_container, deploy_status]
1465
  )
1466
+
1467
+
1468
  # ------------------------
1469
  # 8) 실제 실행
1470
  # ------------------------