ibrohm commited on
Commit
e76d669
·
1 Parent(s): b6c6de0

Fix AI banner: add Uz-En fallback dictionary, fix Gemini model name, add logging

Browse files
Files changed (1) hide show
  1. bot/bot.js +58 -11
bot/bot.js CHANGED
@@ -34,14 +34,49 @@ function downloadImage(url) {
34
  });
35
  }
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  // AI yordamida (Gemini) promptni professional kengaytirish
38
  async function enhancePromptWithGemini(userText, templateBase = '') {
39
- if (!process.env.GEMINI_API_KEY) {
40
- return (templateBase ? templateBase + ', ' : '') + userText + ', photorealistic, detailed';
41
- }
42
- try {
43
- const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
44
- const prompt = `You are an expert AI image prompt engineer for an e-commerce platform.
45
  The user wants to generate a banner image, but they write in Uzbek and might have typos.
46
  CRITICAL RULE: Interpret words in FASHION, CLOTHING, TECH, or E-COMMERCE contexts. E.g., if they write "manti", "mantiani", or "mantiya", they mean "graduation gown", "coat", or "mantle" (clothing), NEVER the food (dumplings).
47
 
@@ -54,12 +89,24 @@ Template Context: ${templateBase || 'None'}
54
  User's Idea: "${userText}"
55
 
56
  Respond ONLY with the final English prompt string, and nothing else.`;
57
- const result = await model.generateContent(prompt);
58
- return result.response.text().trim();
59
- } catch (e) {
60
- console.error("Gemini error:", e);
61
- return userText + ', photorealistic, no text';
 
 
 
 
 
62
  }
 
 
 
 
 
 
 
63
  }
64
 
65
  // AI Banner Shablon Promptlari
 
34
  });
35
  }
36
 
37
+ // O'zbekcha-Inglizcha sodda lug'at (Gemini ishlamasa fallback)
38
+ const uzToEn = {
39
+ 'kiyim': 'clothing', 'kiyimlar': 'clothes', 'moda': 'fashion', 'ayol': 'woman', 'erkak': 'man',
40
+ 'go\'zal': 'beautiful', 'chiroyli': 'beautiful', 'zamonaviy': 'modern', 'yangi': 'new',
41
+ 'chegirma': 'sale discount', 'skidka': 'sale discount', 'aksiya': 'promotion sale',
42
+ 'arzon': 'affordable price', 'narx': 'price',
43
+ 'yozgi': 'summer', 'qishki': 'winter', 'bahorgi': 'spring', 'kuzgi': 'autumn',
44
+ 'kolleksiya': 'collection', 'sport': 'sports athletic', 'sportiv': 'sports athletic',
45
+ 'athietivk': 'athletic sportswear', 'atletik': 'athletic sportswear',
46
+ 'texnika': 'technology gadgets', 'gadjet': 'gadgets devices', 'telefon': 'smartphone phone',
47
+ 'kompyuter': 'computer laptop', 'audio': 'audio headphones',
48
+ 'sovga': 'gift present', 'bayram': 'holiday celebration', 'tovar': 'product', 'tavaar': 'product',
49
+ 'tovarlar': 'products', 'tavaarlar': 'products', 'barch': 'all', 'barcha': 'all',
50
+ 'reklama': 'advertisement', 'banner': 'promotional banner', 'dizayn': 'design',
51
+ 'dizayinda': 'in design style', 'premium': 'premium luxury', 'klassik': 'classic elegant',
52
+ 'rangli': 'colorful', 'qora': 'black', 'oq': 'white', 'qizil': 'red', 'ko\'k': 'blue',
53
+ 'yashil': 'green', 'sariq': 'yellow', 'pushti': 'pink',
54
+ 'kurtka': 'jacket', 'kostyum': 'suit', 'kastiyum': 'suit', 'ko\'ylak': 'shirt dress',
55
+ 'shim': 'pants trousers', 'futbolka': 't-shirt', 'palto': 'coat overcoat',
56
+ 'tufli': 'shoes', 'krossovka': 'sneakers', 'sumka': 'bag handbag',
57
+ 'bolalar': 'children kids', 'bola': 'child kid', 'oila': 'family',
58
+ 'do\'kon': 'store shop', 'magazin': 'store shop', 'savdo': 'commerce shopping',
59
+ 'yasa': '', 'yoq': '', 'deb': '', 'yozilgan': 'with text overlay',
60
+ 'mantiya': 'graduation gown mantle', 'manti': 'graduation gown coat',
61
+ };
62
+
63
+ function translateUzToEn(text) {
64
+ let result = text.toLowerCase();
65
+ // Har bir so'zni tarjima qilish
66
+ for (const [uz, en] of Object.entries(uzToEn)) {
67
+ result = result.replace(new RegExp(uz.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), en);
68
+ }
69
+ // Raqamlarni saqlab qolish (masalan: 30%)
70
+ return result.trim();
71
+ }
72
+
73
  // AI yordamida (Gemini) promptni professional kengaytirish
74
  async function enhancePromptWithGemini(userText, templateBase = '') {
75
+ // Avval Gemini bilan urinib ko'rish
76
+ if (process.env.GEMINI_API_KEY) {
77
+ try {
78
+ const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });
79
+ const prompt = `You are an expert AI image prompt engineer for an e-commerce platform.
 
80
  The user wants to generate a banner image, but they write in Uzbek and might have typos.
81
  CRITICAL RULE: Interpret words in FASHION, CLOTHING, TECH, or E-COMMERCE contexts. E.g., if they write "manti", "mantiani", or "mantiya", they mean "graduation gown", "coat", or "mantle" (clothing), NEVER the food (dumplings).
82
 
 
89
  User's Idea: "${userText}"
90
 
91
  Respond ONLY with the final English prompt string, and nothing else.`;
92
+ const result = await model.generateContent(prompt);
93
+ const enhanced = result.response.text().trim();
94
+ console.log('[AI BANNER] Gemini enhanced:', enhanced);
95
+ return enhanced;
96
+ } catch (e) {
97
+ console.error("[AI BANNER] Gemini FAILED:", e.message);
98
+ // Gemini xato qilsa — fallback'ga tushadi (pastga)
99
+ }
100
+ } else {
101
+ console.warn("[AI BANNER] GEMINI_API_KEY not found, using dictionary fallback");
102
  }
103
+
104
+ // FALLBACK: Gemini ishlamasa, oddiy lug'at orqali tarjima
105
+ const translated = translateUzToEn(userText);
106
+ const base = templateBase ? templateBase + ', ' : '';
107
+ const fallback = `${base}${translated}, professional product photography, photorealistic, cinematic lighting, highly detailed`;
108
+ console.log('[AI BANNER] Fallback prompt:', fallback);
109
+ return fallback;
110
  }
111
 
112
  // AI Banner Shablon Promptlari