|
|
|
import os |
|
import subprocess |
|
import sys |
|
import time |
|
from pathlib import Path |
|
import signal |
|
import shutil |
|
|
|
def main(): |
|
processes = [] |
|
try: |
|
|
|
api_binary = Path("/app/server/bin/api") |
|
playground_dir = Path("/app/playground") |
|
|
|
|
|
if not api_binary.exists(): |
|
print(f"ERROR: API binary not found at {api_binary}", file=sys.stderr) |
|
return 1 |
|
|
|
if not playground_dir.exists(): |
|
print(f"ERROR: Playground directory not found at {playground_dir}", file=sys.stderr) |
|
return 1 |
|
|
|
|
|
from http.server import HTTPServer, SimpleHTTPRequestHandler |
|
|
|
class CustomHandler(SimpleHTTPRequestHandler): |
|
def do_GET(self): |
|
if self.path == '/': |
|
self.send_response(200) |
|
self.send_header('Content-type', 'text/html; charset=utf-8') |
|
self.end_headers() |
|
|
|
html_content = """ |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>TEN Agent - Hugging Face Space</title> |
|
<meta charset="utf-8"> |
|
<style> |
|
body { font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; } |
|
h1 { color: #333; } |
|
.info { background: #f8f9fa; border-left: 4px solid #28a745; padding: 15px; margin-bottom: 20px; } |
|
.endpoint { background: #e9ecef; padding: 10px; border-radius: 5px; font-family: monospace; } |
|
.api { margin-top: 30px; } |
|
</style> |
|
</head> |
|
<body> |
|
<h1>TEN Agent запущен успешно!</h1> |
|
<div class="info"> |
|
<p>TEN Agent API сервер работает на порту 8080.</p> |
|
<p>В связи с ограничениями Hugging Face Space, полноценный интерфейс Playground UI не может быть запущен напрямую.</p> |
|
</div> |
|
|
|
<div class="api"> |
|
<h2>API эндпоинты:</h2> |
|
<ul> |
|
<li><span class="endpoint">GET /health</span> - проверка состояния сервера</li> |
|
<li><span class="endpoint">GET /list</span> - список запущенных агентов</li> |
|
<li><span class="endpoint">GET /graphs</span> - доступные графы</li> |
|
</ul> |
|
</div> |
|
|
|
<h2>Инструкция по локальному использованию</h2> |
|
<p>Для использования полного интерфейса, подключите локальный Playground к этому API:</p> |
|
<ol> |
|
<li>Клонируйте репозиторий: <code>git clone https://github.com/TEN-framework/TEN-Agent.git</code></li> |
|
<li>Перейдите в директорию playground: <code>cd TEN-Agent/playground</code></li> |
|
<li>Установите зависимости: <code>pnpm install</code></li> |
|
<li>Запустите Playground с подключением к API: <code>AGENT_SERVER_URL=https://nitrox-ten.hf.space pnpm dev</code></li> |
|
<li>Откройте в браузере: <code>http://localhost:3000</code></li> |
|
</ol> |
|
|
|
<p>См. <a href="https://github.com/TEN-framework/TEN-Agent" target="_blank">документацию TEN Agent</a> для получения дополнительной информации.</p> |
|
</body> |
|
</html> |
|
""" |
|
|
|
self.wfile.write(html_content.encode('utf-8')) |
|
elif self.path.startswith('/health') or self.path.startswith('/list') or self.path.startswith('/graphs'): |
|
|
|
self.send_response(301) |
|
self.send_header('Location', f'http://localhost:8080{self.path}') |
|
self.end_headers() |
|
else: |
|
self.send_response(404) |
|
self.send_header('Content-type', 'text/plain; charset=utf-8') |
|
self.end_headers() |
|
self.wfile.write(b'Not Found') |
|
|
|
|
|
print("Starting TEN-Agent API server on port 8080...") |
|
api_server_process = subprocess.Popen([str(api_binary)]) |
|
processes.append(api_server_process) |
|
|
|
|
|
port = 7860 |
|
print(f"Starting HTTP server on port {port}...") |
|
httpd = HTTPServer(('0.0.0.0', port), CustomHandler) |
|
httpd.serve_forever() |
|
|
|
except KeyboardInterrupt: |
|
print("Shutting down...") |
|
finally: |
|
|
|
for proc in processes: |
|
try: |
|
proc.terminate() |
|
proc.wait(timeout=5) |
|
except: |
|
proc.kill() |
|
|
|
return 0 |
|
|
|
if __name__ == "__main__": |
|
|
|
signal.signal(signal.SIGINT, lambda sig, frame: sys.exit(0)) |
|
signal.signal(signal.SIGTERM, lambda sig, frame: sys.exit(0)) |
|
|
|
sys.exit(main()) |