Spaces:
Paused
Paused
| import os | |
| from fastapi import FastAPI, WebSocket | |
| from answerer import Answerer | |
| answerer = Answerer( | |
| repo="BlinkDL/rwkv-5-world", | |
| filename="RWKV-5-World-3B-v2-20231118-ctx16k", | |
| vocab="rwkv_vocab_v20230424", | |
| strategy="cpu bf16", | |
| ctx_limit=16*1024, | |
| ) | |
| app = FastAPI() | |
| HTML = """ | |
| <!DOCTYPE HTML> | |
| <html> | |
| <body> | |
| <form action="" onsubmit="sendMessage(event)"> | |
| <input id="prompt" type="text" autocomplete="off"/> | |
| <button>SEND</button> | |
| </form> | |
| <p id="output"></p> | |
| <script> | |
| const prompt = document.getElementById("prompt"); | |
| const output = document.getElementById("output"); | |
| const ws = new WebSocket("ws://huggingface.co/spaces/DaniilAlpha/answerer-api:8000/answer"); | |
| ws.onmessage = function (event) => output.innerText = event.data; | |
| function ask(event) { | |
| ws.send(prompt.value); | |
| event.preventDefault(); | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| async def index(): | |
| return HTMLResponse(HTML) | |
| async def answer(ws: WebSocket): | |
| await ws.accept() | |
| input = await ws.receive_text() | |
| stream = answerer(input, 256) | |
| for el in stream: | |
| await ws.send_text(el) |