index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { AUDIO_API_BASE_URL } from '$lib/constants';
  2. export const getAudioConfig = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${AUDIO_API_BASE_URL}/config`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. }
  10. })
  11. .then(async (res) => {
  12. if (!res.ok) throw await res.json();
  13. return res.json();
  14. })
  15. .catch((err) => {
  16. console.log(err);
  17. error = err.detail;
  18. return null;
  19. });
  20. if (error) {
  21. throw error;
  22. }
  23. return res;
  24. };
  25. type OpenAIConfigForm = {
  26. url: string;
  27. key: string;
  28. model: string;
  29. speaker: string;
  30. };
  31. export const updateAudioConfig = async (token: string, payload: OpenAIConfigForm) => {
  32. let error = null;
  33. const res = await fetch(`${AUDIO_API_BASE_URL}/config/update`, {
  34. method: 'POST',
  35. headers: {
  36. 'Content-Type': 'application/json',
  37. Authorization: `Bearer ${token}`
  38. },
  39. body: JSON.stringify({
  40. ...payload
  41. })
  42. })
  43. .then(async (res) => {
  44. if (!res.ok) throw await res.json();
  45. return res.json();
  46. })
  47. .catch((err) => {
  48. console.log(err);
  49. error = err.detail;
  50. return null;
  51. });
  52. if (error) {
  53. throw error;
  54. }
  55. return res;
  56. };
  57. export const transcribeAudio = async (token: string, file: File) => {
  58. const data = new FormData();
  59. data.append('file', file);
  60. let error = null;
  61. const res = await fetch(`${AUDIO_API_BASE_URL}/transcriptions`, {
  62. method: 'POST',
  63. headers: {
  64. Accept: 'application/json',
  65. authorization: `Bearer ${token}`
  66. },
  67. body: data
  68. })
  69. .then(async (res) => {
  70. if (!res.ok) throw await res.json();
  71. return res.json();
  72. })
  73. .catch((err) => {
  74. error = err.detail;
  75. console.log(err);
  76. return null;
  77. });
  78. if (error) {
  79. throw error;
  80. }
  81. return res;
  82. };
  83. export const synthesizeOpenAISpeech = async (
  84. token: string = '',
  85. speaker: string = 'alloy',
  86. text: string = '',
  87. model?: string
  88. ) => {
  89. let error = null;
  90. const res = await fetch(`${AUDIO_API_BASE_URL}/speech`, {
  91. method: 'POST',
  92. headers: {
  93. Authorization: `Bearer ${token}`,
  94. 'Content-Type': 'application/json'
  95. },
  96. body: JSON.stringify({
  97. input: text,
  98. voice: speaker,
  99. ...(model && { model })
  100. })
  101. })
  102. .then(async (res) => {
  103. if (!res.ok) throw await res.json();
  104. return res;
  105. })
  106. .catch((err) => {
  107. error = err.detail;
  108. console.log(err);
  109. return null;
  110. });
  111. if (error) {
  112. throw error;
  113. }
  114. return res;
  115. };
  116. export const getModels = async (token: string = '') => {
  117. let error = null;
  118. const res = await fetch(`${AUDIO_API_BASE_URL}/models`, {
  119. method: 'GET',
  120. headers: {
  121. 'Content-Type': 'application/json',
  122. Authorization: `Bearer ${token}`
  123. }
  124. })
  125. .then(async (res) => {
  126. if (!res.ok) throw await res.json();
  127. return res.json();
  128. })
  129. .catch((err) => {
  130. error = err.detail;
  131. console.log(err);
  132. return null;
  133. });
  134. if (error) {
  135. throw error;
  136. }
  137. return res;
  138. };
  139. export const getVoices = async (token: string = '') => {
  140. let error = null;
  141. const res = await fetch(`${AUDIO_API_BASE_URL}/voices`, {
  142. method: 'GET',
  143. headers: {
  144. 'Content-Type': 'application/json',
  145. Authorization: `Bearer ${token}`
  146. }
  147. })
  148. .then(async (res) => {
  149. if (!res.ok) throw await res.json();
  150. return res.json();
  151. })
  152. .catch((err) => {
  153. error = err.detail;
  154. console.log(err);
  155. return null;
  156. });
  157. if (error) {
  158. throw error;
  159. }
  160. return res;
  161. };