Spaces:
Configuration error
Configuration error
new code
Browse files- src/App.js +82 -18
src/App.js
CHANGED
|
@@ -1,25 +1,89 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 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 |
}
|
| 24 |
|
| 25 |
-
export default App;
|
|
|
|
| 1 |
+
import { useState } from "react";
|
| 2 |
+
import "./App.css";
|
| 3 |
|
| 4 |
function App() {
|
| 5 |
+
const [message, setMessage] = useState("");
|
| 6 |
+
const [messages, setMessages] = useState([]);
|
| 7 |
+
|
| 8 |
+
const sendMessage = async () => {
|
| 9 |
+
if (!message.trim()) return;
|
| 10 |
+
|
| 11 |
+
const userMessage = {
|
| 12 |
+
role: "user",
|
| 13 |
+
content: message,
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
const updatedMessages = [...messages, userMessage];
|
| 17 |
+
setMessages(updatedMessages);
|
| 18 |
+
setMessage("");
|
| 19 |
+
|
| 20 |
+
try {
|
| 21 |
+
const response = await fetch(
|
| 22 |
+
"https://router.huggingface.co/v1/chat/completions",
|
| 23 |
+
{
|
| 24 |
+
method: "POST",
|
| 25 |
+
headers: {
|
| 26 |
+
Authorization: `Bearer YOUR_HF_TOKEN`,
|
| 27 |
+
"Content-Type": "application/json",
|
| 28 |
+
},
|
| 29 |
+
body: JSON.stringify({
|
| 30 |
+
model: "openai/gpt-oss-20b",
|
| 31 |
+
messages: updatedMessages,
|
| 32 |
+
max_tokens: 512,
|
| 33 |
+
temperature: 0.7,
|
| 34 |
+
}),
|
| 35 |
+
}
|
| 36 |
+
);
|
| 37 |
+
|
| 38 |
+
const data = await response.json();
|
| 39 |
+
|
| 40 |
+
const botMessage = {
|
| 41 |
+
role: "assistant",
|
| 42 |
+
content: data.choices[0].message.content,
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
setMessages((prev) => [...prev, botMessage]);
|
| 46 |
+
} catch (err) {
|
| 47 |
+
console.error(err);
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
return (
|
| 52 |
+
<div style={{ width: "700px", margin: "40px auto" }}>
|
| 53 |
+
<h2>AI Chatbot</h2>
|
| 54 |
+
|
| 55 |
+
<div
|
| 56 |
+
style={{
|
| 57 |
+
height: "450px",
|
| 58 |
+
border: "1px solid gray",
|
| 59 |
+
overflowY: "auto",
|
| 60 |
+
padding: "10px",
|
| 61 |
+
marginBottom: "10px",
|
| 62 |
+
}}
|
| 63 |
+
>
|
| 64 |
+
{messages.map((msg, index) => (
|
| 65 |
+
<div key={index}>
|
| 66 |
+
<b>{msg.role === "user" ? "You" : "Bot"}:</b> {msg.content}
|
| 67 |
+
</div>
|
| 68 |
+
))}
|
| 69 |
+
</div>
|
| 70 |
+
|
| 71 |
+
<input
|
| 72 |
+
type="text"
|
| 73 |
+
value={message}
|
| 74 |
+
onChange={(e) => setMessage(e.target.value)}
|
| 75 |
+
placeholder="Type a message..."
|
| 76 |
+
style={{ width: "80%", padding: "10px" }}
|
| 77 |
+
/>
|
| 78 |
+
|
| 79 |
+
<button
|
| 80 |
+
onClick={sendMessage}
|
| 81 |
+
style={{ width: "18%", padding: "10px", marginLeft: "2%" }}
|
| 82 |
+
>
|
| 83 |
+
Send
|
| 84 |
+
</button>
|
| 85 |
</div>
|
| 86 |
);
|
| 87 |
}
|
| 88 |
|
| 89 |
+
export default App;
|