File size: 3,445 Bytes
369011b
 
93e7522
 
369011b
 
46c244e
369011b
 
46c244e
369011b
 
 
 
 
 
 
46c244e
369011b
 
46c244e
369011b
 
 
 
 
 
 
46c244e
369011b
 
 
 
 
 
 
 
 
 
 
46c244e
 
 
 
 
369011b
 
 
 
46c244e
 
 
369011b
 
 
46c244e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369011b
 
 
93e7522
46c244e
 
 
 
 
 
 
 
369011b
 
 
 
46c244e
 
369011b
46c244e
 
 
369011b
 
46c244e
 
 
 
369011b
46c244e
 
 
 
 
 
 
 
 
 
 
 
369011b
 
46c244e
 
 
 
 
 
369011b
 
 
 
46c244e
369011b
 
46c244e
 
 
 
 
 
369011b
 
 
 
46c244e
 
 
 
 
 
 
369011b
46c244e
369011b
93e7522
 
 
 
369011b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useState } from "react";
import "./App.css";

function App() {
  const [message, setMessage] = useState("");
  const [messages, setMessages] = useState([]);
  const [loading, setLoading] = useState(false);

  const sendMessage = async () => {
    if (!message.trim() || loading) return;

    const userMessage = {
      role: "user",
      content: message,
    };

    const updatedMessages = [...messages, userMessage];

    setMessages(updatedMessages);
    setMessage("");
    setLoading(true);

    try {
      const response = await fetch(
        "https://router.huggingface.co/v1/chat/completions",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${process.env.REACT_APP_HF_TOKEN}`,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            model: "openai/gpt-oss-20b",
            messages: updatedMessages,
            max_tokens: 512,
            temperature: 0.7,
          }),
        }
      );

      if (!response.ok) {
        const error = await response.text();
        throw new Error(error);
      }

      const data = await response.json();

      const botMessage = {
        role: "assistant",
        content:
          data.choices?.[0]?.message?.content ||
          "Sorry, I couldn't generate a response.",
      };

      setMessages((prev) => [...prev, botMessage]);
    } catch (error) {
      console.error(error);

      setMessages((prev) => [
        ...prev,
        {
          role: "assistant",
          content: "❌ Error: Unable to connect to Hugging Face API.",
        },
      ]);
    } finally {
      setLoading(false);
    }
  };

  const handleKeyDown = (e) => {
    if (e.key === "Enter") {
      sendMessage();
    }
  };

  return (
    <div
      style={{
        width: "700px",
        margin: "40px auto",
        fontFamily: "Arial",
      }}
    >
      <h2>🤖 AI Chatbot</h2>

      <div
        style={{
          height: "450px",
          border: "1px solid #ccc",
          borderRadius: "8px",
          overflowY: "auto",
          padding: "15px",
          marginBottom: "15px",
          background: "#f8f8f8",
        }}
      >
        {messages.length === 0 && (
          <p style={{ color: "gray" }}>Start a conversation...</p>
        )}

        {messages.map((msg, index) => (
          <div
            key={index}
            style={{
              marginBottom: "12px",
              textAlign: msg.role === "user" ? "right" : "left",
            }}
          >
            <strong>
              {msg.role === "user" ? "You" : "AI"}
            </strong>
            <br />
            {msg.content}
          </div>
        ))}

        {loading && (
          <p>
            <strong>AI:</strong> Thinking...
          </p>
        )}
      </div>

      <input
        type="text"
        placeholder="Type your message..."
        value={message}
        onChange={(e) => setMessage(e.target.value)}
        onKeyDown={handleKeyDown}
        style={{
          width: "80%",
          padding: "12px",
          fontSize: "16px",
        }}
      />

      <button
        onClick={sendMessage}
        disabled={loading}
        style={{
          width: "18%",
          padding: "12px",
          marginLeft: "2%",
          cursor: "pointer",
        }}
      >
        {loading ? "..." : "Send"}
      </button>
    </div>
  );
}

export default App;