File size: 6,086 Bytes
81cd697
 
 
5a2c1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81cd697
cc3ff6c
5a2c1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc3ff6c
5a2c1f1
cc3ff6c
 
5a2c1f1
cc3ff6c
5a2c1f1
 
 
eb3cc5e
cc3ff6c
5a2c1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81cd697
b56c516
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Serenity - Emotional Support Companion</title>
  <style>
    body {
      background: #f4f7fb;
      font-family: "Inter", sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
      height: 100vh;
      color: #222;
    }
    header {
      background: #5b6ef5;
      color: #fff;
      text-align: center;
      padding: 1rem;
      font-size: 1.3rem;
      font-weight: 600;
      letter-spacing: 0.5px;
    }
    #chat-container {
      flex: 1;
      overflow-y: auto;
      padding: 1rem;
      display: flex;
      flex-direction: column;
    }
    .message {
      margin: 0.6rem 0;
      max-width: 75%;
      padding: 0.7rem 1rem;
      border-radius: 12px;
      line-height: 1.5;
      animation: fadeIn 0.3s ease;
      word-wrap: break-word;
    }
    .user {
      align-self: flex-end;
      background: #dbe2ff;
    }
    .bot {
      align-self: flex-start;
      background: #fff;
      border: 1px solid #e0e0e0;
    }
    .meta {
      font-size: 0.7rem;
      color: #777;
      margin-top: 0.2rem;
      text-align: right;
    }
    #input-container {
      display: flex;
      padding: 0.7rem;
      background: #fff;
      border-top: 1px solid #ddd;
    }
    #user-input {
      flex: 1;
      border: 1px solid #ccc;
      border-radius: 10px;
      padding: 0.6rem 0.8rem;
      font-size: 1rem;
    }
    button {
      margin-left: 0.5rem;
      background: #5b6ef5;
      color: #fff;
      border: none;
      padding: 0.6rem 1rem;
      border-radius: 8px;
      cursor: pointer;
      transition: background 0.2s;
    }
    button:hover {
      background: #4d5fe3;
    }
    #controls {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0.5rem 1rem;
      background: #f0f2ff;
      border-bottom: 1px solid #ddd;
      font-size: 0.9rem;
    }
    select {
      border: 1px solid #ccc;
      border-radius: 8px;
      padding: 0.3rem 0.6rem;
    }
    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(6px); }
      to { opacity: 1; transform: translateY(0); }
    }
  </style>
</head>
<body>
  <header>🪷 Serenity – Your Emotional Support Companion</header>

  <div id="controls">
    <div>
      <label for="voice-profile">Voice/Personality: </label>
      <select id="voice-profile">
        <option value="neutral">Neutral (balanced)</option>
        <option value="calm_male">Calm Male</option>
        <option value="deep_male">Deep Male</option>
        <option value="soothing_male">Soothing Male</option>
        <option value="gentle_female">Gentle Female</option>
        <option value="feminine_female">Feminine Female</option>
        <option value="deep_female">Deep Female</option>
        <option value="soothing_female">Soothing Female</option>
      </select>
    </div>
    <button id="reset">Reset Chat</button>
  </div>

  <div id="chat-container"></div>

  <div id="input-container">
    <input id="user-input" type="text" placeholder="Type your message..." />
    <button id="send">Send</button>
  </div>

  <script>
    const chatContainer = document.getElementById("chat-container");
    const input = document.getElementById("user-input");
    const sendBtn = document.getElementById("send");
    const voiceSelect = document.getElementById("voice-profile");
    const resetBtn = document.getElementById("reset");

    let session = localStorage.getItem("session_id");
    if (!session) {
      session = "sess_" + Math.random().toString(36).substring(2, 10);
      localStorage.setItem("session_id", session);
    }

    async function appendMessage(sender, text, emotion, intent) {
      const div = document.createElement("div");
      div.classList.add("message", sender);
      div.textContent = text;
      chatContainer.appendChild(div);
      if (emotion || intent) {
        const meta = document.createElement("div");
        meta.classList.add("meta");
        meta.textContent = `(${intent || "..."}, ${emotion || "..."})`;
        div.appendChild(meta);
      }
      chatContainer.scrollTop = chatContainer.scrollHeight;
    }

    async function sendMessage() {
      const text = input.value.trim();
      if (!text) return;
      appendMessage("user", text);
      input.value = "";

      const body = {
        session: session,
        message: text,
        personality: voiceSelect.value
      };

      try {
        const res = await fetch("/chat", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(body)
        });
        const data = await res.json();
        appendMessage("bot", data.reply, data.emotion, data.intent);
      } catch (err) {
        appendMessage("bot", "Sorry, I had trouble connecting. Please try again.");
      }
    }

    async function initChat() {
      const res = await fetch("/chat", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ session: session, init: true, personality: voiceSelect.value })
      });
      const data = await res.json();
      appendMessage("bot", data.reply, data.emotion, data.intent);
    }

    async function resetChat() {
      await fetch("/reset_session", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ session })
      });
      localStorage.removeItem("session_id");
      session = "sess_" + Math.random().toString(36).substring(2, 10);
      localStorage.setItem("session_id", session);
      chatContainer.innerHTML = "";
      appendMessage("bot", "🔄 Chat reset. Let's start fresh!");
      initChat();
    }

    sendBtn.addEventListener("click", sendMessage);
    input.addEventListener("keypress", (e) => {
      if (e.key === "Enter") sendMessage();
    });
    resetBtn.addEventListener("click", resetChat);

    initChat();
  </script>
</body>
</html>