File size: 2,279 Bytes
445dc43
f959207
445dc43
c92e861
445dc43
 
c92e861
f959207
445dc43
 
 
 
 
 
 
 
 
 
 
 
f959207
445dc43
 
 
 
 
6161919
445dc43
 
6161919
 
fd57848
445dc43
 
fd57848
c92e861
 
 
 
 
 
445dc43
 
 
 
 
 
 
c92e861
445dc43
6161919
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
import gradio as gr
from agents import agent, createConnection, getAllTable, Inference, set_connection

def connect(username, password, host,port, database):
    print("test")
    global conn
    conn = createConnection(username, password, host,port, database)
    set_connection(conn)
    if conn:
        tables = getAllTable(conn)  # Mengambil data setelah koneksi berhasil
        return (
            conn, 
            gr.update(value="✅ Connect Success", visible=True),  # Menampilkan toast sukses
            gr.update(value=tables, visible=True)  # Menampilkan tabel setelah connect
        )
    return None, gr.update(value="❌ Connection Failed", visible=True), gr.update(visible=False)

def yapping(message,history):
    print(history)
    global conn
    result = Inference(conn,message)
    return str(result) 


with gr.Blocks() as demo:
    state = gr.State(None)
    gr.Markdown("Chatsql")

    with gr.Tab("Chatbot"):
        gr.Markdown('you must connect to database before using the chatbot')
        gr.ChatInterface(fn=yapping, type='messages')
        gr.Markdown('Data and chat history are not being collected, so there\'s no need to worry about misuse of your information.')

    with gr.Tab("Database"):
        gr.Markdown("### Database Connection")
        gr.Markdown("Use only a cloud-hosted MySQL server (local servers are not supported).\n\nYou can use the demo server (if desired).")
        host = gr.Textbox(placeholder="http://127.0.0.1/", label="Host", value='mysql-13c1c04a-alfthr378-61ca.d.aivencloud.com')
        port = gr.Textbox(placeholder="Port", label="Port", value='16222')
        username = gr.Textbox(placeholder="Username", label="Username", value='chatsqlrek')
        password = gr.Textbox(placeholder="Password", type="password", label="Password", value='123456789')
        database = gr.Textbox(placeholder="Database", label="Database", value='sakila')

        toast = gr.Markdown(visible=False)  # Feedback setelah tombol ditekan
        btn = gr.Button("Connect")

        gr.Markdown("### Database Info")
        tables_output = gr.Textbox(visible=False)  # Awalnya disembunyikan

        btn.click(fn=connect, inputs=[username, password, host,port, database], outputs=[state, toast, tables_output])

demo.launch()