from fasthtml.common import *
from fasthtml_hf import setup_hf_backup # Importing setup_hf_backup
import openai
import uvicorn
# Set up the app, including daisyui and tailwind for the chat component
tlink = Script(src="https://cdn.tailwindcss.com")
dlink = Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/daisyui@4.11.1/dist/full.min.css")
app = FastHTML(hdrs=(tlink, dlink, picolink))
# Set up OpenAI API (https://openai.com/)
# openai.api_key = os.getenv("OPENAI_API_KEY")
messages = []
# Chat message component (renders a chat bubble)
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}")
# The input field for the user message. Also used to clear the
# input field after sending a message via an OOB swap
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')
# The main screen
@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
# Handle the form submission
@app.post("/")
def post(msg:str):
messages.append({"role":"user", "content":msg})
# Get response from OpenAI API
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful and concise assistant."},
*messages
]
)
assistant_msg = response.choices[0].message.content.strip()
messages.append({"role":"assistant", "content":assistant_msg})
return (ChatMessage(messages[-2]), # The user's message
ChatMessage(messages[-1]), # The chatbot's response
ChatInput()) # And clear the input field via an OOB swap
#serve()
if __name__ == "__main__":
setup_hf_backup(app)
uvicorn.run(app, host="0.0.0.0", port=7860)