Update server.js
Browse files159 unmodified lines1601611621631641651661 unmodified line168169170171172173174175176177178179180181182183184185186187188189190191192193194195196159 unmodified lines}});// AI Chat Endpoint (using Hugging Face Inference API)app.post('/ai', async (req, res) => {try {const { text } = req.body;1 unmodified linereturn res.status(400).json({ error: 'Text is required' });}// Use Hugging Face free Inference APIconst response = await fetch('https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium', {method: 'POST',headers: {'Content-Type': 'application/json',// Using public model, no token needed},body: JSON.stringify({ inputs: text })});if (response.ok) {const data = await response.json();// DialoGPT returns array of generated responsesconst aiText = data[0]?.generated_text || "Kechirasiz, javob bera olmadim.";res.json({ text: aiText });} else {// Fallback response if API failsconst fallbackResponses = ["Salom! Men sizga yordam berishga tayyorman. 😊","Qiziq savol! Batafsil tushuntirib bera olasizmi?","Men haqiqiy AI emasman, lekin yordam berishga harakat qilaman!","Bu juda yaxshi fikr! Davom eting.","Tushunarli. Yana nima so'rashni xohlaysiz?"];const randomResponse = fallbackResponses[Math.floor(Math.random() * fallbackResponses.length)];res.json({ text: randomResponse });159 unmodified lines1601611621631641651661671681 unmodified line170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212159 unmodified lines}});// AI Chat Endpoint (using Hugging Face Inference API with Token)const HF_TOKEN = process.env.HF_TOKEN || '';app.post('/ai', async (req, res) => {try {const { text } = req.body;1 unmodified linereturn res.status(400).json({ error: 'Text is required' });}// Use Hugging Face Inference API with authenticationconst response = await fetch('https://api-inference.huggingface.co/models/microsoft/DialoGPT-large', {method: 'POST',headers: {'Content-Type': 'application/json','Authorization': `Bearer ${HF_TOKEN}`},body: JSON.stringify({inputs: text,parameters: {max_length: 150,temperature: 0.7}})});if (response.ok) {const data = await response.json();// DialoGPT returns generated textlet aiText = '';if (Array.isArray(data) && data[0]?.generated_text) {aiText = data[0].generated_text;} else if (data.generated_text) {aiText = data.generated_text;} else {aiText = "Tushundim. Yana nima so'rashni xohlaysiz?";}res.json({ text: aiText });} else {console.error('HF API Error:', response.status, await response.text());// Fallback response if API failsconst fallbackResponses = ["Salom! Men sizga yordam berishga tayyorman. 😊","Qiziq savol! Batafsil tushuntirib bera olasizmi?","Bu juda yaxshi fikr! Davom eting.","Tushunarli. Yana nima so'rashni xohlaysiz?","Rahmat! Sizga yana qanday yordam bera olaman?"];const randomResponse = fallbackResponses[Math.floor(Math.random() * fallbackResponses.length)];res.json({ text: randomResponse });
server.js+25-9