beeguy commited on
Commit
27f4327
·
1 Parent(s): 388bac8

chat endpoint

Browse files
Files changed (3) hide show
  1. app.py +8 -0
  2. static/index.html +8 -0
  3. static/script.js +44 -1
app.py CHANGED
@@ -18,6 +18,14 @@ async def receive_embeddings(request: Request):
18
  # Process the embeddings as needed
19
  return {"status": "OK"}
20
 
 
 
 
 
 
 
 
 
21
  @app.get("/embeddings")
22
  def get_embeddings(input: str):
23
  result = embeddings.embed_query(input)
 
18
  # Process the embeddings as needed
19
  return {"status": "OK"}
20
 
21
+ @app.post("/chat")
22
+ async def chat(request: Request):
23
+ data = await request.json()
24
+ message = data.get("message")
25
+ # Process the message and generate a reply
26
+ reply = f"Received your message: {message}"
27
+ return {"reply": reply}
28
+
29
  @app.get("/embeddings")
30
  def get_embeddings(input: str):
31
  result = embeddings.embed_query(input)
static/index.html CHANGED
@@ -31,6 +31,14 @@
31
  <p class="text-gen-output"></p>
32
  </form>
33
  </section>
 
 
 
 
 
 
 
 
34
  </main>
35
  </body>
36
 
 
31
  <p class="text-gen-output"></p>
32
  </form>
33
  </section>
34
+ <section id="chat">
35
+ <h2>Chat with the Model</h2>
36
+ <div id="chat-box"></div>
37
+ <form id="chat-form">
38
+ <input id="chat-input" type="text" placeholder="Type your message here..." />
39
+ <button type="submit">Send</button>
40
+ </form>
41
+ </section>
42
  </main>
43
  </body>
44
 
static/script.js CHANGED
@@ -25,7 +25,9 @@ textGenForm.addEventListener('submit', async (event) => {
25
  });
26
 
27
  const downloadButton = document.getElementById('download-embeddings');
 
28
  const uploadButton = document.getElementById('upload-embeddings');
 
29
  const fileInput = document.getElementById('file-input');
30
 
31
  const updateDownloadButtonState = () => {
@@ -54,6 +56,10 @@ fileInput.addEventListener('change', async (event) => {
54
  try {
55
  const embeddings = JSON.parse(e.target.result);
56
  embeddingsList = embeddings; // Store uploaded embeddings in the variable
 
 
 
 
57
  updateDownloadButtonState(); // Update button state
58
  // Optionally, you can send the embeddings to the server
59
  await fetch('/receive-embeddings', {
@@ -75,4 +81,41 @@ downloadButton.addEventListener('click', downloadEmbeddings);
75
  uploadButton.addEventListener('click', uploadEmbeddings);
76
 
77
  // Initialize button state
78
- updateDownloadButtonState();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  });
26
 
27
  const downloadButton = document.getElementById('download-embeddings');
28
+
29
  const uploadButton = document.getElementById('upload-embeddings');
30
+
31
  const fileInput = document.getElementById('file-input');
32
 
33
  const updateDownloadButtonState = () => {
 
56
  try {
57
  const embeddings = JSON.parse(e.target.result);
58
  embeddingsList = embeddings; // Store uploaded embeddings in the variable
59
+
60
+ const textGenParagraph = document.querySelector('.text-gen-output');
61
+ textGenParagraph.textContent = JSON.stringify(embeddingsList);
62
+
63
  updateDownloadButtonState(); // Update button state
64
  // Optionally, you can send the embeddings to the server
65
  await fetch('/receive-embeddings', {
 
81
  uploadButton.addEventListener('click', uploadEmbeddings);
82
 
83
  // Initialize button state
84
+ updateDownloadButtonState();
85
+
86
+ const chatForm = document.getElementById('chat-form');
87
+ const chatInput = document.getElementById('chat-input');
88
+ const chatBox = document.getElementById('chat-box');
89
+
90
+ const sendMessage = async (message) => {
91
+ const response = await fetch('/chat', {
92
+ method: 'POST',
93
+ headers: {
94
+ 'Content-Type': 'application/json'
95
+ },
96
+ body: JSON.stringify({ message })
97
+ });
98
+ const data = await response.json();
99
+ return data.reply;
100
+ };
101
+
102
+ chatForm.addEventListener('submit', async (event) => {
103
+ event.preventDefault();
104
+ const userMessage = chatInput.value;
105
+ if (userMessage.trim() === '') return;
106
+
107
+ const userMessageElement = document.createElement('div');
108
+ userMessageElement.textContent = `You: ${userMessage}`;
109
+ chatBox.appendChild(userMessageElement);
110
+
111
+ chatInput.value = '';
112
+
113
+ try {
114
+ const reply = await sendMessage(userMessage);
115
+ const replyMessageElement = document.createElement('div');
116
+ replyMessageElement.textContent = `Bot: ${reply}`;
117
+ chatBox.appendChild(replyMessageElement);
118
+ } catch (err) {
119
+ console.error('Error sending message:', err);
120
+ }
121
+ });