debisoft commited on
Commit
03605b7
·
1 Parent(s): 329c810

Added Upstage ChatBot

Browse files
Files changed (2) hide show
  1. main.py +60 -54
  2. requirements.txt +1 -0
main.py CHANGED
@@ -1,57 +1,63 @@
1
  from fasthtml.common import *
2
- from fasthtml_hf import setup_hf_backup
3
-
4
- app,rt,todos,Todo = fast_app(
5
- 'data/todos.db',
6
- hdrs=[Style(':root { --pico-font-size: 100%; }')],
7
- id=int, title=str, done=bool, pk='id')
8
-
9
- id_curr = 'current-todo'
10
- def tid(id): return f'todo-{id}'
11
-
12
- @patch
13
- def __ft__(self:Todo):
14
- show = AX(self.title, f'/todos/{self.id}', id_curr)
15
- edit = AX('edit', f'/edit/{self.id}' , id_curr)
16
- dt = ' ✅' if self.done else ''
17
- return Li(show, dt, ' | ', edit, id=tid(self.id))
18
-
19
- def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", required=True, **kw)
20
-
21
- @rt("/")
22
- async def get():
23
- add = Form(Group(mk_input(), Button("Add")),
24
- hx_post="/", target_id='todo-list', hx_swap="beforeend")
25
- card = Card(Ul(*todos(), id='todo-list'),
26
- header=add, footer=Div(id=id_curr)),
27
- title = 'Todo list'
28
- return Title(title), Main(H1(title), card, cls='container')
29
-
30
- @rt("/todos/{id}")
31
- async def delete(id:int):
32
- todos.delete(id)
33
- return clear(id_curr)
34
-
35
- @rt("/")
36
- async def post(todo:Todo): return todos.insert(todo), mk_input(hx_swap_oob='true')
37
-
38
- @rt("/edit/{id}")
39
- async def get(id:int):
40
- res = Form(Group(Input(id="title"), Button("Save")),
41
- Hidden(id="id"), CheckboxX(id="done", label='Done'),
42
- hx_put="/", target_id=tid(id), id="edit")
43
- return fill_form(res, todos.get(id))
44
-
45
- @rt("/")
46
- async def put(todo: Todo): return todos.upsert(todo), clear(id_curr)
47
-
48
- @rt("/todos/{id}")
49
- async def get(id:int):
50
- todo = todos.get(id)
51
- btn = Button('delete', hx_delete=f'/todos/{todo.id}',
52
- target_id=tid(todo.id), hx_swap="outerHTML")
53
- return Div(Div(todo.title), btn)
54
-
55
- setup_hf_backup(app)
 
 
 
 
 
56
 
57
  serve()
 
 
1
  from fasthtml.common import *
2
+ from openai import OpenAI # openai==1.2.0
3
+ from dotenv import load_dotenv, find_dotenv
4
+ _ = load_dotenv(find_dotenv())
5
+
6
+ upstage_token = os.getenv('UPSTAGE_TOKEN')
7
+
8
+ # Set up the app, including daisyui and tailwind for the chat component
9
+ hdrs = (picolink, Script(src="https://cdn.tailwindcss.com"),
10
+ Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css"))
11
+ app = FastHTML(hdrs=hdrs, cls="p-4 max-w-lg mx-auto")
12
+
13
+ client = OpenAI(
14
+ api_key=upstage_token,
15
+ base_url="https://api.upstage.ai/v1/solar"
16
+ )
17
+
18
+ def get_completion_from_messages(messages, model="solar-1-mini-chat", temperature=0):
19
+ response = client.ChatCompletion.create(
20
+ model=model,
21
+ messages=messages,
22
+ temperature=temperature, # this is the degree of randomness of the model's output
23
+ )
24
+
25
+ # Chat message component (renders a chat bubble)
26
+ def ChatMessage(msg, user):
27
+ bubble_class = "chat-bubble-primary" if user else 'chat-bubble-secondary'
28
+ chat_class = "chat-end" if user else 'chat-start'
29
+ return Div(cls=f"chat {chat_class}")(
30
+ Div('user' if user else 'assistant', cls="chat-header"),
31
+ Div(msg, cls=f"chat-bubble {bubble_class}"),
32
+ Hidden(msg, name="messages")
33
+ )
34
+
35
+ # The input field for the user message. Also used to clear the
36
+ # input field after sending a message via an OOB swap
37
+ def ChatInput():
38
+ return Input(name='msg', id='msg-input', placeholder="Type a message",
39
+ cls="input input-bordered w-full", hx_swap_oob='true')
40
+
41
+ # The main screen
42
+ @app.get
43
+ def index():
44
+ page = Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend")(
45
+ Div(id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"),
46
+ Div(cls="flex space-x-2 mt-2")(
47
+ Group(ChatInput(), Button("Send", cls="btn btn-primary"))
48
+ )
49
+ )
50
+ return Titled('Chatbot Demo', page)
51
+
52
+ # Handle the form submission
53
+ @app.post
54
+ def send(msg:str, messages:list[str]=None):
55
+ if not messages: messages = []
56
+ messages.append(msg.rstrip())
57
+ r = contents(get_completion_from_messages(messages)) # get response from chat model
58
+ return (ChatMessage(msg, True), # The user's message
59
+ ChatMessage(r.rstrip(), False), # The chatbot's response
60
+ ChatInput()) # And clear the input field via an OOB swap
61
 
62
  serve()
63
+
requirements.txt CHANGED
@@ -3,3 +3,4 @@ huggingface-hub
3
  fasthtml-hf
4
  python-multipart
5
  sqlite-utils
 
 
3
  fasthtml-hf
4
  python-multipart
5
  sqlite-utils
6
+ claudette