Spaces:
Sleeping
Sleeping
Jon Taylor
commited on
Commit
•
a3e20c4
1
Parent(s):
82d5975
added bot logic back
Browse files- Dockerfile +1 -1
- app/auth.py +2 -2
- frontend/app/page.js +23 -2
- frontend/components/Button.js +3 -0
- requirements.txt +2 -1
- server.py +77 -2
Dockerfile
CHANGED
@@ -37,7 +37,7 @@ RUN cd frontend && npm install
|
|
37 |
|
38 |
# Copy frontend app and install Node.js dependencies
|
39 |
COPY frontend/ frontend/
|
40 |
-
RUN cd frontend && npm
|
41 |
|
42 |
# Copy everything else
|
43 |
COPY app/ app/
|
|
|
37 |
|
38 |
# Copy frontend app and install Node.js dependencies
|
39 |
COPY frontend/ frontend/
|
40 |
+
RUN cd frontend && npm run build
|
41 |
|
42 |
# Copy everything else
|
43 |
COPY app/ app/
|
app/auth.py
CHANGED
@@ -3,7 +3,7 @@ import urllib
|
|
3 |
|
4 |
from dotenv import load_dotenv
|
5 |
import requests
|
6 |
-
|
7 |
import os
|
8 |
|
9 |
load_dotenv()
|
@@ -17,7 +17,7 @@ def get_meeting_token(room_name, daily_api_key, token_expiry):
|
|
17 |
headers={'Authorization': f'Bearer {daily_api_key}'},
|
18 |
json={'properties': {'room_name': room_name, 'is_owner': True, 'exp': token_expiry}})
|
19 |
if res.status_code != 200:
|
20 |
-
return
|
21 |
meeting_token = res.json()['token']
|
22 |
return meeting_token
|
23 |
|
|
|
3 |
|
4 |
from dotenv import load_dotenv
|
5 |
import requests
|
6 |
+
import json
|
7 |
import os
|
8 |
|
9 |
load_dotenv()
|
|
|
17 |
headers={'Authorization': f'Bearer {daily_api_key}'},
|
18 |
json={'properties': {'room_name': room_name, 'is_owner': True, 'exp': token_expiry}})
|
19 |
if res.status_code != 200:
|
20 |
+
return json.dumps({'error': 'Unable to create meeting token', 'detail': res.text}), 500
|
21 |
meeting_token = res.json()['token']
|
22 |
return meeting_token
|
23 |
|
frontend/app/page.js
CHANGED
@@ -1,11 +1,32 @@
|
|
|
|
|
|
1 |
import Link from "next/link";
|
2 |
|
3 |
export default function Home() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
return (
|
5 |
<main className="flex min-h-screen flex-col items-center justify-between p-24">
|
6 |
-
Hello
|
7 |
-
<hr />
|
8 |
<Link href="/test">Test</Link>
|
|
|
|
|
9 |
</main>
|
10 |
);
|
11 |
}
|
|
|
1 |
+
"use client";
|
2 |
+
|
3 |
import Link from "next/link";
|
4 |
|
5 |
export default function Home() {
|
6 |
+
async function start() {
|
7 |
+
const resp = await fetch(
|
8 |
+
`${process.env.API_URL || "http://localhost:8000"}/start`,
|
9 |
+
{
|
10 |
+
method: "POST",
|
11 |
+
mode: "cors",
|
12 |
+
cache: "no-cache",
|
13 |
+
credentials: "same-origin",
|
14 |
+
headers: {
|
15 |
+
"Content-Type": "application/json",
|
16 |
+
},
|
17 |
+
body: JSON.stringify({}),
|
18 |
+
}
|
19 |
+
);
|
20 |
+
|
21 |
+
const data = await resp.json();
|
22 |
+
console.log(data);
|
23 |
+
}
|
24 |
+
|
25 |
return (
|
26 |
<main className="flex min-h-screen flex-col items-center justify-between p-24">
|
|
|
|
|
27 |
<Link href="/test">Test</Link>
|
28 |
+
<hr />
|
29 |
+
<span onClick={() => start()}>Connect</span>
|
30 |
</main>
|
31 |
);
|
32 |
}
|
frontend/components/Button.js
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
export default function Button({ children }) {
|
2 |
+
return <button>{children}</button>;
|
3 |
+
}
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ imageio-ffmpeg
|
|
4 |
imageio
|
5 |
requests
|
6 |
fastapi
|
7 |
-
uvicorn[standard]
|
|
|
|
4 |
imageio
|
5 |
requests
|
6 |
fastapi
|
7 |
+
uvicorn[standard]
|
8 |
+
requests
|
server.py
CHANGED
@@ -1,10 +1,15 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
-
from fastapi.responses import FileResponse
|
5 |
from dotenv import load_dotenv
|
6 |
from pathlib import Path
|
7 |
import os
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
load_dotenv()
|
10 |
|
@@ -21,6 +26,76 @@ app.add_middleware(
|
|
21 |
# Mount the static directory
|
22 |
app.mount("/static", StaticFiles(directory="frontend/out", html=True), name="static")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
@app.get("/{path_name:path}", response_class=FileResponse)
|
25 |
async def catch_all(path_name: str):
|
26 |
if path_name == "":
|
@@ -35,7 +110,7 @@ async def catch_all(path_name: str):
|
|
35 |
if html_file_path.is_file():
|
36 |
return FileResponse(html_file_path)
|
37 |
|
38 |
-
raise HTTPException(status_code=
|
39 |
|
40 |
# Run the application using Uvicorn
|
41 |
if __name__ == "__main__":
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.responses import FileResponse, JSONResponse
|
5 |
from dotenv import load_dotenv
|
6 |
from pathlib import Path
|
7 |
import os
|
8 |
+
import requests
|
9 |
+
import subprocess
|
10 |
+
import time
|
11 |
+
|
12 |
+
from app.auth import get_meeting_token
|
13 |
|
14 |
load_dotenv()
|
15 |
|
|
|
26 |
# Mount the static directory
|
27 |
app.mount("/static", StaticFiles(directory="frontend/out", html=True), name="static")
|
28 |
|
29 |
+
def _start_bot(bot_path, args=None):
|
30 |
+
daily_api_key = os.getenv("DAILY_API_KEY") or ""
|
31 |
+
api_path = os.getenv("DAILY_API_PATH") or "https://api.daily.co/v1"
|
32 |
+
|
33 |
+
timeout = int(os.getenv("ROOM_TIMEOUT") or os.getenv("BOT_MAX_DURATION") or 300)
|
34 |
+
exp = time.time() + timeout
|
35 |
+
|
36 |
+
'''
|
37 |
+
res = requests.post(
|
38 |
+
f"{api_path}/rooms",
|
39 |
+
headers={"Authorization": f"Bearer {daily_api_key}"},
|
40 |
+
json={
|
41 |
+
"properties": {
|
42 |
+
"exp": exp,
|
43 |
+
"enable_chat": True,
|
44 |
+
"enable_emoji_reactions": True,
|
45 |
+
"eject_at_room_exp": True,
|
46 |
+
"enable_prejoin_ui": False,
|
47 |
+
}
|
48 |
+
},
|
49 |
+
)
|
50 |
+
if res.status_code != 200:
|
51 |
+
return (
|
52 |
+
jsonify(
|
53 |
+
{
|
54 |
+
"error": "Unable to create room",
|
55 |
+
"status_code": res.status_code,
|
56 |
+
"text": res.text,
|
57 |
+
}
|
58 |
+
),
|
59 |
+
500,
|
60 |
+
)
|
61 |
+
'''
|
62 |
+
room_url = os.getenv("DAILY_ROOM_URL") #res.json()["url"]
|
63 |
+
room_name = os.getenv("DAILY_ROOM_NAME") #res.json()["name"]
|
64 |
+
|
65 |
+
meeting_token = get_meeting_token(room_url, daily_api_key, exp)
|
66 |
+
|
67 |
+
if args:
|
68 |
+
extra_args = " ".join([f'-{x[0]} "{x[1]}"' for x in args])
|
69 |
+
else:
|
70 |
+
extra_args = ""
|
71 |
+
|
72 |
+
proc = subprocess.Popen(
|
73 |
+
[
|
74 |
+
f"python3 {bot_path} -u {room_url} -t {meeting_token} {extra_args}"
|
75 |
+
],
|
76 |
+
shell=True,
|
77 |
+
bufsize=1,
|
78 |
+
)
|
79 |
+
|
80 |
+
# Don't return until the bot has joined the room, but wait for at most 2 seconds.
|
81 |
+
attempts = 0
|
82 |
+
while attempts < 20:
|
83 |
+
time.sleep(0.1)
|
84 |
+
attempts += 1
|
85 |
+
res = requests.get(
|
86 |
+
f"{api_path}/rooms/{room_name}/get-session-data",
|
87 |
+
headers={"Authorization": f"Bearer {daily_api_key}"},
|
88 |
+
)
|
89 |
+
if res.status_code == 200:
|
90 |
+
break
|
91 |
+
print(f"Took {attempts} attempts to join room {room_name}")
|
92 |
+
|
93 |
+
return JSONResponse({"room_url": room_url, "token": meeting_token})
|
94 |
+
|
95 |
+
@app.post("/start")
|
96 |
+
async def start():
|
97 |
+
return _start_bot("./app/bot.py")
|
98 |
+
|
99 |
@app.get("/{path_name:path}", response_class=FileResponse)
|
100 |
async def catch_all(path_name: str):
|
101 |
if path_name == "":
|
|
|
110 |
if html_file_path.is_file():
|
111 |
return FileResponse(html_file_path)
|
112 |
|
113 |
+
raise HTTPException(status_code=450, detail="Incorrect API call")
|
114 |
|
115 |
# Run the application using Uvicorn
|
116 |
if __name__ == "__main__":
|