Spaces:
Running
Running
File size: 4,280 Bytes
5ec2a7a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
from h2o_wave import main, app, Q, ui, data
from gradio_client import Client
import ast
async def init_ui(q: Q) -> None:
q.page['meta'] = ui.meta_card(
box='',
layouts=[
ui.layout(breakpoint='xs', min_height='100vh', zones=[
ui.zone('main', size='1', direction=ui.ZoneDirection.ROW, zones=[
ui.zone('sidebar', size='250px'),
ui.zone('body', direction=ui.ZoneDirection.COLUMN, zones=[
ui.zone('title', size='55px'),
ui.zone('content', size='1'),
ui.zone('footer'),
]),
])
])
],
title='H2O GPT',
)
q.page['sidebar'] = ui.nav_card(
box='sidebar', color='primary', title='GPT Wave app', subtitle='Powered by H2O GPT',
value=f"#{q.args['#']}' if q.args['#'] else '#page1",
image='https://wave.h2o.ai/img/h2o-logo.svg', items=[
ui.nav_group('', items=[
ui.nav_item(name='wave-docs', label='Wave docs', path='https://wave.h2o.ai/'),
ui.nav_item(name='h2o-gpt', label='H2O GPT', path='https://github.com/h2oai/h2ogpt'),
ui.nav_item(name='fine-tune', label='LLM Studio', path='https://github.com/h2oai/h2o-llmstudio'),
ui.nav_item(name='more-models', label='More models', path='https://huggingface.co/h2oai'),
]),
],
secondary_items=[
ui.toggle(name='dark_mode', label='Dark mode', trigger=True),
ui.text('<center>Made with H2O Wave.</center>')
]
)
q.page['chatbot'] = ui.chatbot_card(
box=ui.box('content'),
data=data('content from_user', t='list'),
name='chatbot'
)
q.page['title'] = ui.section_card(
box='title',
title='',
subtitle='',
items=[
ui.dropdown(name='model', trigger=True, label='', value='gpt', choices=[
ui.choice(name='gpt', label='H2O GPT'),
ui.choice(name='falcon', label='h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3'),
ui.choice(name='llama', label='h2oai/h2ogpt-research-oasst1-llama-65b'),
ui.choice(name='mpt', label='mosaicml/mpt-30b-instruct'),
]),
ui.button(name='clear', label='Clear', icon='Delete'),
],
)
@app('/')
async def serve(q: Q):
if not q.client.initialized:
await init_ui(q)
q.client.model_client = Client('https://gpt.h2o.ai/')
q.client.initialized = True
# A new message arrived.
if q.args.chatbot:
# Append user message.
q.page['chatbot'].data += [q.args.chatbot, True]
# Append bot response.
kwargs = dict(instruction_nochat=q.args.chatbot)
try:
res = q.client.model_client.predict(str(dict(kwargs)), api_name='/submit_nochat_api')
bot_res = ast.literal_eval(res)['response']
q.page['chatbot'].data += [bot_res, False]
except:
q.page['meta'] = ui.meta_card(box='', notification_bar=ui.notification_bar(
text='An error occurred during prediction. Please try later or a different model.',
type='error',
))
elif q.args.clear:
# Recreate the card.
q.page['chatbot'] = ui.chatbot_card(
box=ui.box('content'),
data=data('content from_user', t='list'),
name='chatbot'
)
elif q.args.dark_mode is not None:
q.page['meta'].theme = 'h2o-dark' if q.args.dark_mode else 'light'
q.page['sidebar'].color = 'card' if q.args.dark_mode else 'primary'
elif q.args.model:
try:
q.client.model_client = Client(f'https://{q.args.model}.h2o.ai/')
q.page['meta'] = ui.meta_card(box='', notification_bar=ui.notification_bar(
text='Model changed successfully.',
type='success',
))
except:
q.page['meta'] = ui.meta_card(box='', notification_bar=ui.notification_bar(
text='An error occurred while changing the model. Please try a different one.',
type='error',
))
await q.page.save()
|