Spaces:
Running
Running
File size: 5,644 Bytes
4fb0c68 7f37689 4fb0c68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import axios from 'axios';
import axiosInstance from './axiosInstance';
// Ensure you have VITE_API_URL set in your .env file
const API_URL = import.meta.env.VITE_API_URL;
export const register = async (first_name, surname, email, phone, country, address, password) => {
const data = new URLSearchParams();
data.append('first_name', first_name);
data.append('surname', surname);
data.append('email', email);
data.append('phone', phone);
data.append('country', country);
data.append('address', address);
data.append('password', password);
try {
console.log('data', data);
const response = await axios.post(`${API_URL}/api/v1/user/`, data,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
});
return response.data;
} catch (error) {
console.error('Registration failed:', error);
throw error;
}
};
export const login = async (email, password) => {
const data = new URLSearchParams();
data.append('username', email);
data.append('password', password);
try {
const response = await axios.post(`${API_URL}/token`, data,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
});
return response;
} catch (error) {
console.error('Login failed:', error);
throw error;
}
};
export const logoutUser = () => {
localStorage.removeItem('token');
};
export const getTemplates = async (templateName) => {
try {
const response = await axiosInstance.get(`${API_URL}/api/v1/ocrtemplate/`, { params: { template_name: templateName } });
return response.data;
} catch (error) {
console.error('Error fetching templates:', error);
throw error;
}
};
export const createTemplate = async (templateName, fields, userId) => {
const data = {
template_name: templateName,
fields: fields,
user_id: userId,
};
try {
const response = await axiosInstance.post(`${API_URL}/api/v1/ocrtemplate/`, data);
return response.data;
} catch (error) {
console.error('Error creating template:', error);
throw error;
}
};
export const createOrUpdateTemplate = async (templateName, fields, userId, updated) => {
const data = {
template_name: templateName,
fields: fields,
user_id: userId,
};
try {
let response;
console.log(updated)
if (updated) {
// Update existing template
response = await axiosInstance.put(`${API_URL}/api/v1/ocrtemplate/templates/`, data);
} else {
// Create new template
response = await axiosInstance.post(`${API_URL}/api/v1/ocrtemplate/`, data);
}
return response.data;
} catch (error) {
console.error('Error creating/updating template:', error);
throw error;
}
};
export const deleteTemplate = async (templateName) => {
try {
const response = await axiosInstance.delete(`${API_URL}/api/v1/ocrtemplate/${templateName}`);
return response.data;
} catch (error) {
console.error('Error deleting template:', error);
throw error;
}
};
export const ocrProcess = async (formData) => {
try {
for (let [key, value] of formData.entries()) {
console.log(`${key}: ${value.name || value}`);
}
const response = await axiosInstance.post(`${API_URL}/api/v1/ocr/process`, formData);
// Check for HTTP errors
if (response.status !== 200) {
throw new Error(`Error: ${response.statusText}`);
}
const data = response.data; // axios automatically parses JSON
console.log("Success:", data);
return data; // Return the parsed data
} catch (error) {
console.error("Error:", error);
alert("There was an error processing the files.");
throw error; // Rethrow the error for further handling if needed
}
};
export const textToSpeech = async (text, languageCode, ssmlGender, name, pitch, speakingRate, volumeGainDb) => {
const data = {
text,
language_code: languageCode,
ssml_gender: ssmlGender,
name,
pitch,
speaking_rate: speakingRate,
volume_gain_db: volumeGainDb
};
// Get the token from your storage solution (e.g., localStorage)
const token = localStorage.getItem('token');
const headers = {
'xi-api-key': 'u2', // Replace with the actual API key for the user
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
};
try {
console.log(data)
// const response = await axios.post(`${API_URL}/api/v1/text-to-speech/`, data, {headers, responseType: 'blob'});
const response = await axios.post(`${API_URL}/api/v1/text-to-speech/generate`, data, {headers});
// Create a URL for the blob and return it
// const audioUrl = window.URL.createObjectURL(new Blob([response.data]));
const audioUrl = response.data.data.key;
console.log(audioUrl )
return audioUrl
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error('Error response data:', error.response.data);
console.error('Error response status:', error.response.status);
console.error('Error response headers:', error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.error('Error request data:', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error('Error message:', error.message);
}
console.error('Error config:', error.config);
throw error;
}
};
|