Spaces:
Paused
Paused
Update server.py
Browse files
server.py
CHANGED
|
@@ -1,59 +1,61 @@
|
|
| 1 |
-
# server.py
|
| 2 |
-
import os
|
| 3 |
-
import uuid
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
app =
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
.
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
print("
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
# server.py
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import subprocess
|
| 5 |
+
import flask
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from livekit import api
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
LIVEKIT_URL = os.environ.get("LIVEKIT_URL")
|
| 13 |
+
LIVEKIT_API_KEY = os.environ.get("LIVEKIT_API_KEY")
|
| 14 |
+
LIVEKIT_API_SECRET = os.environ.get("LIVEKIT_API_SECRET")
|
| 15 |
+
ROOM_NAME = "rajesh-portfolio-room"
|
| 16 |
+
|
| 17 |
+
if not all([LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET]):
|
| 18 |
+
raise EnvironmentError("LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET must be set")
|
| 19 |
+
|
| 20 |
+
app = flask.Flask(__name__, template_folder="templates")
|
| 21 |
+
CORS(app, resources={r"/get-token": {"origins": "*"}})
|
| 22 |
+
|
| 23 |
+
@app.route("/")
|
| 24 |
+
def index():
|
| 25 |
+
return flask.render_template("index.html")
|
| 26 |
+
|
| 27 |
+
@app.route("/get-token", methods=["GET"])
|
| 28 |
+
def get_token():
|
| 29 |
+
identity = f"user-{uuid.uuid4()}"
|
| 30 |
+
token = (
|
| 31 |
+
api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)
|
| 32 |
+
.with_identity(identity)
|
| 33 |
+
.with_name(f"Visitor-{identity}")
|
| 34 |
+
.with_grants(api.VideoGrants(room_join=True, room=ROOM_NAME))
|
| 35 |
+
)
|
| 36 |
+
return flask.jsonify({"token": token.to_jwt(), "livekitUrl": LIVEKIT_URL})
|
| 37 |
+
|
| 38 |
+
# Start agent as subprocess before Flask starts
|
| 39 |
+
agent_process = None
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
print("=" * 60)
|
| 43 |
+
print("Starting LiveKit Agent worker...")
|
| 44 |
+
print("=" * 60)
|
| 45 |
+
|
| 46 |
+
# Start agent as non-blocking subprocess
|
| 47 |
+
agent_process = subprocess.Popen(
|
| 48 |
+
["python", "app.py"],
|
| 49 |
+
stdout=subprocess.PIPE,
|
| 50 |
+
stderr=subprocess.STDOUT,
|
| 51 |
+
text=True,
|
| 52 |
+
bufsize=1
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
print("[AGENT] Process started with PID:", agent_process.pid)
|
| 56 |
+
print("=" * 60)
|
| 57 |
+
print("Starting Flask server...")
|
| 58 |
+
print("=" * 60)
|
| 59 |
+
|
| 60 |
+
# Run Flask (this blocks)
|
| 61 |
app.run(host="0.0.0.0", port=7860)
|