Update index.html
Browse files- index.html +47 -31
index.html
CHANGED
|
@@ -50,6 +50,25 @@
|
|
| 50 |
return await window.hfPipeline(task, model);
|
| 51 |
};
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
const handleSend = async () => {
|
| 54 |
if (!input.trim() || loading) return;
|
| 55 |
|
|
@@ -90,51 +109,47 @@
|
|
| 90 |
return;
|
| 91 |
}
|
| 92 |
|
| 93 |
-
// 2. SUPABASE DATA QUERY
|
| 94 |
const isBusinessQuery = /(sales|product|inventory|order|business|store|item|stock)/i.test(userQuery);
|
| 95 |
|
| 96 |
if (isBusinessQuery) {
|
| 97 |
-
setStatus("Fetching
|
| 98 |
const { data: products } = await supabase.from('products').select('*');
|
| 99 |
const { data: orders } = await supabase.from('orders').select('*');
|
| 100 |
|
| 101 |
const rawDataString = `
|
| 102 |
-
|
| 103 |
Products In Stock: ${JSON.stringify(products || [])}
|
| 104 |
Orders History: ${JSON.stringify(orders || [])}
|
| 105 |
`;
|
| 106 |
|
| 107 |
-
// Load Summarizer
|
| 108 |
if (!pipelinesRef.current.summary) {
|
| 109 |
pipelinesRef.current.summary = await getPipeline('summarization', 'Xenova/distilbart-cnn-6-6');
|
| 110 |
}
|
| 111 |
|
| 112 |
const summaryOutput = await pipelinesRef.current.summary(rawDataString, {
|
| 113 |
-
max_new_tokens:
|
| 114 |
min_new_tokens: 30
|
| 115 |
});
|
| 116 |
|
| 117 |
-
//
|
| 118 |
-
let introText = "
|
| 119 |
try {
|
| 120 |
if (!pipelinesRef.current.generate) {
|
| 121 |
-
pipelinesRef.current.generate = await getPipeline('text-generation', 'Xenova/
|
| 122 |
}
|
| 123 |
-
const
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
});
|
| 127 |
-
|
| 128 |
-
let cleanIntro = rawGen.split('\n')[0].replace(`User: ${userQuery}\nAssistant:`, '').trim();
|
| 129 |
-
if (cleanIntro && cleanIntro.length > 5) {
|
| 130 |
-
introText = cleanIntro;
|
| 131 |
-
}
|
| 132 |
} catch (e) {
|
| 133 |
console.log("Using default conversational intro.");
|
| 134 |
}
|
| 135 |
|
| 136 |
-
|
| 137 |
-
const finalBotResponse = `${introText}\n\n
|
| 138 |
|
| 139 |
setMessages(prev => [...prev, {
|
| 140 |
sender: "bot",
|
|
@@ -142,25 +157,22 @@ Orders History: ${JSON.stringify(orders || [])}
|
|
| 142 |
}]);
|
| 143 |
|
| 144 |
} else {
|
| 145 |
-
// 3.
|
| 146 |
-
setStatus("Generating
|
| 147 |
if (!pipelinesRef.current.generate) {
|
| 148 |
-
pipelinesRef.current.generate = await getPipeline('text-generation', 'Xenova/
|
| 149 |
}
|
| 150 |
|
| 151 |
-
const prompt = `
|
| 152 |
const genOutput = await pipelinesRef.current.generate(prompt, {
|
| 153 |
-
max_new_tokens:
|
| 154 |
-
temperature: 0.
|
| 155 |
-
do_sample: true,
|
| 156 |
-
repetition_penalty: 1.2
|
| 157 |
});
|
| 158 |
|
| 159 |
-
let
|
| 160 |
-
let cleanResponse = rawGen.replace(prompt, '').split('\n')[0].replace(/Response as a helpful assistant:/g, '').trim();
|
| 161 |
|
| 162 |
-
if (!cleanResponse
|
| 163 |
-
cleanResponse = "I'm Tina! I can assist you with store inventory, sales
|
| 164 |
}
|
| 165 |
|
| 166 |
setMessages(prev => [...prev, {
|
|
@@ -184,6 +196,7 @@ Orders History: ${JSON.stringify(orders || [])}
|
|
| 184 |
|
| 185 |
return (
|
| 186 |
<div className="flex h-screen bg-slate-950 text-white">
|
|
|
|
| 187 |
<div className="w-64 bg-slate-900 p-4 border-r border-slate-800 flex flex-col justify-between hidden md:flex">
|
| 188 |
<div>
|
| 189 |
<h1 className="text-md font-bold text-white mb-1">HealthyLifePhil</h1>
|
|
@@ -192,12 +205,14 @@ Orders History: ${JSON.stringify(orders || [])}
|
|
| 192 |
<div className="text-xs text-slate-500">Connected to Supabase DB</div>
|
| 193 |
</div>
|
| 194 |
|
|
|
|
| 195 |
<div className="flex-1 flex flex-col">
|
| 196 |
<header className="p-4 bg-slate-900 border-b border-slate-800 flex justify-between items-center text-sm font-semibold">
|
| 197 |
<span>Tina β Business Analysis Chatbot</span>
|
| 198 |
<span className="text-xs text-emerald-400">{status}</span>
|
| 199 |
</header>
|
| 200 |
|
|
|
|
| 201 |
<div className="flex-1 p-6 overflow-y-auto space-y-4">
|
| 202 |
{messages.map((m, index) => (
|
| 203 |
<div key={index} className={`flex ${m.sender === 'user' ? 'justify-end' : 'justify-start'}`}>
|
|
@@ -208,9 +223,10 @@ Orders History: ${JSON.stringify(orders || [])}
|
|
| 208 |
</div>
|
| 209 |
</div>
|
| 210 |
))}
|
| 211 |
-
{loading && <div className="text-xs text-emerald-400 italic animate-pulse">Tina is
|
| 212 |
</div>
|
| 213 |
|
|
|
|
| 214 |
<div className="p-4 bg-slate-900 border-t border-slate-800 flex gap-2">
|
| 215 |
<input
|
| 216 |
type="text"
|
|
|
|
| 50 |
return await window.hfPipeline(task, model);
|
| 51 |
};
|
| 52 |
|
| 53 |
+
const formatSummaryText = (products, orders, rawSummary) => {
|
| 54 |
+
const totalProducts = products ? products.length : 0;
|
| 55 |
+
const totalOrders = orders ? orders.length : 0;
|
| 56 |
+
|
| 57 |
+
let formatted = `π **BUSINESS ANALYTICS SUMMARY**\n`;
|
| 58 |
+
formatted += `βββββββββββββββββββββββββββ\n`;
|
| 59 |
+
formatted += `β’ **Catalog Items:** ${totalProducts} products indexed\n`;
|
| 60 |
+
formatted += `β’ **Transaction Count:** ${totalOrders} order records\n\n`;
|
| 61 |
+
formatted += `π **Key Takeaways & Insights:**\n`;
|
| 62 |
+
|
| 63 |
+
// Convert raw generated paragraph into clean bulleted points
|
| 64 |
+
const sentences = rawSummary.split('.').map(s => s.trim()).filter(s => s.length > 5);
|
| 65 |
+
sentences.forEach(sentence => {
|
| 66 |
+
formatted += `β’ ${sentence}.\n`;
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
return formatted;
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
const handleSend = async () => {
|
| 73 |
if (!input.trim() || loading) return;
|
| 74 |
|
|
|
|
| 109 |
return;
|
| 110 |
}
|
| 111 |
|
| 112 |
+
// 2. SUPABASE DATA QUERY & FORMATTED SUMMARIZATION
|
| 113 |
const isBusinessQuery = /(sales|product|inventory|order|business|store|item|stock)/i.test(userQuery);
|
| 114 |
|
| 115 |
if (isBusinessQuery) {
|
| 116 |
+
setStatus("Fetching Data & Formatting Summary...");
|
| 117 |
const { data: products } = await supabase.from('products').select('*');
|
| 118 |
const { data: orders } = await supabase.from('orders').select('*');
|
| 119 |
|
| 120 |
const rawDataString = `
|
| 121 |
+
Database State:
|
| 122 |
Products In Stock: ${JSON.stringify(products || [])}
|
| 123 |
Orders History: ${JSON.stringify(orders || [])}
|
| 124 |
`;
|
| 125 |
|
|
|
|
| 126 |
if (!pipelinesRef.current.summary) {
|
| 127 |
pipelinesRef.current.summary = await getPipeline('summarization', 'Xenova/distilbart-cnn-6-6');
|
| 128 |
}
|
| 129 |
|
| 130 |
const summaryOutput = await pipelinesRef.current.summary(rawDataString, {
|
| 131 |
+
max_new_tokens: 120,
|
| 132 |
min_new_tokens: 30
|
| 133 |
});
|
| 134 |
|
| 135 |
+
// Format conversational intro using instruction-tuned model
|
| 136 |
+
let introText = "Here is the structured summary of our business data:";
|
| 137 |
try {
|
| 138 |
if (!pipelinesRef.current.generate) {
|
| 139 |
+
pipelinesRef.current.generate = await getPipeline('text-generation', 'Xenova/LaMini-Flan-T5-248M');
|
| 140 |
}
|
| 141 |
+
const introPrompt = `Acknowledge this business query politely in one brief line: "${userQuery}"`;
|
| 142 |
+
const genOutput = await pipelinesRef.current.generate(introPrompt, {
|
| 143 |
+
max_new_tokens: 30,
|
| 144 |
+
temperature: 0.3
|
| 145 |
});
|
| 146 |
+
introText = genOutput[0].generated_text.trim();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
} catch (e) {
|
| 148 |
console.log("Using default conversational intro.");
|
| 149 |
}
|
| 150 |
|
| 151 |
+
const cleanFormattedSummary = formatSummaryText(products, orders, summaryOutput[0].summary_text);
|
| 152 |
+
const finalBotResponse = `${introText}\n\n${cleanFormattedSummary}`;
|
| 153 |
|
| 154 |
setMessages(prev => [...prev, {
|
| 155 |
sender: "bot",
|
|
|
|
| 157 |
}]);
|
| 158 |
|
| 159 |
} else {
|
| 160 |
+
// 3. INSTRUCTION-TUNED TECHNICAL TEXT GENERATION
|
| 161 |
+
setStatus("Generating Technical Response...");
|
| 162 |
if (!pipelinesRef.current.generate) {
|
| 163 |
+
pipelinesRef.current.generate = await getPipeline('text-generation', 'Xenova/LaMini-Flan-T5-248M');
|
| 164 |
}
|
| 165 |
|
| 166 |
+
const prompt = `You are Tina, an AI business assistant for HealthyLifePhil. Answer the user professionally and concisely: ${userQuery}`;
|
| 167 |
const genOutput = await pipelinesRef.current.generate(prompt, {
|
| 168 |
+
max_new_tokens: 80,
|
| 169 |
+
temperature: 0.5
|
|
|
|
|
|
|
| 170 |
});
|
| 171 |
|
| 172 |
+
let cleanResponse = genOutput[0].generated_text.trim();
|
|
|
|
| 173 |
|
| 174 |
+
if (!cleanResponse) {
|
| 175 |
+
cleanResponse = "I'm Tina! I can assist you with store inventory, sales analytics, product management, and general HealthyLifePhil operations.";
|
| 176 |
}
|
| 177 |
|
| 178 |
setMessages(prev => [...prev, {
|
|
|
|
| 196 |
|
| 197 |
return (
|
| 198 |
<div className="flex h-screen bg-slate-950 text-white">
|
| 199 |
+
{/* Sidebar */}
|
| 200 |
<div className="w-64 bg-slate-900 p-4 border-r border-slate-800 flex flex-col justify-between hidden md:flex">
|
| 201 |
<div>
|
| 202 |
<h1 className="text-md font-bold text-white mb-1">HealthyLifePhil</h1>
|
|
|
|
| 205 |
<div className="text-xs text-slate-500">Connected to Supabase DB</div>
|
| 206 |
</div>
|
| 207 |
|
| 208 |
+
{/* Chat Engine Interface */}
|
| 209 |
<div className="flex-1 flex flex-col">
|
| 210 |
<header className="p-4 bg-slate-900 border-b border-slate-800 flex justify-between items-center text-sm font-semibold">
|
| 211 |
<span>Tina β Business Analysis Chatbot</span>
|
| 212 |
<span className="text-xs text-emerald-400">{status}</span>
|
| 213 |
</header>
|
| 214 |
|
| 215 |
+
{/* Chat History Area */}
|
| 216 |
<div className="flex-1 p-6 overflow-y-auto space-y-4">
|
| 217 |
{messages.map((m, index) => (
|
| 218 |
<div key={index} className={`flex ${m.sender === 'user' ? 'justify-end' : 'justify-start'}`}>
|
|
|
|
| 223 |
</div>
|
| 224 |
</div>
|
| 225 |
))}
|
| 226 |
+
{loading && <div className="text-xs text-emerald-400 italic animate-pulse">Tina is processing request...</div>}
|
| 227 |
</div>
|
| 228 |
|
| 229 |
+
{/* Chat Input Bar */}
|
| 230 |
<div className="p-4 bg-slate-900 border-t border-slate-800 flex gap-2">
|
| 231 |
<input
|
| 232 |
type="text"
|