File size: 2,407 Bytes
11e4790
 
9e3b21a
 
11e4790
9e3b21a
 
 
 
 
 
8414f72
 
 
 
 
11e4790
8414f72
11e4790
 
9e3b21a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11e4790
9e3b21a
 
 
 
 
 
11e4790
9e3b21a
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
import gradio as gr
import pandas as pd
import os
from openai import OPENAI

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)



def process_file(file):
    # 读取文件
    if file.name.endswith('.csv'):
        df = pd.read_csv(file)
    else:
        df = pd.read_excel(file)

    # 将 DataFrame 转换为字符串
    df_string = df.to_string()

    # 返回 DataFrame 字符串,以用作聊天机器人的系统提示
    return df_string

def respond(user_message, chat_history):
    print("=== 變數:user_message ===")
    print(user_message)
    print("=== 變數:chat_history ===")
    print(chat_history)

    sys_content = f"你是一個資料分析師,請用 {df_string_output} 為資料進行對話"
    messages = [
        {"role": "system", "content": sys_content},
        {"role": "user", "content": user_message}
    ]

    print("=====messages=====")
    print(messages)
    print("=====messages=====")


    request_payload = {
        "model": "gpt-4-1106-preview",
        "messages": messages,
        "max_tokens": 2000  # 設定一個較大的值,可根據需要調整
    }

    response = client.chat.completions.create(**request_payload)
    print(response)
    
    response_text = response.choices[0].message.content.strip()

    # 更新聊天历史
    new_chat_history = (user_message, response_text)
    if chat_history is None:
        chat_history = [new_chat_history]
    else:
        chat_history.append(new_chat_history)

    # 返回聊天历史和空字符串清空输入框
    return "", chat_history


with gr.Blocks() as demo:
    with gr.Row():
        file_upload = gr.File(label="Upload your file")
        df_string_output = gr.Textbox(label="DataFrame as String", visible=False)

    with gr.Row():
        chatbot = gr.Chatbot()
    
    with gr.Row():
        with gr.Column():
            with gr.Group():
                # password_input = gr.Textbox(label="Password", type="password")
                msg = gr.Textbox(label="請输入對話內容")
                send_button = gr.Button("發送")

    # 当文件上传时,更新 DataFrame 字符串
    file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)

    # 处理聊天机器人的对话
    send_button.click(
        respond, 
        inputs=[msg, chatbot], 
        outputs=[msg, chatbot]
    )

demo.launch()