Spaces:
Running
Running
Update src/App.js
Browse files- src/App.js +84 -16
src/App.js
CHANGED
|
@@ -1,23 +1,91 @@
|
|
| 1 |
-
import
|
| 2 |
-
import './App.css';
|
| 3 |
|
| 4 |
function App() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
return (
|
| 6 |
-
<div
|
| 7 |
-
<
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
>
|
| 18 |
-
|
| 19 |
-
</
|
| 20 |
-
</
|
| 21 |
</div>
|
| 22 |
);
|
| 23 |
}
|
|
|
|
| 1 |
+
import { useState } from 'react';
|
|
|
|
| 2 |
|
| 3 |
function App() {
|
| 4 |
+
const [messages, setMessages] = useState([]);
|
| 5 |
+
const [input, setInput] = useState('');
|
| 6 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 7 |
+
|
| 8 |
+
// Replace with your model path (e.g., "your-user/model")
|
| 9 |
+
const HF_MODEL_PATH = "your-user/model";
|
| 10 |
+
|
| 11 |
+
const handleSend = async () => {
|
| 12 |
+
if (!input.trim()) return;
|
| 13 |
+
|
| 14 |
+
setIsLoading(true);
|
| 15 |
+
setMessages([...messages, { text: input, sender: 'user' }]);
|
| 16 |
+
setInput('');
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
const response = await fetch(
|
| 20 |
+
`https://api-inference.huggingface.co/models/${HF_MODEL_PATH}`,
|
| 21 |
+
{
|
| 22 |
+
method: 'POST',
|
| 23 |
+
headers: {
|
| 24 |
+
'Content-Type': 'application/json',
|
| 25 |
+
'Authorization': `Bearer ${process.env.HF_TOKEN}`,
|
| 26 |
+
},
|
| 27 |
+
body: JSON.stringify({ inputs: input }),
|
| 28 |
+
}
|
| 29 |
+
);
|
| 30 |
+
|
| 31 |
+
if (!response.ok) throw new Error('Failed to fetch');
|
| 32 |
+
|
| 33 |
+
const data = await response.json();
|
| 34 |
+
// Adjust this based on your model's output format
|
| 35 |
+
const botResponse = data[0]?.generated_text || data[0]?.answer || "Sorry, I couldn't generate a response.";
|
| 36 |
+
|
| 37 |
+
setMessages((prev) => [...prev, { text: botResponse, sender: 'bot' }]);
|
| 38 |
+
} catch (error) {
|
| 39 |
+
setMessages((prev) => [...prev, { text: "Error: Could not get a response.", sender: 'bot' }]);
|
| 40 |
+
} finally {
|
| 41 |
+
setIsLoading(false);
|
| 42 |
+
}
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
return (
|
| 46 |
+
<div style={{ maxWidth: '500px', margin: '0 auto', border: '1px solid #ccc', borderRadius: '8px', padding: '16px' }}>
|
| 47 |
+
<h2>Chatbot</h2>
|
| 48 |
+
<div style={{ height: '300px', overflowY: 'auto', border: '1px solid #eee', padding: '8px', marginBottom: '8px' }}>
|
| 49 |
+
{messages.map((msg, i) => (
|
| 50 |
+
<div key={i} style={{ textAlign: msg.sender === 'user' ? 'right' : 'left', margin: '4px 0' }}>
|
| 51 |
+
<span style={{
|
| 52 |
+
background: msg.sender === 'user' ? '#007bff' : '#f1f1f1',
|
| 53 |
+
color: msg.sender === 'user' ? 'white' : 'black',
|
| 54 |
+
padding: '4px 8px',
|
| 55 |
+
borderRadius: '4px',
|
| 56 |
+
display: 'inline-block'
|
| 57 |
+
}}>
|
| 58 |
+
{msg.text}
|
| 59 |
+
</span>
|
| 60 |
+
</div>
|
| 61 |
+
))}
|
| 62 |
+
{isLoading && <div style={{ textAlign: 'left', margin: '4px 0' }}>Typing...</div>}
|
| 63 |
+
</div>
|
| 64 |
+
<div style={{ display: 'flex' }}>
|
| 65 |
+
<input
|
| 66 |
+
type="text"
|
| 67 |
+
value={input}
|
| 68 |
+
onChange={(e) => setInput(e.target.value)}
|
| 69 |
+
onKeyPress={(e) => e.key === 'Enter' && handleSend()}
|
| 70 |
+
style={{ flex: 1, padding: '8px', border: '1px solid #ccc', borderRadius: '4px' }}
|
| 71 |
+
disabled={isLoading}
|
| 72 |
+
/>
|
| 73 |
+
<button
|
| 74 |
+
onClick={handleSend}
|
| 75 |
+
style={{
|
| 76 |
+
marginLeft: '8px',
|
| 77 |
+
padding: '8px 16px',
|
| 78 |
+
background: '#007bff',
|
| 79 |
+
color: 'white',
|
| 80 |
+
border: 'none',
|
| 81 |
+
borderRadius: '4px',
|
| 82 |
+
cursor: 'pointer'
|
| 83 |
+
}}
|
| 84 |
+
disabled={isLoading}
|
| 85 |
>
|
| 86 |
+
Send
|
| 87 |
+
</button>
|
| 88 |
+
</div>
|
| 89 |
</div>
|
| 90 |
);
|
| 91 |
}
|