nroggendorff commited on
Commit
8bdccc6
1 Parent(s): 6e71a5c

Create client.js

Browse files
Files changed (1) hide show
  1. public/client.js +32 -0
public/client.js ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Client } from "@gradio/client";
2
+
3
+ document.addEventListener("DOMContentLoaded", async () => {
4
+ const messagesDiv = document.getElementById("messages");
5
+ const chatForm = document.getElementById("chat-form");
6
+ const userInput = document.getElementById("user-input");
7
+
8
+ const client = await Client.connect("nroggendorff/not-my-emma");
9
+
10
+ chatForm.addEventListener("submit", async (e) => {
11
+ e.preventDefault();
12
+ const userMessage = userInput.value;
13
+ if (!userMessage) return;
14
+
15
+ addMessage(userMessage, "user");
16
+
17
+ const result = await client.predict("/chat", {
18
+ message: userMessage
19
+ });
20
+
21
+ addMessage(result.data, "ai");
22
+ userInput.value = "";
23
+ });
24
+
25
+ function addMessage(message, sender) {
26
+ const messageElement = document.createElement("div");
27
+ messageElement.classList.add("message", sender);
28
+ messageElement.textContent = message;
29
+ messagesDiv.appendChild(messageElement);
30
+ messagesDiv.scrollTop = messagesDiv.scrollHeight;
31
+ }
32
+ });