-
Notifications
You must be signed in to change notification settings - Fork 24
/
showcase.py
121 lines (99 loc) · 3.53 KB
/
showcase.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import asyncio
import json
import os
import urllib.error
import urllib.request
import aiohttp
import server
from aiohttp import web
BIZYAIR_DEBUG = os.getenv("BIZYAIR_DEBUG", False)
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
SHOW_CASES = {}
async def get_bizyair_news(base_url="https://bizyair.siliconflow.cn"):
url = f"{base_url}/bznews.json"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=2) as response:
if response.status == 200:
data = await response.text()
return json.loads(data)
else:
print(f"Failed to fetch bznews.json: HTTP Status {response.status}")
return {}
except aiohttp.ClientError as e:
print(f"Error fetching bznews.json: {e}")
return {}
except asyncio.exceptions.TimeoutError as e:
print(f"Request bizyair news timed out: {e}")
return {}
except Exception as e:
print(f"Error fetching BizyAir bznews.json: {type(e).__name__} - {str(e)}")
return {}
with open(os.path.join(CURRENT_DIR, "bizyair_example_menu.json"), "r") as file:
SHOW_CASES.update(json.load(file))
def extract_files(data):
file_whitelist = []
for key, value in data.items():
if isinstance(value, dict):
file_whitelist.extend(extract_files(value))
else:
file_whitelist.append(value)
return file_whitelist
file_whitelist = extract_files(SHOW_CASES)
from server import PromptServer
@PromptServer.instance.routes.get("/bizyair/showcases")
async def set_api_key_page(request):
return web.Response(
text=json.dumps(SHOW_CASES, ensure_ascii=False), content_type="application/json"
)
@PromptServer.instance.routes.get("/bizyair/news")
async def list_news(request):
return web.Response(
text=json.dumps(await get_bizyair_news(), ensure_ascii=False),
content_type="application/json",
)
@PromptServer.instance.routes.post("/bizyair/workflow")
async def get_file_content(request):
try:
data = await request.json()
except json.JSONDecodeError:
return web.Response(
text=json.dumps({"error": "Invalid JSON body"}),
status=400,
content_type="application/json",
)
filename = data.get("file")
if not filename:
return web.Response(
text=json.dumps({"error": "Missing file parameter"}),
status=400,
content_type="application/json",
)
if filename not in file_whitelist:
return web.Response(
text=json.dumps({"error": "Filename not allowed"}),
status=400,
content_type="application/json",
)
file_path = os.path.join(CURRENT_DIR, "examples", filename)
if BIZYAIR_DEBUG:
print(f"request the json workflow: {file_path}")
if not os.path.isfile(file_path):
return web.Response(
text=json.dumps({"error": "File not found"}),
status=404,
content_type="application/json",
)
try:
with open(file_path, "r", encoding="utf-8") as file:
file_content = json.load(file)
return web.Response(
text=json.dumps(file_content, ensure_ascii=False),
content_type="application/json",
)
except Exception as e:
return web.Response(
text=json.dumps({"error": str(e)}),
status=500,
content_type="application/json",
)