forked from 101dotxyz/GPTeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (68 loc) · 2.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import asyncio
import os
import subprocess
import traceback
import webbrowser
from multiprocessing import Process
from time import sleep
import openai
from dotenv import load_dotenv
from src.utils.database.client import get_database
from src.utils.discord import discord_listener
from src.world.base import World
from .utils.colors import LogColor
from .utils.database.base import Tables
from .utils.formatting import print_to_console
from .utils.logging import init_logging
from .utils.parameters import DISCORD_ENABLED
from .web import get_server
from .utils.general import get_open_port
load_dotenv()
init_logging()
async def run_world_async():
openai.api_key = os.getenv("OPENAI_API_KEY")
try:
database = await get_database()
worlds = await database.get_all(Tables.Worlds)
if len(worlds) == 0:
raise ValueError("No worlds found!")
world = await World.from_id(worlds[-1]["id"])
print_to_console(
f"Welcome to {world.name}!",
LogColor.ANNOUNCEMENT,
"\n",
)
await world.run()
except Exception:
print(traceback.format_exc())
finally:
await (await get_database()).close()
def run_world():
run_in_new_loop(run_world_async())
def run_server():
app = get_server()
port = get_open_port()
run_in_new_loop(app.run_task(port=port))
def run_in_new_loop(coro):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(coro)
finally:
loop.close()
def run():
port = get_open_port()
process_discord = Process(target=discord_listener)
process_world = Process(target=run_world)
process_server = Process(target=run_server)
process_discord.start()
process_world.start()
process_server.start()
sleep(3)
print(f"Opening browser on port {port}...")
webbrowser.open(f"http://127.0.0.1:{port}")
process_discord.join()
process_world.join()
process_server.join()
def main():
run()