Spaces:
Running
Running
File size: 18,630 Bytes
ea943de 926e3b0 ea943de 926e3b0 ea943de 926e3b0 ea943de cd96ee7 ea943de cd96ee7 ea943de cd96ee7 ea943de cd96ee7 ea943de |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
import React, { useState, useEffect, useRef } from 'react';
import { Send, Plus, MessageSquare, Settings, Users, Download, Trash2, RefreshCw, Bot } from 'lucide-react';
const ChatFlowApp = () => {
const [messages, setMessages] = useState([]);
const [currentMessage, setCurrentMessage] = useState('');
const [selectedModel, setSelectedModel] = useState('openai/gpt-3.5-turbo');
const [isLoading, setIsLoading] = useState(false);
const [sessions, setSessions] = useState([]);
const [currentSessionId, setCurrentSessionId] = useState('default');
const [onlineUsers, setOnlineUsers] = useState(1);
const [apiStatus, setApiStatus] = useState('Connected');
const [autoSave, setAutoSave] = useState(true);
const messagesEndRef = useRef(null);
const [userId] = useState('User-' + Math.random().toString(36).substr(2, 8));
const models = [
{ name: "GPT-3.5 Turbo", id: "openai/gpt-3.5-turbo" },
{ name: "LLaMA 3.1 8B", id: "meta-llama/llama-3.1-8b-instruct" },
{ name: "LLaMA 3.1 70B", id: "meta-llama/llama-3.1-70b-instruct" },
{ name: "DeepSeek Chat v3", id: "deepseek/deepseek-chat-v3-0324:free" },
{ name: "DeepSeek R1", id: "deepseek/deepseek-r1-0528:free" },
{ name: "Qwen3 Coder", id: "qwen/qwen3-coder:free" },
{ name: "Microsoft MAI DS R1", id: "microsoft/mai-ds-r1:free" },
{ name: "Gemma 3 27B", id: "google/gemma-3-27b-it:free" },
{ name: "Gemma 3 4B", id: "google/gemma-3-4b-it:free" },
{ name: "Auto (Best Available)", id: "openrouter/auto" }
];
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => {
scrollToBottom();
}, [messages]);
useEffect(() => {
// Simulate online users update
const interval = setInterval(() => {
setOnlineUsers(Math.floor(Math.random() * 5) + 1);
}, 10000);
return () => clearInterval(interval);
}, []);
useEffect(() => {
// Check API status on component mount
const checkAPIStatus = async () => {
try {
const OPENROUTER_API_KEY = process.env.REACT_APP_OPENROUTER_API_KEY ||
window.OPENROUTER_API_KEY ||
process.env.OPENROUTER_API_KEY;
if (!OPENROUTER_API_KEY) {
setApiStatus('No API Key');
return;
}
const response = await fetch("https://openrouter.ai/api/v1/models", {
headers: { "Authorization": `Bearer ${OPENROUTER_API_KEY}` }
});
setApiStatus(response.ok ? 'Connected' : 'Error');
} catch {
setApiStatus('Error');
}
};
checkAPIStatus();
}, []);
const generateChatTitle = (msgs) => {
if (!msgs || msgs.length === 0) return "New Chat";
const firstUserMessage = msgs.find(m => m.role === 'user');
if (!firstUserMessage) return "New Chat";
const content = firstUserMessage.content;
return content.length > 30 ? content.substring(0, 30) + "..." : content;
};
const startNewChat = () => {
if (messages.length > 0) {
const newSession = {
id: 'session-' + Date.now(),
title: generateChatTitle(messages),
messages: [...messages],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
setSessions(prev => [newSession, ...prev]);
}
setMessages([]);
setCurrentSessionId('session-' + Date.now());
};
const loadSession = (session) => {
if (messages.length > 0) {
const currentSession = {
id: currentSessionId,
title: generateChatTitle(messages),
messages: [...messages],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
setSessions(prev => {
const filtered = prev.filter(s => s.id !== currentSessionId);
return [currentSession, ...filtered];
});
}
setMessages(session.messages);
setCurrentSessionId(session.id);
};
const deleteSession = (sessionId) => {
setSessions(prev => prev.filter(s => s.id !== sessionId));
if (sessionId === currentSessionId) {
startNewChat();
}
};
// OpenRouter API integration
const getAIResponse = async (userMessage) => {
setIsLoading(true);
try {
// Get API key from environment variables (Hugging Face Spaces secrets)
const OPENROUTER_API_KEY = process.env.REACT_APP_OPENROUTER_API_KEY ||
window.OPENROUTER_API_KEY ||
process.env.OPENROUTER_API_KEY;
if (!OPENROUTER_API_KEY) {
throw new Error("No API key found. Please add OPENROUTER_API_KEY to environment variables.");
}
const url = "https://openrouter.ai/api/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENROUTER_API_KEY}`,
"HTTP-Referer": "https://huggingface.co/spaces",
"X-Title": "Chat Flow AI Assistant"
};
// Prepare messages for API
const apiMessages = [
{ role: "system", content: "You are a helpful AI assistant. Provide clear and helpful responses." },
...messages.map(msg => ({
role: msg.role,
content: msg.content.split('\n\n---\n*Response created by:')[0] // Remove attribution from content
})),
{ role: "user", content: userMessage }
];
const data = {
model: selectedModel,
messages: apiMessages,
stream: false, // Set to false for simpler handling in React
max_tokens: 2000,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
};
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
});
if (!response.ok) {
let errorDetail = "";
try {
const errorData = await response.json();
errorDetail = errorData.error?.message || `HTTP ${response.status}`;
} catch {
errorDetail = `HTTP ${response.status}: ${response.statusText}`;
}
throw new Error(`API Error: ${errorDetail}. Please try a different model or check your API key.`);
}
const result = await response.json();
const aiResponse = result.choices[0].message.content;
const selectedModelName = models.find(m => m.id === selectedModel)?.name || "AI";
setIsLoading(false);
return aiResponse + `\n\n---\n*Response created by: **${selectedModelName}***`;
} catch (error) {
setIsLoading(false);
console.error('API Error:', error);
if (error.message.includes('timeout')) {
return "Request timed out. Please try again with a shorter message or different model.";
} else if (error.message.includes('Connection')) {
return "Connection error. Please check your internet connection and try again.";
} else {
return `Error: ${error.message}`;
}
}
};
const handleSendMessage = async (e) => {
e.preventDefault();
if (!currentMessage.trim() || isLoading) return;
const userMessage = {
role: 'user',
content: currentMessage.trim(),
timestamp: new Date().toISOString()
};
setMessages(prev => [...prev, userMessage]);
const messageToSend = currentMessage.trim();
setCurrentMessage('');
try {
const aiResponse = await getAIResponse(messageToSend);
const assistantMessage = {
role: 'assistant',
content: aiResponse,
timestamp: new Date().toISOString()
};
setMessages(prev => [...prev, assistantMessage]);
} catch (error) {
const errorMessage = {
role: 'assistant',
content: 'Sorry, I encountered an error. Please try again.',
timestamp: new Date().toISOString()
};
setMessages(prev => [...prev, errorMessage]);
}
};
const clearChat = () => {
setMessages([]);
};
const downloadHistory = () => {
const dataStr = JSON.stringify(messages, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = `chat_history_${new Date().toISOString().split('T')[0]}.json`;
link.click();
URL.revokeObjectURL(url);
};
const getSelectedModelName = () => {
return models.find(m => m.id === selectedModel)?.name || "GPT-3.5 Turbo";
};
return (
<div className="flex h-screen bg-gray-900 text-white">
{/* Sidebar */}
<div className="w-80 bg-gray-800 border-r border-gray-700 flex flex-col">
{/* Header */}
<div className="p-4 border-b border-gray-700">
<div className="flex items-center gap-3 mb-4">
<div className="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center">
<Bot className="w-5 h-5" />
</div>
<h1 className="text-xl font-semibold">Chat Flow</h1>
</div>
<button
onClick={startNewChat}
className="w-full flex items-center gap-2 px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition-colors"
>
<Plus className="w-4 h-4" />
New Chat
</button>
</div>
{/* Chat History */}
<div className="flex-1 overflow-y-auto p-4">
<h3 className="text-sm font-medium text-gray-400 mb-3">π¬ Chat History</h3>
{sessions.length > 0 ? (
<div className="space-y-2">
{sessions.map((session) => (
<div key={session.id} className="group flex items-center gap-2">
<button
onClick={() => loadSession(session)}
className={`flex-1 text-left px-3 py-2 rounded-lg transition-colors ${
session.id === currentSessionId
? 'bg-blue-600 text-white'
: 'bg-gray-700 hover:bg-gray-600 text-gray-300'
}`}
>
<div className="text-sm font-medium truncate">{session.title}</div>
<div className="text-xs text-gray-400">
{new Date(session.updatedAt).toLocaleDateString()}
</div>
</button>
<button
onClick={() => deleteSession(session.id)}
className="opacity-0 group-hover:opacity-100 p-1 text-gray-400 hover:text-red-400 transition-all"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
))}
</div>
) : (
<p className="text-gray-500 text-sm">No previous chats yet</p>
)}
</div>
{/* Settings */}
<div className="p-4 border-t border-gray-700 space-y-4">
{/* Online Users */}
<div>
<h3 className="text-sm font-medium text-gray-400 mb-2">π₯ Who's Online</h3>
<div className="flex items-center gap-2 text-sm">
<div className="w-2 h-2 bg-green-500 rounded-full"></div>
<span>{onlineUsers === 1 ? 'Just you online' : `${onlineUsers} people online`}</span>
</div>
<p className="text-xs text-gray-500 mt-1">You: {userId}</p>
</div>
{/* Model Selection */}
<div>
<label className="block text-sm font-medium text-gray-400 mb-2">AI Model</label>
<select
value={selectedModel}
onChange={(e) => setSelectedModel(e.target.value)}
className="w-full px-3 py-2 bg-gray-700 border border-gray-600 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
>
{models.map((model) => (
<option key={model.id} value={model.id}>
{model.name}
</option>
))}
</select>
<p className="text-xs text-green-400 mt-1 font-mono">{selectedModel}</p>
</div>
{/* API Status */}
<div className="flex items-center gap-2 text-sm">
<div className={`w-2 h-2 rounded-full ${apiStatus === 'Connected' ? 'bg-green-500' : 'bg-red-500'}`}></div>
<span>{apiStatus === 'Connected' ? 'π’ API Connected' : 'π΄ Connection Issue'}</span>
</div>
{/* Controls */}
<div className="flex gap-2">
<button
onClick={downloadHistory}
className="flex-1 flex items-center justify-center gap-1 px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm"
title="Download History"
>
<Download className="w-4 h-4" />
</button>
<button
onClick={clearChat}
className="flex-1 flex items-center justify-center gap-1 px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm"
title="Clear Chat"
>
<Trash2 className="w-4 h-4" />
</button>
<button
onClick={() => window.location.reload()}
className="flex-1 flex items-center justify-center gap-1 px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm"
title="Refresh"
>
<RefreshCw className="w-4 h-4" />
</button>
</div>
</div>
</div>
{/* Main Chat Area */}
<div className="flex-1 flex flex-col">
{/* Chat Messages */}
<div className="flex-1 overflow-y-auto">
{messages.length === 0 ? (
<div className="h-full flex items-center justify-center">
<div className="text-center max-w-md mx-auto px-4">
<div className="w-16 h-16 bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-6">
<Bot className="w-8 h-8 text-blue-400" />
</div>
<h2 className="text-2xl font-semibold mb-4 text-gray-100">Your personal assistant</h2>
<p className="text-gray-400 leading-relaxed">
A personal assistant streamlines your life by managing tasks, schedules,
and communications efficiently.
</p>
</div>
</div>
) : (
<div className="p-6 space-y-6 max-w-4xl mx-auto">
{messages.map((message, index) => (
<div key={index} className="flex gap-4">
<div className="w-8 h-8 rounded-full bg-gray-700 flex items-center justify-center flex-shrink-0">
{message.role === 'user' ? (
<div className="w-6 h-6 bg-blue-600 rounded-full"></div>
) : (
<Bot className="w-5 h-5 text-blue-400" />
)}
</div>
<div className="flex-1 min-w-0">
<div className="prose prose-invert max-w-none">
{message.role === 'assistant' && message.content.includes('---\n*Response created by:') ? (
<>
<div className="whitespace-pre-wrap text-gray-100">
{message.content.split('\n\n---\n*Response created by:')[0]}
</div>
<div className="text-xs text-gray-500 mt-2 italic">
Response created by: <strong>{message.content.split('**')[1]}</strong>
</div>
</>
) : (
<div className="whitespace-pre-wrap text-gray-100">{message.content}</div>
)}
</div>
</div>
</div>
))}
{isLoading && (
<div className="flex gap-4">
<div className="w-8 h-8 rounded-full bg-gray-700 flex items-center justify-center flex-shrink-0">
<Bot className="w-5 h-5 text-blue-400" />
</div>
<div className="flex-1">
<div className="flex items-center gap-2 text-gray-400">
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"></div>
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{animationDelay: '0.1s'}}></div>
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{animationDelay: '0.2s'}}></div>
</div>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
)}
</div>
{/* Message Input */}
<div className="border-t border-gray-700 p-4">
<div className="max-w-4xl mx-auto">
<div className="flex gap-3">
<div className="flex-1 relative">
<input
type="text"
value={currentMessage}
onChange={(e) => setCurrentMessage(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage(e);
}
}}
placeholder="Chat Smarter. Chat many Brains"
className="w-full px-4 py-3 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
disabled={isLoading}
/>
</div>
<button
onClick={handleSendMessage}
disabled={!currentMessage.trim() || isLoading}
className="px-6 py-3 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg transition-colors flex items-center gap-2"
>
<Send className="w-4 h-4" />
Send
</button>
</div>
<div className="mt-2 text-center">
<span className="text-xs text-gray-500">Currently using: <strong>{getSelectedModelName()}</strong></span>
</div>
</div>
</div>
</div>
</div>
);
};
export default ChatFlowApp; |