|
from fasthtml.common import *
|
|
from fasthtml_hf import setup_hf_backup
|
|
import uvicorn
|
|
from transformers import pipeline
|
|
|
|
|
|
tlink = Script(src="https://cdn.tailwindcss.com")
|
|
dlink = Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")
|
|
app = FastHTML(hdrs=(tlink, dlink, picolink))
|
|
|
|
|
|
pipe = pipeline("text-generation", model="ReliableAI/UCCIX-Llama2-13B-Instruct")
|
|
|
|
messages = []
|
|
|
|
|
|
def ChatMessage(msg):
|
|
bubble_class = f"chat-bubble-{'primary' if msg['role'] == 'user' else 'secondary'}"
|
|
chat_class = f"chat-{'end' if msg['role'] == 'user' else 'start'}"
|
|
return Div(Div(msg['role'], cls="chat-header"),
|
|
Div(msg['content'], cls=f"chat-bubble {bubble_class}"),
|
|
cls=f"chat {chat_class}")
|
|
|
|
|
|
|
|
def ChatInput():
|
|
return Input(type="text", name='msg', id='msg-input',
|
|
placeholder="Type a message",
|
|
cls="input input-bordered w-full", hx_swap_oob='true')
|
|
|
|
|
|
@app.route("/")
|
|
def get():
|
|
page = Body(H1('Chatbot Demo'),
|
|
Div(*[ChatMessage(msg) for msg in messages],
|
|
id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"),
|
|
Form(Group(ChatInput(), Button("Send", cls="btn btn-primary")),
|
|
hx_post="/", hx_target="#chatlist", hx_swap="beforeend",
|
|
cls="flex space-x-2 mt-2",
|
|
), cls="p-4 max-w-lg mx-auto")
|
|
return Title('Chatbot Demo'), page
|
|
|
|
|
|
@app.post("/")
|
|
def post(msg:str):
|
|
messages.append({"role":"user", "content":msg})
|
|
|
|
|
|
full_prompt = "You are a helpful and concise assistant.\n\n"
|
|
for m in messages:
|
|
full_prompt += f"{m['role'].capitalize()}: {m['content']}\nAssistant: "
|
|
|
|
response = pipe(full_prompt, max_length=2048, num_return_sequences=1)
|
|
|
|
assistant_msg = response[0]['generated_text'].split("Assistant: ")[-1].strip()
|
|
messages.append({"role":"assistant", "content":assistant_msg})
|
|
|
|
return (ChatMessage(messages[-2]),
|
|
ChatMessage(messages[-1]),
|
|
ChatInput())
|
|
|
|
if __name__ == "__main__":
|
|
setup_hf_backup(app)
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |