ts-api / pages /api /huggingchat.js
Shuddho's picture
Upload 42 files
d04e364 verified
// pages/api/chat.js
import axios from 'axios';
export const config = {
"name" : "huggingchat",
"url" : "/api/huggingchat",
"description" : "Get response from huggingchat AI. huggingchat AI is a very powerful AI that can do a lot of things. It have access to real time internet! It can provide you with the latest news, weather, and more.",
"query" : "prompt, web_search",
"response" : "text",
"testURL" : "./api/huggingchat?prompt=hello&web_search=false"
}
const getUUID = async ()=>{
const headers = {
Cookie: '_ga=GA1.1.263364295.1704009324; __stripe_mid=576d7254-9b38-447d-b03a-0455234c85aa028d68; token=HEiBngeHcwmTbeNyMsqghqEbqamhgoGDNPAkFnaLNdUzfcoRqDBFImcbJrZwxXiAcVgxycPcNYLiGLwxAplmnxYpiwSmJzKfeINQjOfOzRRYkceAMEdDMmAmbOvBoJCB; token=HEiBngeHcwmTbeNyMsqghqEbqamhgoGDNPAkFnaLNdUzfcoRqDBFImcbJrZwxXiAcVgxycPcNYLiGLwxAplmnxYpiwSmJzKfeINQjOfOzRRYkceAMEdDMmAmbOvBoJCB; aws-waf-token=d60bed03-062c-4a5e-a13a-6812ec27064f:HgoAbUVRTiMBAAAA:0mDwu8h4MQwINC18fLNSLZa1kT3/1v4svl7RhEX1M8tXwchT/FqCc6fsn9eg88/G2yxHICSo0ab7R71o+rZlYjLDSsKteYXZSbVJyACrSUoE+x0T30pCqCkepaLaiI7jy5bC5DRPEWmpy5TYVpPmfLWf5EA9o3Jp9HYG7qw2i7t350MCdgb0fmDPxP7wnQoAMioXkyL13f3r2rxYAZVw78KlZdE=; __stripe_sid=94dfd71a-ca40-498b-ad9f-6e29b1f8d7c634c758; hf-chat=699d2836-9e8f-482c-9515-3dd214d419ce; _ga_8Q63TH4CSL=GS1.1.1712576098.12.1.1712576109.49.0.0',
'Content-Type': 'application/json'
};
let url = 'https://huggingface.co/chat/conversation/6614111dbb90af362a0d6ab4/__data.json?x-sveltekit-invalidated=11'
const response = await axios.get(url, { headers });
const data = response.data;
function findAllUUIDs(data) {
const uuids = []; // Array to store UUIDs
// Recursive function to traverse nested objects and arrays
function traverse(obj) {
for (const key in obj) {
if (typeof obj[key] === 'string' && obj[key].match(/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/)) {
uuids.push(obj[key]); // If the value is a UUID, push it to the array
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
traverse(obj[key]); // If the value is an object or array, recursively call the function
}
}
}
traverse(data); // Start traversal from the root object
return uuids;
}
const allUUIDs = findAllUUIDs(data);
console.log(allUUIDs);
console.log(allUUIDs[allUUIDs.length - 1]);
return allUUIDs[allUUIDs.length - 1];
}
export default async function handler(req, res) {
if (req.method === 'GET') {
const { prompt, web_search } = req.query;
const url = 'https://huggingface.co/chat/conversation/6614111dbb90af362a0d6ab4';
const headers = {
Cookie: '_ga=GA1.1.263364295.1704009324; __stripe_mid=576d7254-9b38-447d-b03a-0455234c85aa028d68; token=HEiBngeHcwmTbeNyMsqghqEbqamhgoGDNPAkFnaLNdUzfcoRqDBFImcbJrZwxXiAcVgxycPcNYLiGLwxAplmnxYpiwSmJzKfeINQjOfOzRRYkceAMEdDMmAmbOvBoJCB; token=HEiBngeHcwmTbeNyMsqghqEbqamhgoGDNPAkFnaLNdUzfcoRqDBFImcbJrZwxXiAcVgxycPcNYLiGLwxAplmnxYpiwSmJzKfeINQjOfOzRRYkceAMEdDMmAmbOvBoJCB; aws-waf-token=d60bed03-062c-4a5e-a13a-6812ec27064f:HgoAbUVRTiMBAAAA:0mDwu8h4MQwINC18fLNSLZa1kT3/1v4svl7RhEX1M8tXwchT/FqCc6fsn9eg88/G2yxHICSo0ab7R71o+rZlYjLDSsKteYXZSbVJyACrSUoE+x0T30pCqCkepaLaiI7jy5bC5DRPEWmpy5TYVpPmfLWf5EA9o3Jp9HYG7qw2i7t350MCdgb0fmDPxP7wnQoAMioXkyL13f3r2rxYAZVw78KlZdE=; __stripe_sid=94dfd71a-ca40-498b-ad9f-6e29b1f8d7c634c758; hf-chat=699d2836-9e8f-482c-9515-3dd214d419ce; _ga_8Q63TH4CSL=GS1.1.1712576098.12.1.1712576109.49.0.0',
'Content-Type': 'application/json'
};
const data = {
inputs: prompt || 'hi',
id: await getUUID(),
is_retry: false,
is_continue: false,
web_search: web_search=='true',
files: []
};
try {
const response = await axios.post(url, data, { headers });
console.log('API response:', response.data);
res.status(response.status).json(JSON.parse(response.data.split('\n')[response.data.split('\n').length - 2]));
} catch (error) {
console.error('Error:', error);
res.status(error.response?.status || 500).json({ message: 'Internal server error' });
}
} else {
res.setHeader('Allow', ['GET']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}