Team Ai
Apppublic

Dylan-Kaneshiro/Text-to-SQL

sourceHugging Facemitupdated 3y agoView on Hugging Face
1likes
app.py73 linesDownload Raw Back to root
1import random2import time3import gradio as gr4from create_query_engine import *5 6logo_filepath = 'Readable.png'7robot_filepath = 'Robot.png'8 9custom_css = """10.column {11  align-items: center;12}13 14.column_el {15  flex-grow: 1;16}17"""18 19 20def respond(message, chat_history, query_engine, sql_engine):21    response = query_engine.query(message)22    bot_message = response.response  23    chat_history.append((message, bot_message))24    time.sleep(2)25    return "", chat_history, display_question_and_answer(chat_history), query(sql_engine, response.metadata['sql_query']), response.metadata['sql_query']26 27def display_question_and_answer(tuple_list):28    qa_history = ""29    for i, (q, a) in enumerate(tuple_list):30        qa_history += f"<details><summary>Q: {q}</summary><p>A: {a}</p></details>"31    return qa_history32 33 34with gr.Blocks(css=custom_css) as demo:35    with gr.Tab("Home"):  36            with gr.Row(variant='compact'):37                with gr.Column(scale=1, elem_classes='column'):38                    logo = gr.Image(value=logo_filepath, type='pil', show_label=False, show_download_button=False, container=False, elem_classes='column_el')39                    agent = gr.State()40                    file = gr.File(file_types=['pdf'], file_count='single', show_label=False, elem_classes='column_el', height=75)41 42                    43                    SQLidx = gr.State()44                    sql_engine = gr.State()45 46                    # Connect to database + give context pane47                    with gr.Accordion("Command String Details"):48                        gr.Markdown("Enter command string details here")49                        with gr.Row():50                            user = gr.Textbox(value='postgres', label="Username")51                            password = gr.Textbox(value='aimfall23',label="Password")52                        with gr.Row():53                            host = gr.Textbox(value='aim-23-text-to-sql.cv6cnzb0rcuo.us-east-1.rds.amazonaws.com',label="Host")54                            port = gr.Textbox(value='5432', label="Port")55                            myDB = gr.Textbox(value='sample_db', label="mydatabase")56                            upload_status = gr.Textbox(value="Database not connected yet", label="Connect Status")57                        submit_SQL_btn = gr.Button("Submit postgres details")58                    submit_SQL_btn.click(fn=create_query_engine, inputs = [file, user, password, host, port, myDB], outputs=[SQLidx, sql_engine, upload_status])59                    60                    robot = gr.Image(value=robot_filepath, type='pil', show_label=False, show_download_button=False, container=False, width=125, height=125, elem_classes='column_el')61                62                with gr.Column(scale=3, elem_classes='column'):63                    df = gr.Dataframe()64                    chatbot = gr.Chatbot(elem_classes='column_el')65                    msg = gr.Textbox(show_label=False, elem_classes='column_el')66                    with gr.Accordion("Executed SQL Query"):67                        sql_statement = gr.Textbox(label="", lines=5)68 69    with gr.Tab("Chat History"):70        history = gr.HTML()71        msg.submit(respond, [msg, chatbot, SQLidx, sql_engine], [msg, chatbot, history, df, sql_statement])72 73demo.launch()