SakibAhmed commited on
Commit
c1b4ab5
·
verified ·
1 Parent(s): 0c56863

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +60 -58
server.py CHANGED
@@ -1,59 +1,61 @@
1
- # server.py
2
- import os
3
- import uuid
4
- from flask import Flask, render_template, jsonify
5
- from flask_cors import CORS
6
- from dotenv import load_dotenv
7
- from livekit import api
8
- import asyncio
9
- import threading
10
-
11
- load_dotenv()
12
-
13
- LIVEKIT_URL = os.environ.get("LIVEKIT_URL")
14
- LIVEKIT_API_KEY = os.environ.get("LIVEKIT_API_KEY")
15
- LIVEKIT_API_SECRET = os.environ.get("LIVEKIT_API_SECRET")
16
- ROOM_NAME = "rajesh-portfolio-room"
17
-
18
- if not all([LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET]):
19
- raise EnvironmentError("Missing LiveKit credentials")
20
-
21
- app = Flask(__name__, template_folder="templates")
22
- CORS(app, resources={r"/get-token": {"origins": "*"}})
23
-
24
- @app.route("/")
25
- def index():
26
- return render_template("index.html")
27
-
28
- @app.route("/get-token", methods=["GET"])
29
- def get_token():
30
- identity = f"user-{uuid.uuid4()}"
31
- token = (
32
- api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)
33
- .with_identity(identity)
34
- .with_name(f"Visitor-{identity}")
35
- .with_grants(api.VideoGrants(room_join=True, room=ROOM_NAME))
36
- )
37
- return jsonify({"token": token.to_jwt(), "livekitUrl": LIVEKIT_URL})
38
-
39
- def run_agent():
40
- """Run agent in background thread with its own event loop"""
41
- from livekit.agents import WorkerOptions, cli
42
- from app import entrypoint
43
-
44
- print("[AGENT] Starting worker...")
45
- worker_opts = WorkerOptions(
46
- entrypoint_fnc=entrypoint,
47
- api_key=LIVEKIT_API_KEY,
48
- api_secret=LIVEKIT_API_SECRET,
49
- ws_url=LIVEKIT_URL,
50
- )
51
- cli.run_app(worker_opts)
52
-
53
- if __name__ == "__main__":
54
- # Start agent in daemon thread
55
- agent_thread = threading.Thread(target=run_agent, daemon=True)
56
- agent_thread.start()
57
-
58
- # Start Flask
 
 
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)