AbuAlone09 commited on
Commit
a91229a
·
verified ·
1 Parent(s): b7ad8b9

Update backend_api.py

Browse files
Files changed (1) hide show
  1. backend_api.py +53 -22
backend_api.py CHANGED
@@ -1333,26 +1333,41 @@ async def import_model(path: str, prefer_local: bool = False):
1333
  "metadata": {}
1334
  }
1335
 
 
 
 
 
 
1336
 
1337
- @app.get("/api/import/github/{owner}/{repo}")
1338
- async def import_github(owner: str, repo: str):
1339
- """Import a GitHub repository by owner and repo name"""
1340
  try:
1341
- importer = ProjectImporter()
1342
- result = importer.import_github_repo(owner, repo)
1343
- return result
1344
- except Exception as e:
1345
- return {
1346
- "status": "error",
1347
- "message": f"Failed to import repository: {str(e)}",
1348
- "code": "",
1349
- "language": "python",
1350
- "url": f"https://github.com/{owner}/{repo}",
1351
- "metadata": {}
1352
- }
1353
-
1354
-
1355
- # ... (Phần code cũ của bạn) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
1356
 
1357
  # Send completion
1358
  await websocket.send_json({
@@ -1360,7 +1375,7 @@ async def import_github(owner: str, repo: str):
1360
  "code": sample_code
1361
  })
1362
 
1363
- # --- CHÈN ĐOẠN NÀY VÀO ĐỂ TỰ ĐỘNG DEPLOY ---
1364
  try:
1365
  from backend_deploy import deploy_to_huggingface_space
1366
  print(f"DEBUG: [FLOW] Bắt đầu deploy tự động cho {language}...")
@@ -1369,11 +1384,10 @@ async def import_github(owner: str, repo: str):
1369
  success, msg, url = deploy_to_huggingface_space(
1370
  code=sample_code,
1371
  language=language,
1372
- # Tên space tự sinh để tránh trùng lặp
1373
  space_name=f"anycoder-{secrets.token_hex(4)}"
1374
  )
1375
 
1376
- # Gửi kết quả deploy về cho frontend hiển thị
1377
  await websocket.send_json({
1378
  "type": "deploy_status",
1379
  "success": success,
@@ -1383,8 +1397,25 @@ async def import_github(owner: str, repo: str):
1383
  print(f"DEBUG: [FLOW] Kết quả deploy: {success}, URL: {url}")
1384
  except Exception as e:
1385
  print(f"DEBUG: [FLOW] Lỗi gọi deploy: {str(e)}")
1386
- # -------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1387
 
 
1388
  if __name__ == "__main__":
1389
  import uvicorn
1390
  uvicorn.run("backend_api:app", host="0.0.0.0", port=8000, reload=True)
 
1333
  "metadata": {}
1334
  }
1335
 
1336
+ @app.websocket("/ws/generate")
1337
+ async def websocket_generate(websocket: WebSocket):
1338
+ """WebSocket endpoint for real-time code generation"""
1339
+ await websocket.accept()
1340
+ import secrets # Đảm bảo đã import secrets để tạo tên space ngẫu nhiên
1341
 
 
 
 
1342
  try:
1343
+ while True:
1344
+ # Receive message from client
1345
+ data = await websocket.receive_json()
1346
+
1347
+ query = data.get("query")
1348
+ language = data.get("language", "html")
1349
+ model_id = data.get("model_id", "claude-opus-4.5")
1350
+
1351
+ # Send acknowledgment
1352
+ await websocket.send_json({
1353
+ "type": "status",
1354
+ "message": "Generating code..."
1355
+ })
1356
+
1357
+ # Mock code generation
1358
+ await asyncio.sleep(0.5)
1359
+
1360
+ # Code mẫu
1361
+ sample_code = f"\n<h1>Hello from AnyCoder!</h1>"
1362
+
1363
+ # Stream code
1364
+ for i, char in enumerate(sample_code):
1365
+ await websocket.send_json({
1366
+ "type": "chunk",
1367
+ "content": char,
1368
+ "progress": (i + 1) / len(sample_code) * 100
1369
+ })
1370
+ await asyncio.sleep(0.01)
1371
 
1372
  # Send completion
1373
  await websocket.send_json({
 
1375
  "code": sample_code
1376
  })
1377
 
1378
+ # Tự động Deploy
1379
  try:
1380
  from backend_deploy import deploy_to_huggingface_space
1381
  print(f"DEBUG: [FLOW] Bắt đầu deploy tự động cho {language}...")
 
1384
  success, msg, url = deploy_to_huggingface_space(
1385
  code=sample_code,
1386
  language=language,
 
1387
  space_name=f"anycoder-{secrets.token_hex(4)}"
1388
  )
1389
 
1390
+ # Gửi kết quả về frontend
1391
  await websocket.send_json({
1392
  "type": "deploy_status",
1393
  "success": success,
 
1397
  print(f"DEBUG: [FLOW] Kết quả deploy: {success}, URL: {url}")
1398
  except Exception as e:
1399
  print(f"DEBUG: [FLOW] Lỗi gọi deploy: {str(e)}")
1400
+ await websocket.send_json({
1401
+ "type": "deploy_status",
1402
+ "success": False,
1403
+ "message": f"Deploy failed: {str(e)}",
1404
+ "url": None
1405
+ })
1406
+
1407
+ except WebSocketDisconnect:
1408
+ print("Client disconnected")
1409
+ except Exception as e:
1410
+ print(f"Error: {str(e)}")
1411
+ try:
1412
+ await websocket.send_json({"type": "error", "message": str(e)})
1413
+ await websocket.close()
1414
+ except:
1415
+ pass
1416
+
1417
 
1418
+
1419
  if __name__ == "__main__":
1420
  import uvicorn
1421
  uvicorn.run("backend_api:app", host="0.0.0.0", port=8000, reload=True)