Skip to content

Instantly share code, notes, and snippets.

@alexb4a
Created June 27, 2024 13:43
Show Gist options
  • Save alexb4a/4ae4615bd0262c4c8966893f18671fe0 to your computer and use it in GitHub Desktop.
Save alexb4a/4ae4615bd0262c4c8966893f18671fe0 to your computer and use it in GitHub Desktop.
Cloud Code for our Add AI to your App Youtube Series
const OpenAI = require('openai');
const axios = require("axios");
const FormData = require("form-data");
// Initialize OpenAI with your API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// AfterSave trigger for the Image class
Parse.Cloud.afterSave('Image', async(request) => {
const imageObject = request.object;
const prompt = imageObject.get('prompt');
// If the object already existed, do nothing
if (imageObject.existed()){
return;
}
try {
// Generate an image using OpenAI's DALL-E model
const response = await openai.images.generate({
model: 'dall-e-3',
prompt: prompt,
size: '1024x1024',
n: 1,
});
const imageUrl = response.data[0].url;
// Fetch the generated image from the URL
const imageResponse = await axios.get(imageUrl, { responseType: 'arraybuffer'});
const imageBuffer = Buffer.from(imageResponse.data, 'binary');
// Create a Parse File from the image buffer
const parseFile = new Parse.File('image.png', { base64: imageBuffer.toString('base64')});
await parseFile.save();
// Set the file field of the Image object and save it
imageObject.set('file', parseFile);
await imageObject.save(null, { useMasterKey: true});
console.log("Image Saved!");
} catch (error) {
console.log("ERROR: " + error);
}
});
// AfterSave trigger for the Audio class
Parse.Cloud.afterSave("Audio", async(request) => {
const audioUpload = request.object;
// Check if the audio field is present and the translation field is empty
if (audioUpload.get('audio') && !audioUpload.get("translation")) {
const audioFile = audioUpload.get("audio");
const audioUrl = audioFile.url();
try {
// Fetch the audio file from the URL
const response = await axios.get(audioUrl, { responseType: 'arraybuffer'});
const audioBuffer = Buffer.from(response.data);
// Prepare form data for the Whisper API request
const formData = new FormData();
formData.append('file', audioBuffer, { filename:"audio.mp3"});
formData.append('model', 'whisper-1');
formData.append('source_lang', 'pt');
formData.append('target_lang', 'en');
// Send the audio file to OpenAI's Whisper model for translation
const translationResponse = await axios.post("https://api.openai.com/v1/audio/translations", formData, {
headers: {
...formData.getHeaders(),
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
}
});
// Retrieve the translated text from the response
const translatedText = translationResponse.data.text;
audioUpload.set('translation', translatedText);
await audioUpload.save();
} catch (error) {
console.log("ERROR: " + error);
}
} else {
console.log("Not running this code");
}
});
// AfterSave trigger for the Upload class
Parse.Cloud.afterSave('Upload', async (request) => {
const upload = request.object;
console.log('>>>> Starting AfterSave');
// Check if the photo field is present and the description field is empty
if (upload.get('photo') && !upload.get('description')) {
console.log('>>>> Entering If');
const photoFile = upload.get('photo');
const photoUrl = photoFile.url();
try {
console.log('>>>> Entering Try');
// Call OpenAI API to get image description
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Descreva esta imagem e tente identificar onde a foto foi tirada. Tente verificar imagens semelhantes, vegetação, relevo para obter um resultado mais preciso."
},
{
type: "image_url",
image_url: {
"url": photoUrl,
},
},
],
},
],
});
// Retrieve the description from OpenAI response
const description = response.choices[0].message.content;
// Update the Upload object with the new description
upload.set('description', description);
await upload.save();
console.log('Description updated successfully:', description);
} catch (error) {
console.log('>>>> Entering Catch');
console.error('Error processing image with OpenAI:', error);
}
} else {
console.log('>>>> Entering Else');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment