youngtsai commited on
Commit
ef94623
1 Parent(s): 3c6552e
Files changed (1) hide show
  1. app.py +45 -14
app.py CHANGED
@@ -6,51 +6,82 @@ from openai import OpenAI
6
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
7
  client = OpenAI(api_key=OPENAI_API_KEY)
8
 
 
 
9
  def process_file(file):
10
  # 读取文件
11
  if file.name.endswith('.csv'):
12
  df = pd.read_csv(file)
13
  else:
14
  df = pd.read_excel(file)
 
15
  # 将 DataFrame 转换为字符串
16
- return df.to_string()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- def respond(user_message, df_string, chat_history):
19
- sys_content = f"你是一个资料分析师,以下是数据分析资料:\n{df_string}"
20
- messages = chat_history if chat_history is not None else []
21
- messages.append({"role": "system", "content": sys_content})
22
- messages.append({"role": "user", "content": user_message})
23
 
24
  request_payload = {
25
  "model": "gpt-4-1106-preview",
26
  "messages": messages,
27
- "max_tokens": 2000
28
  }
29
 
30
  response = client.chat.completions.create(**request_payload)
 
 
31
  response_text = response.choices[0].message.content.strip()
32
 
33
- messages.append({"role": "assistant", "content": response_text})
 
 
 
 
 
 
 
 
34
 
35
- return "", messages
36
 
37
  with gr.Blocks() as demo:
38
  with gr.Row():
39
  file_upload = gr.File(label="Upload your file")
40
- df_string_output = gr.Variable()
41
 
42
  with gr.Row():
43
  chatbot = gr.Chatbot()
44
-
45
  with gr.Row():
46
- msg = gr.Textbox(label="请输入对话内容")
47
- send_button = gr.Button("发送")
 
 
 
48
 
 
49
  file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
50
 
 
51
  send_button.click(
52
  respond,
53
- inputs=[msg, df_string_output, chatbot],
54
  outputs=[msg, chatbot]
55
  )
56
 
 
6
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
7
  client = OpenAI(api_key=OPENAI_API_KEY)
8
 
9
+
10
+
11
  def process_file(file):
12
  # 读取文件
13
  if file.name.endswith('.csv'):
14
  df = pd.read_csv(file)
15
  else:
16
  df = pd.read_excel(file)
17
+
18
  # 将 DataFrame 转换为字符串
19
+ df_string = df.to_string()
20
+
21
+ # 返回 DataFrame 字符串,以用作聊天机器人的系统提示
22
+ return df_string
23
+
24
+ def respond(user_message, chat_history):
25
+ print("=== 變數:user_message ===")
26
+ print(user_message)
27
+ print("=== 變數:chat_history ===")
28
+ print(chat_history)
29
+
30
+ sys_content = f"你是一個資料分析師,請用 {df_string_output} 為資料進行對話"
31
+ messages = [
32
+ {"role": "system", "content": sys_content},
33
+ {"role": "user", "content": user_message}
34
+ ]
35
+
36
+ print("=====messages=====")
37
+ print(messages)
38
+ print("=====messages=====")
39
 
 
 
 
 
 
40
 
41
  request_payload = {
42
  "model": "gpt-4-1106-preview",
43
  "messages": messages,
44
+ "max_tokens": 2000 # 設定一個較大的值,可根據需要調整
45
  }
46
 
47
  response = client.chat.completions.create(**request_payload)
48
+ print(response)
49
+
50
  response_text = response.choices[0].message.content.strip()
51
 
52
+ # 更新聊天历史
53
+ new_chat_history = (user_message, response_text)
54
+ if chat_history is None:
55
+ chat_history = [new_chat_history]
56
+ else:
57
+ chat_history.append(new_chat_history)
58
+
59
+ # 返回聊天历史和空字符串清空输入框
60
+ return "", chat_history
61
 
 
62
 
63
  with gr.Blocks() as demo:
64
  with gr.Row():
65
  file_upload = gr.File(label="Upload your file")
66
+ df_string_output = gr.Textbox(label="DataFrame as String", visible=False)
67
 
68
  with gr.Row():
69
  chatbot = gr.Chatbot()
70
+
71
  with gr.Row():
72
+ with gr.Column():
73
+ with gr.Group():
74
+ # password_input = gr.Textbox(label="Password", type="password")
75
+ msg = gr.Textbox(label="請输入對話內容")
76
+ send_button = gr.Button("發送")
77
 
78
+ # 当文件上传时,更新 DataFrame 字符串
79
  file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
80
 
81
+ # 处理聊天机器人的对话
82
  send_button.click(
83
  respond,
84
+ inputs=[msg, chatbot],
85
  outputs=[msg, chatbot]
86
  )
87