Upload demo-gpt-oss.js with huggingface_hub
Browse files- demo-gpt-oss.js +41 -0
demo-gpt-oss.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
+
|
| 3 |
+
const HF_INFERENCE_API_URL = "https://api-inference.huggingface.co/models/openai/gpt-oss-120b";
|
| 4 |
+
const HF_TOKEN = process.env.HF_TOKEN; // تأكد من تعيينه كمتغير بيئة
|
| 5 |
+
|
| 6 |
+
async function generateProductDescription(productName, keywords = '') {
|
| 7 |
+
const prompt = `اكتب وصفًا جذابًا ومفصلاً للمنتج التالي: ${productName}. ركز على الكلمات المفتاحية: ${keywords}. اجعله مناسبًا لمتجر إلكتروني حديث مثل Shopify Hydrogen.`;
|
| 8 |
+
try {
|
| 9 |
+
const response = await axios.post(
|
| 10 |
+
HF_INFERENCE_API_URL,
|
| 11 |
+
{ inputs: prompt, parameters: { max_new_tokens: 200, temperature: 0.7, top_p: 0.9 } },
|
| 12 |
+
{
|
| 13 |
+
headers: {
|
| 14 |
+
Authorization: `Bearer ${HF_TOKEN}`,
|
| 15 |
+
'Content-Type': 'application/json',
|
| 16 |
+
},
|
| 17 |
+
}
|
| 18 |
+
);
|
| 19 |
+
return response.data[0].generated_text.replace(prompt, '').trim(); // إزالة الـ prompt من بداية النص المولّد
|
| 20 |
+
} catch (error) {
|
| 21 |
+
console.error("Error generating product description:", error.response ? error.response.data : error.message);
|
| 22 |
+
throw error;
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// مثال للاستخدام
|
| 27 |
+
async function main() {
|
| 28 |
+
const productName = "فستان سهرة حريري فاخر";
|
| 29 |
+
const keywords = "أنيق، مريح، تصميم فريد، مناسبات خاصة، جودة عالية";
|
| 30 |
+
|
| 31 |
+
console.log(`Generating description for: ${productName} with keywords: ${keywords}`);
|
| 32 |
+
try {
|
| 33 |
+
const description = await generateProductDescription(productName, keywords);
|
| 34 |
+
console.log("\nGenerated Product Description:");
|
| 35 |
+
console.log(description);
|
| 36 |
+
} catch (error) {
|
| 37 |
+
console.error("Failed to generate description in main function.", error);
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
main();
|