A Streamlit app based on vanna.ai #4494
-
We can chat with our DataBase with this app. https://github.com/vanna-ai/vanna-streamlit |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Hey. First of all, welcome to the NiceGUI discussion forum and I am glad you show interest to NiceGUI. Coming from streamlit, you may find that you have to get used to the different syntax. I'll save you the details but we use callbacks instead of if-statements, because the former is more realistic in terms of how an actual web application works. No server in the world re-executes the page code in a tight loop in order to detect an update on button click. Instead, traditionally, the frontend often communicates to the backend via XMLHttpRequest or the Fetch API, and NiceGUI uses WebSocket to do that in the same connection while also enabling the backend to update UI elements on the frontend. There is a learning curve, but I assue you, (1) it is instinctive after you learn it, (2) speaking from experience the learning curve is super gentile compared to some other UI frameworks (something something TSX, it's a nightmare to read). NiceGUI strives to be the "Nicest UI framework", following the "Nice Guy" ethos throughout. Watch the talk by the one and only @falkoschindler to find out. Not only that, but he is actually also a nice guy. If you are getting confused at the start, it's OK! The map of NiceGUI lays out all features in the library, which although a bit long for humans to read through, is just about perfect for getting AIs to begin to help you along with any issues you face. https://nicegui.io/documentation#map-of-nicegui The community is active and close-knitted. There are many examples on https://nicegui.io/#examples, and you can always find help in the discussion provided you have a Minimum Reproducable Example of the code pin-pointing what is the issue, or report a bug if you are sure it is a NiceGUI problem. You can check out the community Wiki, and even edit it as well! https://github.com/zauberzeug/nicegui/wiki If you are feeling adventurous, you can even contribute to new features. It is easy to setup the development environment by following https://github.com/zauberzeug/nicegui/blob/main/CONTRIBUTING.md. I myself (a Python noob) contributed to 2 speed improvements in the library as well. 😉 Specific to your example of an AI, check here: https://github.com/zauberzeug/nicegui/tree/main/examples/chat_with_ai/ I wish you on a pleasant journey 🥰 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I tried implementing it with NiceGUI, and the inference from Vanna is quite good. However, the UI often freezes during output, and ui.spinner() doesn't display. Is there a way to improve this? Here's my code, which can be run directly after installing the necessary libraries. The api_key and database connection are both working properly. #!/usr/bin/env python3
from typing import List, Tuple, Union
from vanna.openai.openai_chat import OpenAI_Chat
from vanna.chromadb.chromadb_vector import ChromaDB_VectorStore
from openai import OpenAI
from nicegui import ui,run
import pymysql
import pandas as pd
#SET UP
client = OpenAI(
api_key="Your api key here",
base_url="https://api.deepseek.com",
)
class MyVanna(ChromaDB_VectorStore, OpenAI_Chat):
def __init__(self,client=None, config=None):
ChromaDB_VectorStore.__init__(self, config=config)
OpenAI_Chat.__init__(self, client=client, config=config)
vn = MyVanna(client=client, config={"model": "deepseek-chat"})
conn_details = {
"host": "host",
"port": 3307,
"user": "user_name",
"password": "password",
"database": "database_name",
# "charset": "utf8mb4",
}
# 建立MySQL数据库连接
conn = pymysql.connect(**conn_details)
def run_sql(sql: str) -> pd.DataFrame:
df = pd.read_sql_query(sql, conn)
return df
vn.run_sql = run_sql
vn.run_sql_is_set = True
#Train
vn.train(ddl="""CREATE TABLE IF NOT EXISTS titanic (
PassengerID INT(11) NOT NULL COMMENT '旅客ID',
Survived INT(11) UNIQUE NOT NULL COMMENT '是否生还',
Pclass INT(11) COMMENT '席位等级',
Name VARCHAR(255) COMMENT '姓名',
Sex VARCHAR(255) COMMENT '性别',
Age DOUBLE COMMENT '年龄',
SibSp INT(11) COMMENT'兄弟姐妹及配偶数量',
Parch INT(11) COMMENT '父母及子女数量',
Ticket VARCHAR(255) COMMENT '船票编号',
Cabin VARCHAR(255) COMMENT '客舱号',
Embarked VARCHAR(255) COMMENT '登船港口',
Fare DOUBLE COMMENT '票价'
) COMMENT='用户信息表' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
""",
documentation='This is a dataset about Titanic',
sql='SELECT PassengerID, Survived, Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Cabin,Embarked FROM titanic')
@ui.page('/')
def main():
async def send() -> None:
question = text.value
text.value = ''
with message_container:
ui.chat_message(text=question, name='你', sent=True)
spinner = ui.spinner(type='dots')
response_message = ui.chat_message(name='智能助手', sent=False)
# await run.cpu_bound(answer)
if question:
sql= vn.generate_sql(question=question, allow_llm_to_see_data=True)
if sql:
if vn.is_sql_valid(sql=sql):
df=vn.run_sql(sql=sql)
if df is not None:
print(df)
with response_message:
ui.table.from_pandas(df)
print(vn.should_generate_chart(df=df))
if vn.should_generate_chart(df=df):
code = vn.generate_plotly_code(question=question, sql=sql, df=df)
# if chart.value:
if code is not None and code !="":
print(f'code:{code}')
fig=vn.get_plotly_figure(plotly_code=code, df=df)
if fig is not None:
print(f'fig:{fig}')
with response_message:
ui.plotly(fig).classes('w-full')
else:
with response_message:
ui.html('无法为该请求生成查询')
# ui.run_javascript('window.scrollTo(0, document.body.scrollHeight)')
message_container.remove(spinner)
ui.add_css(r'a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}')
# the queries below are used to expand the contend down to the footer (content can then use flex-grow to expand)
ui.query('.q-page').classes('flex')
ui.query('.nicegui-content').classes('w-full')
with ui.tabs().classes('w-full') as tabs:
chat_tab = ui.tab('Chat')
with ui.tab_panels(tabs, value=chat_tab).classes('w-full mx-auto flex-grow items-stretch'):
message_container = ui.tab_panel(chat_tab).classes('items-stretch')
with ui.footer().classes('bg-white'), ui.column().classes('w-full max-w-3xl mx-auto my-6'):
with ui.row().classes('w-full no-wrap items-center'):
placeholder = 'message'
text = ui.input(placeholder=placeholder).props('rounded outlined input-class=mx-3') \
.classes('w-full self-center').on('keydown.enter', send)
ui.markdown('simple chat app built with [NiceGUI](https://nicegui.io)') \
.classes('text-xs self-end mr-8 m-[-1em] text-primary')
with ui.left_drawer():
chart=ui.switch('generate chart?',value=False)
ui.run(title='Chat with vanna (example)') |
Beta Was this translation helpful? Give feedback.
-
Try AsyncOpenAI: https://github.com/openai/openai-python?tab=readme-ov-file#async-usage Not sure if vanna let you use AsyncOpenAI. If not, last resort would be to use |
Beta Was this translation helpful? Give feedback.
Try AsyncOpenAI:
https://github.com/openai/openai-python?tab=readme-ov-file#async-usage
Not sure if vanna let you use AsyncOpenAI. If not, last resort would be to use
run.io_bound