File size: 1,633 Bytes
1e28d04
2a5e5fa
9a6620f
1e28d04
26790f6
9a6620f
26790f6
 
 
 
1e28d04
1a5c000
1e28d04
 
 
 
 
9a6620f
 
a3ccca5
 
 
 
 
 
6942628
05dc8f9
 
 
a3ccca5
 
a05f54e
a3ccca5
 
 
 
239df0b
9e7b790
a3ccca5
 
9e7b790
 
 
 
 
a3ccca5
 
 
9e7b790
 
 
 
a3ccca5
 
 
 
 
 
 
26790f6
 
 
 
 
 
a3ccca5
1e28d04
 
 
26790f6
 
3518fdf
1e28d04
26790f6
 
 
 
 
185b88a
26790f6
 
 
185b88a
26790f6
 
1df84d8
185b88a
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
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse

from answerer import Answerer
from mapper import Mapper

mapper = Mapper(
   repo="sentence-transformers",
   model="multi-qa-distilbert-cos-v1",
)
answerer = Answerer(
  model="RWKV-5-World-3B-v2-20231118-ctx16k.pth",
  vocab="rwkv_vocab_v20230424",
  strategy="cpu bf16",
  ctx_limit=16*1024,
)
  
app = FastAPI()

HTML = """
<!DOCTYPE HTML>

<html>

<body>
  <form action="" onsubmit="ask(event)">
    <input id="prompt" type="text" autocomplete="off" />
    <br>
    <input type="submit" value="SEND" />
  </form>

  <p id="output"></p>
  <script>
    const prompt = document.getElementById("prompt");
    const output = document.getElementById("output");

    const ws = new WebSocket("wss://daniilalpha-answerer-api.hf.space/answer"); 
    ws.onmessage = (e) => answer(e.data);

    function ask(event) {
      if(ws.readyState != 1) {
        answer("websocket is not connected!");
        return;
      }

      ws.send(prompt.value);
      event.preventDefault();
    }

    function answer(value) {
      output.innerHTML = value;
    }
  </script>
</body>

</html>
"""

@app.get("/")
def index():
  return HTMLResponse(HTML)

@app.get("/map")
def map():
  return HTMLResponse(HTML)

@app.websocket("/answer")
async def answer(ws: WebSocket):
  await ws.accept()

  print("ws accepted!")
      
  input = await ws.receive_text()

  print("input received!")

  await ws.send_text("OK!")

  output = answerer(input, 32)

  print("output created!")

  for el in output:
    print(f"sent: '{el}'")
    await ws.send_text(el)

  await ws.close()