youngtsai commited on
Commit
9e3b21a
1 Parent(s): 42e61fb
Files changed (1) hide show
  1. app.py +74 -9
app.py CHANGED
@@ -1,7 +1,14 @@
1
  import gradio as gr
2
  import pandas as pd
 
 
3
 
4
- def process_file(file, question):
 
 
 
 
 
5
  # 读取文件
6
  if file.name.endswith('.csv'):
7
  df = pd.read_csv(file)
@@ -11,13 +18,71 @@ def process_file(file, question):
11
  # 将 DataFrame 转换为字符串
12
  df_string = df.to_string()
13
 
14
- # 返回 DataFrame 字符串和用户问题
15
- return df_string + "\nQ: " + question
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- iface = gr.Interface(
18
- fn=process_file,
19
- inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Your Question")],
20
- outputs=gr.Text(label="Output")
21
- )
 
22
 
23
- iface.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import os
4
+ from openai import OPENAI
5
 
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)
 
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
 
88
+ demo.launch()