VarunKumarGupta2003 commited on
Commit
46c244e
·
verified ·
1 Parent(s): 369011b

new patch

Browse files
Files changed (1) hide show
  1. src/App.js +81 -16
src/App.js CHANGED
@@ -4,9 +4,10 @@ import "./App.css";
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",
@@ -14,8 +15,10 @@ function App() {
14
  };
15
 
16
  const updatedMessages = [...messages, userMessage];
 
17
  setMessages(updatedMessages);
18
  setMessage("");
 
19
 
20
  try {
21
  const response = await fetch(
@@ -23,7 +26,7 @@ function App() {
23
  {
24
  method: "POST",
25
  headers: {
26
- Authorization: `Bearer YOUR_HF_TOKEN`,
27
  "Content-Type": "application/json",
28
  },
29
  body: JSON.stringify({
@@ -35,52 +38,114 @@ function App() {
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
  );
 
4
  function App() {
5
  const [message, setMessage] = useState("");
6
  const [messages, setMessages] = useState([]);
7
+ const [loading, setLoading] = useState(false);
8
 
9
  const sendMessage = async () => {
10
+ if (!message.trim() || loading) return;
11
 
12
  const userMessage = {
13
  role: "user",
 
15
  };
16
 
17
  const updatedMessages = [...messages, userMessage];
18
+
19
  setMessages(updatedMessages);
20
  setMessage("");
21
+ setLoading(true);
22
 
23
  try {
24
  const response = await fetch(
 
26
  {
27
  method: "POST",
28
  headers: {
29
+ Authorization: `Bearer ${process.env.REACT_APP_HF_TOKEN}`,
30
  "Content-Type": "application/json",
31
  },
32
  body: JSON.stringify({
 
38
  }
39
  );
40
 
41
+ if (!response.ok) {
42
+ const error = await response.text();
43
+ throw new Error(error);
44
+ }
45
+
46
  const data = await response.json();
47
 
48
  const botMessage = {
49
  role: "assistant",
50
+ content:
51
+ data.choices?.[0]?.message?.content ||
52
+ "Sorry, I couldn't generate a response.",
53
  };
54
 
55
  setMessages((prev) => [...prev, botMessage]);
56
+ } catch (error) {
57
+ console.error(error);
58
+
59
+ setMessages((prev) => [
60
+ ...prev,
61
+ {
62
+ role: "assistant",
63
+ content: "❌ Error: Unable to connect to Hugging Face API.",
64
+ },
65
+ ]);
66
+ } finally {
67
+ setLoading(false);
68
+ }
69
+ };
70
+
71
+ const handleKeyDown = (e) => {
72
+ if (e.key === "Enter") {
73
+ sendMessage();
74
  }
75
  };
76
 
77
  return (
78
+ <div
79
+ style={{
80
+ width: "700px",
81
+ margin: "40px auto",
82
+ fontFamily: "Arial",
83
+ }}
84
+ >
85
+ <h2>🤖 AI Chatbot</h2>
86
 
87
  <div
88
  style={{
89
  height: "450px",
90
+ border: "1px solid #ccc",
91
+ borderRadius: "8px",
92
  overflowY: "auto",
93
+ padding: "15px",
94
+ marginBottom: "15px",
95
+ background: "#f8f8f8",
96
  }}
97
  >
98
+ {messages.length === 0 && (
99
+ <p style={{ color: "gray" }}>Start a conversation...</p>
100
+ )}
101
+
102
  {messages.map((msg, index) => (
103
+ <div
104
+ key={index}
105
+ style={{
106
+ marginBottom: "12px",
107
+ textAlign: msg.role === "user" ? "right" : "left",
108
+ }}
109
+ >
110
+ <strong>
111
+ {msg.role === "user" ? "You" : "AI"}
112
+ </strong>
113
+ <br />
114
+ {msg.content}
115
  </div>
116
  ))}
117
+
118
+ {loading && (
119
+ <p>
120
+ <strong>AI:</strong> Thinking...
121
+ </p>
122
+ )}
123
  </div>
124
 
125
  <input
126
  type="text"
127
+ placeholder="Type your message..."
128
  value={message}
129
  onChange={(e) => setMessage(e.target.value)}
130
+ onKeyDown={handleKeyDown}
131
+ style={{
132
+ width: "80%",
133
+ padding: "12px",
134
+ fontSize: "16px",
135
+ }}
136
  />
137
 
138
  <button
139
  onClick={sendMessage}
140
+ disabled={loading}
141
+ style={{
142
+ width: "18%",
143
+ padding: "12px",
144
+ marginLeft: "2%",
145
+ cursor: "pointer",
146
+ }}
147
  >
148
+ {loading ? "..." : "Send"}
149
  </button>
150
  </div>
151
  );