-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (61 loc) · 2.05 KB
/
index.js
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
import OpenAI from "openai";
import got from "got";
import "dotenv/config";
const imaggaApiKey = process.env.IMAGGA_API_KEY;
const imaggaApiSecret = process.env.IMAGGA_API_SECRET;
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function getKeywordsFromImage(imageUrl) {
try {
const imaggaUrl = `https://api.imagga.com/v2/tags?image_url=${encodeURIComponent(
imageUrl
)}`;
const response = await got(imaggaUrl, {
username: imaggaApiKey,
password: imaggaApiSecret,
});
const tags = JSON.parse(response.body).result.tags;
const tagNames = tags.map((tag) => tag.tag.en);
const keywords = tagNames.join(", ");
return keywords;
} catch (error) {
console.error(error.response.body);
throw new Error("Failed to retrieve keywords from the image.");
}
}
async function generateSongsList(keywords) {
try {
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: `Generate a list of 10 hindi songs in bulleted form along with the context based on the keywords: ${keywords}`,
},
],
model: "gpt-3.5-turbo",
});
const songsList = completion.choices[0].message.content.trim();
return songsList;
} catch (error) {
console.error("Error generating songs list:", error);
}
}
const generateSongsUsingAI = async (imageUrl) => {
try {
const keywords = await getKeywordsFromImage(imageUrl);
const songList = await generateSongsList(keywords);
return songList;
} catch (error) {
console.error("Error:", error.message);
}
};
export default generateSongsUsingAI;
// console.log(await generateSongsUsingAI("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg"))
// (async () => {
// try {
// const keywords = await getKeywordsFromImage(imageUrl);
// await generateSongsList(keywords);
// } catch (error) {
// console.error('Error:', error.message);
// }
// })();