ibrohm commited on
Commit
4c30579
·
verified ·
1 Parent(s): e39e981

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +25 -9
server.js CHANGED
@@ -160,7 +160,9 @@ app.post('/status', async (req, res) => {
160
  }
161
  });
162
 
163
- // AI Chat Endpoint (using Hugging Face Inference API)
 
 
164
  app.post('/ai', async (req, res) => {
165
  try {
166
  const { text } = req.body;
@@ -168,29 +170,43 @@ app.post('/ai', async (req, res) => {
168
  return res.status(400).json({ error: 'Text is required' });
169
  }
170
 
171
- // Use Hugging Face free Inference API
172
- const response = await fetch('https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium', {
173
  method: 'POST',
174
  headers: {
175
  'Content-Type': 'application/json',
176
- // Using public model, no token needed
177
  },
178
- body: JSON.stringify({ inputs: text })
 
 
 
 
 
 
179
  });
180
 
181
  if (response.ok) {
182
  const data = await response.json();
183
- // DialoGPT returns array of generated responses
184
- const aiText = data[0]?.generated_text || "Kechirasiz, javob bera olmadim.";
 
 
 
 
 
 
 
185
  res.json({ text: aiText });
186
  } else {
 
187
  // Fallback response if API fails
188
  const fallbackResponses = [
189
  "Salom! Men sizga yordam berishga tayyorman. 😊",
190
  "Qiziq savol! Batafsil tushuntirib bera olasizmi?",
191
- "Men haqiqiy AI emasman, lekin yordam berishga harakat qilaman!",
192
  "Bu juda yaxshi fikr! Davom eting.",
193
- "Tushunarli. Yana nima so'rashni xohlaysiz?"
 
194
  ];
195
  const randomResponse = fallbackResponses[Math.floor(Math.random() * fallbackResponses.length)];
196
  res.json({ text: randomResponse });
 
160
  }
161
  });
162
 
163
+ // AI Chat Endpoint (using Hugging Face Inference API with Token)
164
+ const HF_TOKEN = process.env.HF_TOKEN || '';
165
+
166
  app.post('/ai', async (req, res) => {
167
  try {
168
  const { text } = req.body;
 
170
  return res.status(400).json({ error: 'Text is required' });
171
  }
172
 
173
+ // Use Hugging Face Inference API with authentication
174
+ const response = await fetch('https://api-inference.huggingface.co/models/microsoft/DialoGPT-large', {
175
  method: 'POST',
176
  headers: {
177
  'Content-Type': 'application/json',
178
+ 'Authorization': `Bearer ${HF_TOKEN}`
179
  },
180
+ body: JSON.stringify({
181
+ inputs: text,
182
+ parameters: {
183
+ max_length: 150,
184
+ temperature: 0.7
185
+ }
186
+ })
187
  });
188
 
189
  if (response.ok) {
190
  const data = await response.json();
191
+ // DialoGPT returns generated text
192
+ let aiText = '';
193
+ if (Array.isArray(data) && data[0]?.generated_text) {
194
+ aiText = data[0].generated_text;
195
+ } else if (data.generated_text) {
196
+ aiText = data.generated_text;
197
+ } else {
198
+ aiText = "Tushundim. Yana nima so'rashni xohlaysiz?";
199
+ }
200
  res.json({ text: aiText });
201
  } else {
202
+ console.error('HF API Error:', response.status, await response.text());
203
  // Fallback response if API fails
204
  const fallbackResponses = [
205
  "Salom! Men sizga yordam berishga tayyorman. 😊",
206
  "Qiziq savol! Batafsil tushuntirib bera olasizmi?",
 
207
  "Bu juda yaxshi fikr! Davom eting.",
208
+ "Tushunarli. Yana nima so'rashni xohlaysiz?",
209
+ "Rahmat! Sizga yana qanday yordam bera olaman?"
210
  ];
211
  const randomResponse = fallbackResponses[Math.floor(Math.random() * fallbackResponses.length)];
212
  res.json({ text: randomResponse });