index.ts 9.2 KB

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