Spaces:
Build error
Build error
Upload pages/index.js with huggingface_hub
Browse files- pages/index.js +89 -0
pages/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import ChatMessage from '../components/ChatMessage';
|
| 3 |
+
import InputArea from '../components/InputArea';
|
| 4 |
+
|
| 5 |
+
export default function Home() {
|
| 6 |
+
const [messages, setMessages] = useState([]);
|
| 7 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 8 |
+
const [error, setError] = useState(null);
|
| 9 |
+
|
| 10 |
+
const API_URL = 'http://localhost:8000/api/v1/chat'; // آدرس endpoint
|
| 11 |
+
|
| 12 |
+
const handleSend = async (message) => {
|
| 13 |
+
setIsLoading(true);
|
| 14 |
+
setError(null);
|
| 15 |
+
const userMessage = { type: 'user', content: message };
|
| 16 |
+
setMessages((prev) => [...prev, userMessage]);
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
const res = await fetch(API_URL, {
|
| 20 |
+
method: 'POST',
|
| 21 |
+
headers: { 'Content-Type': 'application/json' },
|
| 22 |
+
body: JSON.stringify({
|
| 23 |
+
message: message,
|
| 24 |
+
user_id: 1,
|
| 25 |
+
}),
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
| 29 |
+
const data = await res.json();
|
| 30 |
+
setMessages((prev) => [...prev, { type: 'bot', ...data }]);
|
| 31 |
+
} catch (e) {
|
| 32 |
+
setError(e.message);
|
| 33 |
+
setMessages((prev) => [
|
| 34 |
+
...prev,
|
| 35 |
+
{ type: 'bot', content: `❌ خطا در ارسال پیام: ${e.message}` },
|
| 36 |
+
]);
|
| 37 |
+
} finally {
|
| 38 |
+
setIsLoading(false);
|
| 39 |
+
}
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
return (
|
| 43 |
+
<div className="min-h-screen bg-gray-100 flex flex-col items-center py-10 px-4">
|
| 44 |
+
{/* Header */}
|
| 45 |
+
<div className="w-full max-w-4xl mb-8 text-center">
|
| 46 |
+
<h1 className="text-3xl md:text-4xl font-extrabold text-gray-800 mb-2">
|
| 47 |
+
🧠 Enterprise Search Chat
|
| 48 |
+
</h1>
|
| 49 |
+
<p className="text-gray-600">چت هوشمند جستجوی تجاری</p>
|
| 50 |
+
</div>
|
| 51 |
+
|
| 52 |
+
{/* Chat Container */}
|
| 53 |
+
<div className="w-full max-w-4xl bg-white rounded-xl shadow-lg overflow-hidden flex flex-col h-[70vh]">
|
| 54 |
+
{/* Messages Area */}
|
| 55 |
+
<div className="flex-1 overflow-y-auto p-6 space-y-4 bg-gray-50">
|
| 56 |
+
{messages.length === 0 && (
|
| 57 |
+
<div className="text-center text-gray-400 py-10">
|
| 58 |
+
<p className="text-lg">پیامی موجود نیست. شروع کنید!</p>
|
| 59 |
+
<p className="text-sm mt-2">مثال: برند اپکس بالای 5 میلیون</p>
|
| 60 |
+
</div>
|
| 61 |
+
)}
|
| 62 |
+
{messages.map((msg, idx) => (
|
| 63 |
+
<ChatMessage key={idx} {...msg} />
|
| 64 |
+
))}
|
| 65 |
+
{isLoading && (
|
| 66 |
+
<div className="text-center text-gray-500 py-4 animate-pulse">
|
| 67 |
+
<p>در حال پردازش...</p>
|
| 68 |
+
</div>
|
| 69 |
+
)}
|
| 70 |
+
{error && (
|
| 71 |
+
<div className="bg-red-100 text-red-700 p-3 rounded-lg text-center">
|
| 72 |
+
{error}
|
| 73 |
+
</div>
|
| 74 |
+
)}
|
| 75 |
+
</div>
|
| 76 |
+
|
| 77 |
+
{/* Input Area */}
|
| 78 |
+
<div className="p-4 bg-white border-t border-gray-200">
|
| 79 |
+
<InputArea onSend={handleSend} />
|
| 80 |
+
</div>
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
{/* Footer */}
|
| 84 |
+
<div className="mt-6 text-center">
|
| 85 |
+
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" rel="noreferrer" className="text-blue-600 hover:text-blue-800 font-bold underline">anyCoder</a>
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
);
|
| 89 |
+
}
|