answerer-api / main.py
DaniilAlpha's picture
Update main.py
1df84d8
raw
history blame
1.43 kB
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
from answerer import Answerer
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("/")
async def index():
return HTMLResponse(HTML)
@app.websocket("/answer")
async def answer(ws: WebSocket):
await ws.accept()
print("ws connected!")
input = await ws.receive_text()
output = answerer(input, 32)
print("output ready!")
for el in output:
print(f"sent: '{el}'")
await ws.send_text(el)
await ws.close()
print("ws closed!")