index.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import { IMAGES_API_BASE_URL } from '$lib/constants';
  2. export const getImageGenerationConfig = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${IMAGES_API_BASE_URL}/config`, {
  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 updateImageGenerationConfig = async (
  31. token: string = '',
  32. engine: string,
  33. enabled: boolean
  34. ) => {
  35. let error = null;
  36. const res = await fetch(`${IMAGES_API_BASE_URL}/config/update`, {
  37. method: 'POST',
  38. headers: {
  39. Accept: 'application/json',
  40. 'Content-Type': 'application/json',
  41. ...(token && { authorization: `Bearer ${token}` })
  42. },
  43. body: JSON.stringify({
  44. engine,
  45. enabled
  46. })
  47. })
  48. .then(async (res) => {
  49. if (!res.ok) throw await res.json();
  50. return res.json();
  51. })
  52. .catch((err) => {
  53. console.log(err);
  54. if ('detail' in err) {
  55. error = err.detail;
  56. } else {
  57. error = 'Server connection failed';
  58. }
  59. return null;
  60. });
  61. if (error) {
  62. throw error;
  63. }
  64. return res;
  65. };
  66. export const getOpenAIKey = async (token: string = '') => {
  67. let error = null;
  68. const res = await fetch(`${IMAGES_API_BASE_URL}/key`, {
  69. method: 'GET',
  70. headers: {
  71. Accept: 'application/json',
  72. 'Content-Type': 'application/json',
  73. ...(token && { authorization: `Bearer ${token}` })
  74. }
  75. })
  76. .then(async (res) => {
  77. if (!res.ok) throw await res.json();
  78. return res.json();
  79. })
  80. .catch((err) => {
  81. console.log(err);
  82. if ('detail' in err) {
  83. error = err.detail;
  84. } else {
  85. error = 'Server connection failed';
  86. }
  87. return null;
  88. });
  89. if (error) {
  90. throw error;
  91. }
  92. return res.OPENAI_API_KEY;
  93. };
  94. export const updateOpenAIKey = async (token: string = '', key: string) => {
  95. let error = null;
  96. const res = await fetch(`${IMAGES_API_BASE_URL}/key/update`, {
  97. method: 'POST',
  98. headers: {
  99. Accept: 'application/json',
  100. 'Content-Type': 'application/json',
  101. ...(token && { authorization: `Bearer ${token}` })
  102. },
  103. body: JSON.stringify({
  104. key: key
  105. })
  106. })
  107. .then(async (res) => {
  108. if (!res.ok) throw await res.json();
  109. return res.json();
  110. })
  111. .catch((err) => {
  112. console.log(err);
  113. if ('detail' in err) {
  114. error = err.detail;
  115. } else {
  116. error = 'Server connection failed';
  117. }
  118. return null;
  119. });
  120. if (error) {
  121. throw error;
  122. }
  123. return res.OPENAI_API_KEY;
  124. };
  125. export const getAUTOMATIC1111Url = async (token: string = '') => {
  126. let error = null;
  127. const res = await fetch(`${IMAGES_API_BASE_URL}/url`, {
  128. method: 'GET',
  129. headers: {
  130. Accept: 'application/json',
  131. 'Content-Type': 'application/json',
  132. ...(token && { authorization: `Bearer ${token}` })
  133. }
  134. })
  135. .then(async (res) => {
  136. if (!res.ok) throw await res.json();
  137. return res.json();
  138. })
  139. .catch((err) => {
  140. console.log(err);
  141. if ('detail' in err) {
  142. error = err.detail;
  143. } else {
  144. error = 'Server connection failed';
  145. }
  146. return null;
  147. });
  148. if (error) {
  149. throw error;
  150. }
  151. return res.AUTOMATIC1111_BASE_URL;
  152. };
  153. export const updateAUTOMATIC1111Url = async (token: string = '', url: string) => {
  154. let error = null;
  155. const res = await fetch(`${IMAGES_API_BASE_URL}/url/update`, {
  156. method: 'POST',
  157. headers: {
  158. Accept: 'application/json',
  159. 'Content-Type': 'application/json',
  160. ...(token && { authorization: `Bearer ${token}` })
  161. },
  162. body: JSON.stringify({
  163. url: url
  164. })
  165. })
  166. .then(async (res) => {
  167. if (!res.ok) throw await res.json();
  168. return res.json();
  169. })
  170. .catch((err) => {
  171. console.log(err);
  172. if ('detail' in err) {
  173. error = err.detail;
  174. } else {
  175. error = 'Server connection failed';
  176. }
  177. return null;
  178. });
  179. if (error) {
  180. throw error;
  181. }
  182. return res.AUTOMATIC1111_BASE_URL;
  183. };
  184. export const getImageSize = async (token: string = '') => {
  185. let error = null;
  186. const res = await fetch(`${IMAGES_API_BASE_URL}/size`, {
  187. method: 'GET',
  188. headers: {
  189. Accept: 'application/json',
  190. 'Content-Type': 'application/json',
  191. ...(token && { authorization: `Bearer ${token}` })
  192. }
  193. })
  194. .then(async (res) => {
  195. if (!res.ok) throw await res.json();
  196. return res.json();
  197. })
  198. .catch((err) => {
  199. console.log(err);
  200. if ('detail' in err) {
  201. error = err.detail;
  202. } else {
  203. error = 'Server connection failed';
  204. }
  205. return null;
  206. });
  207. if (error) {
  208. throw error;
  209. }
  210. return res.IMAGE_SIZE;
  211. };
  212. export const updateImageSize = async (token: string = '', size: string) => {
  213. let error = null;
  214. const res = await fetch(`${IMAGES_API_BASE_URL}/size/update`, {
  215. method: 'POST',
  216. headers: {
  217. Accept: 'application/json',
  218. 'Content-Type': 'application/json',
  219. ...(token && { authorization: `Bearer ${token}` })
  220. },
  221. body: JSON.stringify({
  222. size: size
  223. })
  224. })
  225. .then(async (res) => {
  226. if (!res.ok) throw await res.json();
  227. return res.json();
  228. })
  229. .catch((err) => {
  230. console.log(err);
  231. if ('detail' in err) {
  232. error = err.detail;
  233. } else {
  234. error = 'Server connection failed';
  235. }
  236. return null;
  237. });
  238. if (error) {
  239. throw error;
  240. }
  241. return res.IMAGE_SIZE;
  242. };
  243. export const getImageSteps = async (token: string = '') => {
  244. let error = null;
  245. const res = await fetch(`${IMAGES_API_BASE_URL}/steps`, {
  246. method: 'GET',
  247. headers: {
  248. Accept: 'application/json',
  249. 'Content-Type': 'application/json',
  250. ...(token && { authorization: `Bearer ${token}` })
  251. }
  252. })
  253. .then(async (res) => {
  254. if (!res.ok) throw await res.json();
  255. return res.json();
  256. })
  257. .catch((err) => {
  258. console.log(err);
  259. if ('detail' in err) {
  260. error = err.detail;
  261. } else {
  262. error = 'Server connection failed';
  263. }
  264. return null;
  265. });
  266. if (error) {
  267. throw error;
  268. }
  269. return res.IMAGE_STEPS;
  270. };
  271. export const updateImageSteps = async (token: string = '', steps: number) => {
  272. let error = null;
  273. const res = await fetch(`${IMAGES_API_BASE_URL}/steps/update`, {
  274. method: 'POST',
  275. headers: {
  276. Accept: 'application/json',
  277. 'Content-Type': 'application/json',
  278. ...(token && { authorization: `Bearer ${token}` })
  279. },
  280. body: JSON.stringify({ steps })
  281. })
  282. .then(async (res) => {
  283. if (!res.ok) throw await res.json();
  284. return res.json();
  285. })
  286. .catch((err) => {
  287. console.log(err);
  288. if ('detail' in err) {
  289. error = err.detail;
  290. } else {
  291. error = 'Server connection failed';
  292. }
  293. return null;
  294. });
  295. if (error) {
  296. throw error;
  297. }
  298. return res.IMAGE_STEPS;
  299. };
  300. export const getImageGenerationModels = async (token: string = '') => {
  301. let error = null;
  302. const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
  303. method: 'GET',
  304. headers: {
  305. Accept: 'application/json',
  306. 'Content-Type': 'application/json',
  307. ...(token && { authorization: `Bearer ${token}` })
  308. }
  309. })
  310. .then(async (res) => {
  311. if (!res.ok) throw await res.json();
  312. return res.json();
  313. })
  314. .catch((err) => {
  315. console.log(err);
  316. if ('detail' in err) {
  317. error = err.detail;
  318. } else {
  319. error = 'Server connection failed';
  320. }
  321. return null;
  322. });
  323. if (error) {
  324. throw error;
  325. }
  326. return res;
  327. };
  328. export const getDefaultImageGenerationModel = async (token: string = '') => {
  329. let error = null;
  330. const res = await fetch(`${IMAGES_API_BASE_URL}/models/default`, {
  331. method: 'GET',
  332. headers: {
  333. Accept: 'application/json',
  334. 'Content-Type': 'application/json',
  335. ...(token && { authorization: `Bearer ${token}` })
  336. }
  337. })
  338. .then(async (res) => {
  339. if (!res.ok) throw await res.json();
  340. return res.json();
  341. })
  342. .catch((err) => {
  343. console.log(err);
  344. if ('detail' in err) {
  345. error = err.detail;
  346. } else {
  347. error = 'Server connection failed';
  348. }
  349. return null;
  350. });
  351. if (error) {
  352. throw error;
  353. }
  354. return res.model;
  355. };
  356. export const updateDefaultImageGenerationModel = async (token: string = '', model: string) => {
  357. let error = null;
  358. const res = await fetch(`${IMAGES_API_BASE_URL}/models/default/update`, {
  359. method: 'POST',
  360. headers: {
  361. Accept: 'application/json',
  362. 'Content-Type': 'application/json',
  363. ...(token && { authorization: `Bearer ${token}` })
  364. },
  365. body: JSON.stringify({
  366. model: model
  367. })
  368. })
  369. .then(async (res) => {
  370. if (!res.ok) throw await res.json();
  371. return res.json();
  372. })
  373. .catch((err) => {
  374. console.log(err);
  375. if ('detail' in err) {
  376. error = err.detail;
  377. } else {
  378. error = 'Server connection failed';
  379. }
  380. return null;
  381. });
  382. if (error) {
  383. throw error;
  384. }
  385. return res.model;
  386. };
  387. export const imageGenerations = async (token: string = '', prompt: string) => {
  388. let error = null;
  389. const res = await fetch(`${IMAGES_API_BASE_URL}/generations`, {
  390. method: 'POST',
  391. headers: {
  392. Accept: 'application/json',
  393. 'Content-Type': 'application/json',
  394. ...(token && { authorization: `Bearer ${token}` })
  395. },
  396. body: JSON.stringify({
  397. prompt: prompt
  398. })
  399. })
  400. .then(async (res) => {
  401. if (!res.ok) throw await res.json();
  402. return res.json();
  403. })
  404. .catch((err) => {
  405. console.log(err);
  406. if ('detail' in err) {
  407. error = err.detail;
  408. } else {
  409. error = 'Server connection failed';
  410. }
  411. return null;
  412. });
  413. if (error) {
  414. throw error;
  415. }
  416. return res;
  417. };