|
|
|
import os |
|
import subprocess |
|
import sys |
|
import time |
|
from pathlib import Path |
|
|
|
def main(): |
|
|
|
server_process = None |
|
try: |
|
|
|
api_binary = Path("/app/server/bin/api") |
|
|
|
|
|
if not api_binary.exists(): |
|
print(f"ERROR: API binary not found at {api_binary}", file=sys.stderr) |
|
return 1 |
|
|
|
|
|
print("Starting TEN-Agent server...") |
|
server_process = subprocess.Popen([str(api_binary)]) |
|
|
|
|
|
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') |
|
self.end_headers() |
|
|
|
html_content = """ |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>TEN Agent - Hugging Face Space</title> |
|
<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; } |
|
</style> |
|
</head> |
|
<body> |
|
<h1>TEN Agent запущен успешно!</h1> |
|
<div class="info"> |
|
<p>TEN Agent API сервер работает на порту 8080.</p> |
|
</div> |
|
<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> |
|
<p>См. <a href="https://github.com/TEN-framework/TEN-Agent" target="_blank">документацию TEN Agent</a> для получения дополнительной информации.</p> |
|
</body> |
|
</html> |
|
""" |
|
|
|
self.wfile.write(html_content.encode()) |
|
else: |
|
self.send_response(404) |
|
self.send_header('Content-type', 'text/plain') |
|
self.end_headers() |
|
self.wfile.write(b'Not Found') |
|
|
|
|
|
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: |
|
|
|
if server_process: |
|
server_process.terminate() |
|
server_process.wait() |
|
|
|
return 0 |
|
|
|
if __name__ == "__main__": |
|
sys.exit(main()) |