Spaces:
Running
Running
Download src/App_old.jsx from DeepThoughtAI/pl_ai_frontend: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/spaces/DeepThoughtAI/pl_ai_frontend/resolve/main/src/App_old.jsx
- Command line
-
hf download hf://spaces/DeepThoughtAI/pl_ai_frontend/src/App_old.jsx
-
curl -L -o App_old.jsx https://huggingface.co/spaces/DeepThoughtAI/pl_ai_frontend/resolve/main/src/App_old.jsx
2.9 kB
| import React, { useState } from "react"; | |
| import { sendChat } from "./api"; | |
| import "./App.css"; | |
| function App() { | |
| const [messages, setMessages] = useState([ | |
| { role: "assistant", text: "Hello! Ask me anything." } | |
| ]); | |
| const [input, setInput] = useState(""); | |
| const [strictMode, setStrictMode] = useState(true); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState(null); | |
| async function handleSend(e) { | |
| if (e) e.preventDefault(); | |
| if (!input.trim() || loading) return; | |
| const userMsg = { role: "user", text: input.trim() }; | |
| const newMessages = [...messages, userMsg]; | |
| setMessages(newMessages); | |
| setInput(""); | |
| setError(null); | |
| setLoading(true); | |
| try { | |
| const historyForBackend = newMessages.map((m) => ({ | |
| role: m.role, | |
| content: m.text, | |
| })); | |
| const response = await sendChat( | |
| userMsg.text, | |
| strictMode, | |
| historyForBackend | |
| ); | |
| const assistantMsg = { | |
| role: "assistant", | |
| text: response.reply, | |
| debug: response.prolog_query_info | |
| }; | |
| setMessages([...newMessages, assistantMsg]); | |
| } catch (err) { | |
| setError(err.message || "Unknown error"); | |
| } finally { | |
| setLoading(false); | |
| } | |
| } | |
| return ( | |
| <div className="app-root"> | |
| <header className="header"> | |
| <h1>🧠 Prolog AI Chatbot</h1> | |
| <label style={{ fontSize: "0.9rem" }}> | |
| <input | |
| type="checkbox" | |
| checked={strictMode} | |
| onChange={(e) => setStrictMode(e.target.checked)} | |
| />{" "} | |
| Strict Mode | |
| </label> | |
| </header> | |
| <main className="main"> | |
| <div className="messages"> | |
| {messages.map((msg, idx) => ( | |
| <Message key={idx} message={msg} /> | |
| ))} | |
| {loading && <div className="assistant bubble">Thinking…</div>} | |
| </div> | |
| {error && <div className="error">Error: {error}</div>} | |
| <form className="input-row" onSubmit={handleSend}> | |
| <textarea | |
| placeholder="Type your question..." | |
| value={input} | |
| onChange={(e) => setInput(e.target.value)} | |
| disabled={loading} | |
| /> | |
| <button type="submit" disabled={loading || !input.trim()}> | |
| Send | |
| </button> | |
| </form> | |
| </main> | |
| </div> | |
| ); | |
| } | |
| function Message({ message }) { | |
| return ( | |
| <div className={`message ${message.role}`}> | |
| <div className="bubble">{message.text}</div> | |
| {message.role === "assistant" && message.debug && ( | |
| <details className="debug"> | |
| <summary>Show Prolog Debug</summary> | |
| <pre>{JSON.stringify(message.debug, null, 2)}</pre> | |
| </details> | |
| )} | |
| </div> | |
| ); | |
| } | |
| export default App; | |