tumwesigeibra commited on
Commit
c7f6195
·
verified ·
1 Parent(s): 8567ba1

Upload 2 files

Browse files
frontend/frontend/public/index.html ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title>Disease Q&A Chatbot</title>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ </head>
8
+ <body>
9
+ <div id="root"></div>
10
+ </body>
11
+ </html>
frontend/frontend/src/index.js ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import ReactDOM from "react-dom/client";
3
+
4
+ function App() {
5
+ const [input, setInput] = useState("");
6
+ const [chat, setChat] = useState([]);
7
+ const [loading, setLoading] = useState(false);
8
+ const [loggedIn, setLoggedIn] = useState(false);
9
+ const [auth, setAuth] = useState({ username: "", password: "" });
10
+ const [sessionId, setSessionId] = useState(() => crypto.randomUUID());
11
+ const API_TOKEN = "supersecret";
12
+
13
+ const login = async () => {
14
+ const res = await fetch("/api/login", {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify(auth),
18
+ });
19
+ const data = await res.json();
20
+ if (data.message === "Login successful") setLoggedIn(true);
21
+ else alert(data.error || "Login failed.");
22
+ };
23
+
24
+ const register = async () => {
25
+ const res = await fetch("/api/register", {
26
+ method: "POST",
27
+ headers: { "Content-Type": "application/json" },
28
+ body: JSON.stringify(auth),
29
+ });
30
+ const data = await res.json();
31
+ if (data.message === "User registered successfully") alert("Registered!");
32
+ else alert(data.error || "Registration failed.");
33
+ };
34
+
35
+ const sendMessage = async () => {
36
+ if (!input.trim()) return;
37
+ setChat((prev) => [...prev, { sender: "You", text: input }]);
38
+ setInput("");
39
+ setLoading(true);
40
+ try {
41
+ const response = await fetch("/api/chat", {
42
+ method: "POST",
43
+ headers: {
44
+ "Authorization": `Bearer ${API_TOKEN}`,
45
+ "Content-Type": "application/json"
46
+ },
47
+ body: JSON.stringify({
48
+ message: input,
49
+ model_choice: "TinyLLaMA (1.1B)",
50
+ session_id: sessionId
51
+ })
52
+ });
53
+ const data = await response.json();
54
+ setChat((prev) => [...prev, { sender: "Bot", text: data.answer || data.error }]);
55
+ } catch {
56
+ setChat((prev) => [...prev, { sender: "Bot", text: "❌ Server error" }]);
57
+ }
58
+ setLoading(false);
59
+ };
60
+
61
+ const resetSession = async () => {
62
+ await fetch("/api/reset", {
63
+ method: "POST",
64
+ headers: {
65
+ "Authorization": `Bearer ${API_TOKEN}`,
66
+ "Content-Type": "application/json"
67
+ },
68
+ body: JSON.stringify({ session_id: sessionId })
69
+ });
70
+ setChat([]);
71
+ setSessionId(crypto.randomUUID());
72
+ };
73
+
74
+ if (!loggedIn) {
75
+ return (
76
+ <div style={{ padding: "2rem", textAlign: "center" }}>
77
+ <h2>Login</h2>
78
+ <input placeholder="Username" onChange={e => setAuth({ ...auth, username: e.target.value })} /><br />
79
+ <input type="password" placeholder="Password" onChange={e => setAuth({ ...auth, password: e.target.value })} /><br />
80
+ <button onClick={login}>Login</button> <button onClick={register}>Register</button>
81
+ </div>
82
+ );
83
+ }
84
+
85
+ return (
86
+ <div style={{ padding: "2rem", maxWidth: "600px", margin: "auto" }}>
87
+ <h2>Disease Q&A Chatbot</h2>
88
+ <div style={{ border: "1px solid #ccc", height: "300px", overflowY: "auto", padding: "1rem", background: "#f9f9f9" }}>
89
+ {chat.map((msg, i) => <p key={i}><strong>{msg.sender}:</strong> {msg.text}</p>)}
90
+ {loading && <p><em>Thinking...</em></p>}
91
+ </div>
92
+ <input value={input} onChange={e => setInput(e.target.value)} onKeyDown={e => e.key === 'Enter' && sendMessage()} placeholder="Ask something..." style={{ width: "80%", padding: "0.5rem" }} />
93
+ <button onClick={sendMessage}>Send</button>
94
+ <br />
95
+ <button onClick={resetSession} style={{ marginTop: "1rem" }}>Reset Session</button>
96
+ </div>
97
+ );
98
+ }
99
+
100
+ const root = ReactDOM.createRoot(document.getElementById("root"));
101
+ root.render(<App />);