index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import { IMAGES_API_BASE_URL } from '$lib/constants';
  2. export const getImageGenerationEnabledStatus = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${IMAGES_API_BASE_URL}/enabled`, {
  5. method: 'GET',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. ...(token && { authorization: `Bearer ${token}` })
  10. }
  11. })
  12. .then(async (res) => {
  13. if (!res.ok) throw await res.json();
  14. return res.json();
  15. })
  16. .catch((err) => {
  17. console.log(err);
  18. if ('detail' in err) {
  19. error = err.detail;
  20. } else {
  21. error = 'Server connection failed';
  22. }
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const toggleImageGenerationEnabledStatus = async (token: string = '') => {
  31. let error = null;
  32. const res = await fetch(`${IMAGES_API_BASE_URL}/enabled/toggle`, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. ...(token && { authorization: `Bearer ${token}` })
  38. }
  39. })
  40. .then(async (res) => {
  41. if (!res.ok) throw await res.json();
  42. return res.json();
  43. })
  44. .catch((err) => {
  45. console.log(err);
  46. if ('detail' in err) {
  47. error = err.detail;
  48. } else {
  49. error = 'Server connection failed';
  50. }
  51. return null;
  52. });
  53. if (error) {
  54. throw error;
  55. }
  56. return res;
  57. };
  58. export const getAUTOMATIC1111Url = async (token: string = '') => {
  59. let error = null;
  60. const res = await fetch(`${IMAGES_API_BASE_URL}/url`, {
  61. method: 'GET',
  62. headers: {
  63. Accept: 'application/json',
  64. 'Content-Type': 'application/json',
  65. ...(token && { authorization: `Bearer ${token}` })
  66. }
  67. })
  68. .then(async (res) => {
  69. if (!res.ok) throw await res.json();
  70. return res.json();
  71. })
  72. .catch((err) => {
  73. console.log(err);
  74. if ('detail' in err) {
  75. error = err.detail;
  76. } else {
  77. error = 'Server connection failed';
  78. }
  79. return null;
  80. });
  81. if (error) {
  82. throw error;
  83. }
  84. return res.AUTOMATIC1111_BASE_URL;
  85. };
  86. export const updateAUTOMATIC1111Url = async (token: string = '', url: string) => {
  87. let error = null;
  88. const res = await fetch(`${IMAGES_API_BASE_URL}/url/update`, {
  89. method: 'POST',
  90. headers: {
  91. Accept: 'application/json',
  92. 'Content-Type': 'application/json',
  93. ...(token && { authorization: `Bearer ${token}` })
  94. },
  95. body: JSON.stringify({
  96. url: url
  97. })
  98. })
  99. .then(async (res) => {
  100. if (!res.ok) throw await res.json();
  101. return res.json();
  102. })
  103. .catch((err) => {
  104. console.log(err);
  105. if ('detail' in err) {
  106. error = err.detail;
  107. } else {
  108. error = 'Server connection failed';
  109. }
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return res.AUTOMATIC1111_BASE_URL;
  116. };
  117. export const getImageSize = async (token: string = '') => {
  118. let error = null;
  119. const res = await fetch(`${IMAGES_API_BASE_URL}/size`, {
  120. method: 'GET',
  121. headers: {
  122. Accept: 'application/json',
  123. 'Content-Type': 'application/json',
  124. ...(token && { authorization: `Bearer ${token}` })
  125. }
  126. })
  127. .then(async (res) => {
  128. if (!res.ok) throw await res.json();
  129. return res.json();
  130. })
  131. .catch((err) => {
  132. console.log(err);
  133. if ('detail' in err) {
  134. error = err.detail;
  135. } else {
  136. error = 'Server connection failed';
  137. }
  138. return null;
  139. });
  140. if (error) {
  141. throw error;
  142. }
  143. return res.IMAGE_SIZE;
  144. };
  145. export const updateImageSize = async (token: string = '', size: string) => {
  146. let error = null;
  147. const res = await fetch(`${IMAGES_API_BASE_URL}/size/update`, {
  148. method: 'POST',
  149. headers: {
  150. Accept: 'application/json',
  151. 'Content-Type': 'application/json',
  152. ...(token && { authorization: `Bearer ${token}` })
  153. },
  154. body: JSON.stringify({
  155. size: size
  156. })
  157. })
  158. .then(async (res) => {
  159. if (!res.ok) throw await res.json();
  160. return res.json();
  161. })
  162. .catch((err) => {
  163. console.log(err);
  164. if ('detail' in err) {
  165. error = err.detail;
  166. } else {
  167. error = 'Server connection failed';
  168. }
  169. return null;
  170. });
  171. if (error) {
  172. throw error;
  173. }
  174. return res.IMAGE_SIZE;
  175. };
  176. export const getImageSteps = async (token: string = '') => {
  177. let error = null;
  178. const res = await fetch(`${IMAGES_API_BASE_URL}/steps`, {
  179. method: 'GET',
  180. headers: {
  181. Accept: 'application/json',
  182. 'Content-Type': 'application/json',
  183. ...(token && { authorization: `Bearer ${token}` })
  184. }
  185. })
  186. .then(async (res) => {
  187. if (!res.ok) throw await res.json();
  188. return res.json();
  189. })
  190. .catch((err) => {
  191. console.log(err);
  192. if ('detail' in err) {
  193. error = err.detail;
  194. } else {
  195. error = 'Server connection failed';
  196. }
  197. return null;
  198. });
  199. if (error) {
  200. throw error;
  201. }
  202. return res.IMAGE_STEPS;
  203. };
  204. export const updateImageSteps = async (token: string = '', steps: number) => {
  205. let error = null;
  206. const res = await fetch(`${IMAGES_API_BASE_URL}/steps/update`, {
  207. method: 'POST',
  208. headers: {
  209. Accept: 'application/json',
  210. 'Content-Type': 'application/json',
  211. ...(token && { authorization: `Bearer ${token}` })
  212. },
  213. body: JSON.stringify({ steps })
  214. })
  215. .then(async (res) => {
  216. if (!res.ok) throw await res.json();
  217. return res.json();
  218. })
  219. .catch((err) => {
  220. console.log(err);
  221. if ('detail' in err) {
  222. error = err.detail;
  223. } else {
  224. error = 'Server connection failed';
  225. }
  226. return null;
  227. });
  228. if (error) {
  229. throw error;
  230. }
  231. return res.IMAGE_STEPS;
  232. };
  233. export const getDiffusionModels = async (token: string = '') => {
  234. let error = null;
  235. const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
  236. method: 'GET',
  237. headers: {
  238. Accept: 'application/json',
  239. 'Content-Type': 'application/json',
  240. ...(token && { authorization: `Bearer ${token}` })
  241. }
  242. })
  243. .then(async (res) => {
  244. if (!res.ok) throw await res.json();
  245. return res.json();
  246. })
  247. .catch((err) => {
  248. console.log(err);
  249. if ('detail' in err) {
  250. error = err.detail;
  251. } else {
  252. error = 'Server connection failed';
  253. }
  254. return null;
  255. });
  256. if (error) {
  257. throw error;
  258. }
  259. return res;
  260. };
  261. export const getDefaultDiffusionModel = async (token: string = '') => {
  262. let error = null;
  263. const res = await fetch(`${IMAGES_API_BASE_URL}/models/default`, {
  264. method: 'GET',
  265. headers: {
  266. Accept: 'application/json',
  267. 'Content-Type': 'application/json',
  268. ...(token && { authorization: `Bearer ${token}` })
  269. }
  270. })
  271. .then(async (res) => {
  272. if (!res.ok) throw await res.json();
  273. return res.json();
  274. })
  275. .catch((err) => {
  276. console.log(err);
  277. if ('detail' in err) {
  278. error = err.detail;
  279. } else {
  280. error = 'Server connection failed';
  281. }
  282. return null;
  283. });
  284. if (error) {
  285. throw error;
  286. }
  287. return res.model;
  288. };
  289. export const updateDefaultDiffusionModel = async (token: string = '', model: string) => {
  290. let error = null;
  291. const res = await fetch(`${IMAGES_API_BASE_URL}/models/default/update`, {
  292. method: 'POST',
  293. headers: {
  294. Accept: 'application/json',
  295. 'Content-Type': 'application/json',
  296. ...(token && { authorization: `Bearer ${token}` })
  297. },
  298. body: JSON.stringify({
  299. model: model
  300. })
  301. })
  302. .then(async (res) => {
  303. if (!res.ok) throw await res.json();
  304. return res.json();
  305. })
  306. .catch((err) => {
  307. console.log(err);
  308. if ('detail' in err) {
  309. error = err.detail;
  310. } else {
  311. error = 'Server connection failed';
  312. }
  313. return null;
  314. });
  315. if (error) {
  316. throw error;
  317. }
  318. return res.model;
  319. };
  320. export const imageGenerations = async (token: string = '', prompt: string) => {
  321. let error = null;
  322. const res = await fetch(`${IMAGES_API_BASE_URL}/generations`, {
  323. method: 'POST',
  324. headers: {
  325. Accept: 'application/json',
  326. 'Content-Type': 'application/json',
  327. ...(token && { authorization: `Bearer ${token}` })
  328. },
  329. body: JSON.stringify({
  330. prompt: prompt
  331. })
  332. })
  333. .then(async (res) => {
  334. if (!res.ok) throw await res.json();
  335. return res.json();
  336. })
  337. .catch((err) => {
  338. console.log(err);
  339. if ('detail' in err) {
  340. error = err.detail;
  341. } else {
  342. error = 'Server connection failed';
  343. }
  344. return null;
  345. });
  346. if (error) {
  347. throw error;
  348. }
  349. return res;
  350. };