Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
5 |
-
@app.get('/
|
6 |
-
async def
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
"
|
13 |
-
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
from livekit import api
|
5 |
+
from fastapi.responses import JSONResponse
|
6 |
from fastapi import FastAPI, Request
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
LIVEKIT_API_KEY_LOCAL = os.getenv('LIVEKIT_API_KEY_LOCAL')
|
11 |
+
LIVEKIT_API_SECRET_LOCAL = os.getenv('LIVEKIT_API_SECRET_LOCAL')
|
12 |
+
LIVEKIT_WEBSCOKET_URL_LOCAL = os.getenv('LIVEKIT_WEBSCOKET_URL_LOCAL')
|
13 |
+
|
14 |
+
LIVEKIT_API_KEY_STAGE = os.getenv('LIVEKIT_API_KEY_STAGE')
|
15 |
+
LIVEKIT_API_SECRET_STAGE = os.getenv('LIVEKIT_API_SECRET_STAGE')
|
16 |
+
LIVEKIT_WEBSCOKET_URL_STAGE = os.getenv('LIVEKIT_WEBSCOKET_URL_STAGE')
|
17 |
+
|
18 |
+
LIVEKIT_API_KEY_PROD = os.getenv('LIVEKIT_API_KEY_PROD')
|
19 |
+
LIVEKIT_API_SECRET_PROD = os.getenv('LIVEKIT_API_SECRET_PROD')
|
20 |
+
LIVEKIT_WEBSCOKET_URL_PROD = os.getenv('LIVEKIT_WEBSCOKET_URL_PROD')
|
21 |
+
|
22 |
+
LIVEKIT_API_KEY_SUWARNA = os.getenv('LIVEKIT_API_KEY_SUWARNA')
|
23 |
+
LIVEKIT_API_SECRET_SUWARNA = os.getenv('LIVEKIT_API_SECRET_SUWARNA')
|
24 |
+
LIVEKIT_WEBSCOKET_URL_SUWARNA = os.getenv('LIVEKIT_URL_SUWARNA')
|
25 |
|
26 |
app = FastAPI()
|
27 |
|
28 |
+
@app.get('/generate_token_local')
|
29 |
+
async def generate_token(request: Request):
|
30 |
+
agent_token = request.query_params.get("agent_token")
|
31 |
+
web_uuid = request.query_params.get("web_uuid")
|
32 |
+
|
33 |
+
room_name = 'local-'+''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=5))
|
34 |
+
if not LIVEKIT_API_KEY_LOCAL or not LIVEKIT_API_SECRET_LOCAL:
|
35 |
+
raise ValueError("LIVEKIT_API_KEY_LOCAL and LIVEKIT_API_SECRET_LOCAL must be set")
|
36 |
+
metadata = json.dumps({
|
37 |
+
"agent_token": agent_token,
|
38 |
+
"web_uuid": web_uuid
|
39 |
+
})
|
40 |
+
identity = "human_local"
|
41 |
+
name = "kickcall_local_name"
|
42 |
+
at = api.AccessToken(LIVEKIT_API_KEY_LOCAL, LIVEKIT_API_SECRET_LOCAL).with_identity(identity).with_name(name).with_metadata(metadata)
|
43 |
+
at.with_grants(api.VideoGrants(room=room_name, room_join=True, can_publish=True,
|
44 |
+
can_publish_data=True, can_subscribe=True, can_update_own_metadata=True))
|
45 |
+
|
46 |
+
access_token = at.to_jwt()
|
47 |
+
return JSONResponse(content={
|
48 |
+
"accessToken": access_token,
|
49 |
+
"url": LIVEKIT_WEBSCOKET_URL_LOCAL,
|
50 |
+
})
|
51 |
+
|
52 |
+
|
53 |
+
@app.get('/generate_token_stage')
|
54 |
+
async def generate_token(request: Request):
|
55 |
+
agent_token = request.query_params.get("agent_token")
|
56 |
+
web_uuid = request.query_params.get("web_uuid")
|
57 |
+
|
58 |
+
room_name = 'stage-'+''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=7))
|
59 |
+
if not LIVEKIT_API_KEY_STAGE or not LIVEKIT_API_SECRET_STAGE:
|
60 |
+
raise ValueError("LIVEKIT_API_KEY_STAGE and LIVEKIT_API_SECRET_STAGE must be set")
|
61 |
+
metadata = json.dumps({
|
62 |
+
"agent_token": agent_token,
|
63 |
+
"web_uuid": web_uuid
|
64 |
+
})
|
65 |
+
identity = "human_stage"
|
66 |
+
name = "kickcall_stage_name"
|
67 |
+
at = api.AccessToken(LIVEKIT_API_KEY_STAGE, LIVEKIT_API_SECRET_STAGE).with_identity(identity).with_name(name).with_metadata(metadata)
|
68 |
+
at.with_grants(api.VideoGrants(room=room_name, room_join=True, can_publish=True,
|
69 |
+
can_publish_data=True, can_subscribe=True, can_update_own_metadata=True))
|
70 |
+
|
71 |
+
access_token = at.to_jwt()
|
72 |
+
return JSONResponse(content={
|
73 |
+
"accessToken": access_token,
|
74 |
+
"url": LIVEKIT_WEBSCOKET_URL_STAGE,
|
75 |
+
})
|
76 |
+
|
77 |
+
|
78 |
+
@app.get('/generate_token_prod')
|
79 |
+
async def generate_token(request: Request):
|
80 |
+
agent_token = request.query_params.get("agent_token")
|
81 |
+
web_uuid = request.query_params.get("web_uuid")
|
82 |
+
|
83 |
+
room_name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=7))
|
84 |
+
if not LIVEKIT_API_KEY_PROD or not LIVEKIT_API_SECRET_PROD:
|
85 |
+
raise ValueError("LIVEKIT_API_KEY_PROD and LIVEKIT_API_SECRET_PROD must be set")
|
86 |
+
metadata = json.dumps({
|
87 |
+
"agent_token": agent_token,
|
88 |
+
"web_uuid": web_uuid
|
89 |
+
})
|
90 |
+
identity = "human_prod"
|
91 |
+
name = "kickcall_prod_name"
|
92 |
+
at = api.AccessToken(LIVEKIT_API_KEY_PROD, LIVEKIT_API_SECRET_PROD).with_identity(identity).with_name(name).with_metadata(metadata)
|
93 |
+
at.with_grants(api.VideoGrants(room=room_name, room_join=True, can_publish=True,
|
94 |
+
can_publish_data=True, can_subscribe=True, can_update_own_metadata=True))
|
95 |
+
|
96 |
+
access_token = at.to_jwt()
|
97 |
+
return JSONResponse(content={
|
98 |
+
"accessToken": access_token,
|
99 |
+
"url": LIVEKIT_WEBSCOKET_URL_PROD,
|
100 |
+
})
|
101 |
+
|
102 |
+
|
103 |
+
@app.get('/generate_token_suwarna')
|
104 |
+
async def generate_token(request: Request):
|
105 |
+
agent_token = request.query_params.get("agent_token")
|
106 |
+
web_uuid = request.query_params.get("web_uuid")
|
107 |
+
|
108 |
+
room_name = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=7))
|
109 |
+
if not LIVEKIT_API_KEY_SUWARNA or not LIVEKIT_API_SECRET_SUWARNA:
|
110 |
+
raise ValueError("LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set")
|
111 |
+
|
112 |
+
metadata = json.dumps({
|
113 |
+
"agent_token": agent_token,
|
114 |
+
"web_uuid": web_uuid
|
115 |
+
})
|
116 |
+
|
117 |
+
identity = "human_suwarna"
|
118 |
+
name = "suwarna"
|
119 |
+
at = api.AccessToken(LIVEKIT_API_KEY_SUWARNA, LIVEKIT_API_SECRET_SUWARNA).with_identity(identity).with_name(name).with_metadata(metadata)
|
120 |
+
at.with_grants(api.VideoGrants(room=room_name, room_join=True, can_publish=True,
|
121 |
+
can_publish_data=True, can_subscribe=True, can_update_own_metadata=True))
|
122 |
+
|
123 |
|
124 |
+
access_token = at.to_jwt()
|
125 |
+
return JSONResponse(content={
|
126 |
+
"accessToken": access_token,
|
127 |
+
"url": LIVEKIT_WEBSCOKET_URL_SUWARNA,
|
128 |
+
})
|
129 |
+
|