P01yH3dr0n commited on
Commit
2af6387
1 Parent(s): 6a54251

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +6 -62
utils.py CHANGED
@@ -1,83 +1,29 @@
1
  import random
2
  import io
3
  import zipfile
 
4
 
5
  from PIL import Image
6
- from httpx import AsyncClient
7
 
8
 
9
  jwt_token = ''
10
  url = "https://api.novelai.net/ai/generate-image"
11
- global_client = AsyncClient(timeout=100)
12
 
13
 
14
  def set_token(token):
15
- global jwt_token, global_client
16
  if jwt_token == token:
17
  return
18
  jwt_token = token
19
- global_client = AsyncClient(
20
- timeout=100,
21
- headers = {
22
  "Authorization": f"Bearer {jwt_token}",
23
  "Content-Type": "application/json",
24
  "Origin": "https://novelai.net",
25
  "Referer": "https://novelai.net/"
26
  }
27
- )
28
 
29
-
30
- async def remote_login(end_point, password):
31
- payload = {"password": password}
32
- response = await global_client.post(f'{end_point}/login', params=payload)
33
- if response.status_code == 200:
34
- return response.json()["status"]
35
- else:
36
- return None
37
-
38
-
39
- async def remote_gen(
40
- end_point="http://127.0.0.1:7000",
41
- input_text="",
42
- quality_tags="",
43
- negative_prompt="",
44
- seed=-1,
45
- scale=5.0,
46
- width=1024,
47
- height=1024,
48
- steps=28,
49
- sampler="k_euler",
50
- schedule='native',
51
- smea=False,
52
- dyn=False,
53
- dyn_threshold=False,
54
- chg_rescale=0,
55
- ):
56
- payload = {
57
- "prompt": f'{input_text}, {quality_tags}',
58
- "neg_prompt": negative_prompt,
59
- "seed": seed,
60
- "scale": scale,
61
- "width": width,
62
- "height": height,
63
- "steps": steps,
64
- "sampler": sampler,
65
- "schedule": schedule,
66
- "smea": smea,
67
- "dyn": dyn,
68
- "dyn_threshold": dyn_threshold,
69
- "chg_rescale": chg_rescale,
70
- }
71
- response = await global_client.post(f'{end_point}/gen', json=payload)
72
- if response.status_code == 200:
73
- mem_file = io.BytesIO(response.content)
74
- mem_file.seek(0)
75
- return Image.open(mem_file)
76
- else:
77
- return None
78
-
79
-
80
- async def generate_novelai_image(
81
  input_text="",
82
  negative_prompt="",
83
  seed=-1,
@@ -125,7 +71,7 @@ async def generate_novelai_image(
125
  }
126
 
127
  # Send the POST request
128
- response = await global_client.post(url, json=payload)
129
 
130
  # Process the response
131
  if response.headers.get('Content-Type') == 'application/x-zip-compressed':
@@ -141,8 +87,6 @@ async def generate_novelai_image(
141
  return "Generation failed", response
142
 
143
 
144
- def free_check(width, height, steps):
145
- return width * height <= 1024 * 1024 and steps<=28
146
 
147
 
148
  def image_from_bytes(data):
 
1
  import random
2
  import io
3
  import zipfile
4
+ import requests
5
 
6
  from PIL import Image
 
7
 
8
 
9
  jwt_token = ''
10
  url = "https://api.novelai.net/ai/generate-image"
11
+ headers = {}
12
 
13
 
14
  def set_token(token):
15
+ global jwt_token, headers
16
  if jwt_token == token:
17
  return
18
  jwt_token = token
19
+ headers = {
 
 
20
  "Authorization": f"Bearer {jwt_token}",
21
  "Content-Type": "application/json",
22
  "Origin": "https://novelai.net",
23
  "Referer": "https://novelai.net/"
24
  }
 
25
 
26
+ def generate_novelai_image(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  input_text="",
28
  negative_prompt="",
29
  seed=-1,
 
71
  }
72
 
73
  # Send the POST request
74
+ response = requests.post(url, json=payload, headers=headers)
75
 
76
  # Process the response
77
  if response.headers.get('Content-Type') == 'application/x-zip-compressed':
 
87
  return "Generation failed", response
88
 
89
 
 
 
90
 
91
 
92
  def image_from_bytes(data):