Spaces:
Sleeping
Sleeping
File size: 7,322 Bytes
eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e 8ac59dc eef335e |
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 |
import { useState, useEffect, useRef } from 'react'
import ChatMessage from '../components/ChatMessage'
import ChatInput from '../components/ChatInput'
export default function Home() {
const [messages, setMessages] = useState([
{
id: 1,
role: 'assistant',
content: 'Hello! I\'m an AI assistant powered by Qwen Vision Language model. I can understand both text and images. You can upload images and ask me questions about them, or just chat with me! How can I help you today?',
timestamp: new Date().toISOString()
}
])
const [isLoading, setIsLoading] = useState(false)
const [isTyping, setIsTyping] = useState(false)
const messagesEndRef = useRef(null)
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
}
useEffect(() => {
scrollToBottom()
}, [messages])
const handleSendMessage = async (messageData) => {
const userMessage = {
id: Date.now(),
role: 'user',
content: messageData.content,
image: messageData.image,
timestamp: new Date().toISOString()
}
setMessages(prev => [...prev, userMessage])
setIsLoading(true)
setIsTyping(true)
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages: [...messages, userMessage]
}),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const reader = response.body.getReader()
const decoder = new TextDecoder()
let assistantMessage = {
id: Date.now() + 1,
role: 'assistant',
content: '',
timestamp: new Date().toISOString()
}
setMessages(prev => [...prev, assistantMessage])
while (true) {
const { done, value } = await reader.read()
if (done) break
const chunk = decoder.decode(value)
const lines = chunk.split('\n')
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6)
if (data === '[DONE]') {
setIsTyping(false)
break
}
try {
const parsed = JSON.parse(data)
if (parsed.content) {
setMessages(prev => {
const updated = [...prev]
updated[updated.length - 1] = {
...updated[updated.length - 1],
content: updated[updated.length - 1].content + parsed.content
}
return updated
})
} else if (parsed.error) {
throw new Error(parsed.error)
}
} catch (e) {
console.warn('Failed to parse chunk:', data)
}
}
}
}
} catch (error) {
console.error('Chat error:', error)
setMessages(prev => [...prev, {
id: Date.now() + 1,
role: 'assistant',
content: `Sorry, I encountered an error: ${error.message}. Please make sure your Hugging Face token is properly configured and try again.`,
timestamp: new Date().toISOString()
}])
} finally {
setIsLoading(false)
setIsTyping(false)
}
}
const clearChat = () => {
setMessages([
{
id: 1,
role: 'assistant',
content: 'Hello! I\'m an AI assistant powered by Qwen Vision Language model. I can understand both text and images. You can upload images and ask me questions about them, or just chat with me! How can I help you today?',
timestamp: new Date().toISOString()
}
])
}
return (
<div className="min-h-screen flex flex-col bg-black">
{/* Header */}
<header className="apple-glass border-b border-white border-opacity-10 p-6">
<div className="max-w-4xl mx-auto flex items-center justify-between">
<div>
<h1 className="text-2xl font-semibold text-white tracking-tight">
AI Vision Chat
</h1>
<p className="text-sm text-gray-400 mt-1 font-medium">
Powered by Qwen3-VL-8B-Instruct
</p>
</div>
<div className="flex items-center gap-3">
<button
onClick={clearChat}
className="px-4 py-2 text-sm font-medium text-gray-300 hover:text-white apple-button disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200"
disabled={isLoading}
>
Clear Chat
</button>
<a
href="https://huggingface.co/spaces/akhaliq/anycoder"
target="_blank"
rel="noopener noreferrer"
className="text-sm text-blue-400 hover:text-blue-300 transition-colors font-medium"
>
Built with anycoder
</a>
</div>
</div>
</header>
{/* Chat Messages */}
<div className="flex-1 overflow-hidden">
<div className="max-w-4xl mx-auto h-full px-6 py-8">
<div className="h-full overflow-y-auto pr-2">
<div className="space-y-8">
{messages.map((message, index) => (
<ChatMessage key={message.id} message={message} index={index} />
))}
{isTyping && (
<div className="flex gap-4 mb-8 animate-fade-in">
<div className="flex-shrink-0">
<div
className="w-10 h-10 rounded-full flex items-center justify-center text-white font-medium text-sm bg-gradient-to-r from-gray-600 to-gray-700 shadow-lg"
>
AI
</div>
</div>
<div className="chat-message assistant">
<div className="flex items-center gap-3">
<div className="flex gap-1">
<div className="w-1.5 h-1.5 bg-gray-400 rounded-full animate-pulse"></div>
<div className="w-1.5 h-1.5 bg-gray-400 rounded-full animate-pulse" style={{ animationDelay: '0.2s' }}></div>
<div className="w-1.5 h-1.5 bg-gray-400 rounded-full animate-pulse" style={{ animationDelay: '0.4s' }}></div>
</div>
<span className="text-xs text-gray-400 font-medium">AI is typing...</span>
</div>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
</div>
</div>
</div>
{/* Input Area */}
<div className="max-w-4xl mx-auto w-full px-6 pb-8">
<ChatInput
onSendMessage={handleSendMessage}
disabled={isLoading || isTyping}
/>
<div className="mt-4 text-center">
<p className="text-xs text-gray-500 font-medium">
Upload images to ask questions about them, or just chat!
<span className="text-blue-400 ml-1">Powered by Hugging Face</span>
</p>
</div>
</div>
</div>
)
} |