import React, { useState, useRef, useEffect } from 'react'; const ChatInterface = () => { const [messages, setMessages] = useState([ { id: 1, role: 'assistant', content: 'Hello! I am your Dental Fine-Tuning Assistant. How can I help you with dental procedures, oral health, or medical queries today?' } ]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSend = async () => { if (!input.trim()) return; const userMessage = { id: Date.now(), role: 'user', content: input }; setMessages((prev) => [...prev, userMessage]); setInput(''); setIsLoading(true); try { // Simulating API call to MedGemma // In production, replace this with your actual API endpoint const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ prompt: input }), }); const data = await response.json(); if (data.reply) { setMessages((prev) => [...prev, { id: Date.now() + 1, role: 'assistant', content: data.reply }]); } else { setMessages((prev) => [...prev, { id: Date.now() + 1, role: 'assistant', content: 'I apologize, but I am unable to generate a response right now.' }]); } } catch (error) { console.error('Error:', error); setMessages((prev) => [ ...prev, { id: Date.now() + 1, role: 'assistant', content: 'Sorry, there was an error connecting to the server.' } ]); } finally { setIsLoading(false); } }; const handleKeyPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; return (
Powered by MedGemma
{message.role === 'user' ? 'You' : 'Dental Assistant'}
{message.content}