Spaces:
Sleeping
Sleeping
def generate_df_summarise(df_string):
Browse files
app.py
CHANGED
|
@@ -20,18 +20,40 @@ def process_file(file):
|
|
| 20 |
|
| 21 |
# 根据上传的文件内容生成问题
|
| 22 |
questions = generate_questions(df_string)
|
|
|
|
| 23 |
|
| 24 |
# 返回按钮文本和 DataFrame 字符串
|
| 25 |
return questions[0] if len(questions) > 0 else "", \
|
| 26 |
questions[1] if len(questions) > 1 else "", \
|
| 27 |
questions[2] if len(questions) > 2 else "", \
|
|
|
|
| 28 |
df_string
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
|
| 36 |
def generate_questions(df_string):
|
| 37 |
# 使用 OpenAI 生成基于上传数据的问题
|
|
@@ -106,11 +128,6 @@ def respond(user_message, df_string_output, chat_history):
|
|
| 106 |
# 返回聊天历史和空字符串清空输入框
|
| 107 |
return "", chat_history
|
| 108 |
|
| 109 |
-
|
| 110 |
-
def on_button_click(button, df_string_output, chat_history):
|
| 111 |
-
# 当按钮被点击时发送对应的问题
|
| 112 |
-
return respond(button, df_string_output, chat_history)
|
| 113 |
-
|
| 114 |
with gr.Blocks() as demo:
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column():
|
|
@@ -120,8 +137,12 @@ with gr.Blocks() as demo:
|
|
| 120 |
send_button = gr.Button("Send")
|
| 121 |
|
| 122 |
with gr.Column():
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Group():
|
| 126 |
gr.Markdown("## 常用問題")
|
| 127 |
btn_1 = gr.Button()
|
|
@@ -134,16 +155,17 @@ with gr.Blocks() as demo:
|
|
| 134 |
inputs=[msg, df_string_output, chatbot],
|
| 135 |
outputs=[msg, chatbot]
|
| 136 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|
| 139 |
-
file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_string_output])
|
| 140 |
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
# 连接按钮点击事件
|
| 143 |
-
btn_1.click(on_button_click, inputs=[btn_1, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 144 |
-
btn_2.click(on_button_click, inputs=[btn_2, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 145 |
-
btn_3.click(on_button_click, inputs=[btn_3, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 146 |
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
demo.launch()
|
|
|
|
| 20 |
|
| 21 |
# 根据上传的文件内容生成问题
|
| 22 |
questions = generate_questions(df_string)
|
| 23 |
+
df_summarise = generate_df_summarise(df_string)
|
| 24 |
|
| 25 |
# 返回按钮文本和 DataFrame 字符串
|
| 26 |
return questions[0] if len(questions) > 0 else "", \
|
| 27 |
questions[1] if len(questions) > 1 else "", \
|
| 28 |
questions[2] if len(questions) > 2 else "", \
|
| 29 |
+
df_summarise, \
|
| 30 |
df_string
|
| 31 |
|
| 32 |
+
def generate_df_summarise(df_string):
|
| 33 |
+
# 使用 OpenAI 生成基于上传数据的问题
|
| 34 |
+
sys_content = "你是一個資料分析師,服務對象為老師,請精讀資料,使用 zh-TW"
|
| 35 |
+
user_content = f"請根據 {df_string},大概描述這張表的欄位敘述,以及內容的資料樣態與解析"
|
| 36 |
+
messages = [
|
| 37 |
+
{"role": "system", "content": sys_content},
|
| 38 |
+
{"role": "user", "content": user_content}
|
| 39 |
+
]
|
| 40 |
+
print("=====messages=====")
|
| 41 |
+
print(messages)
|
| 42 |
+
print("=====messages=====")
|
| 43 |
+
|
| 44 |
+
request_payload = {
|
| 45 |
+
"model": "gpt-4-1106-preview",
|
| 46 |
+
"messages": messages,
|
| 47 |
+
"max_tokens": 2000,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
response = client.chat.completions.create(**request_payload)
|
| 51 |
+
df_summarise = response.choices[0].message.content.strip()
|
| 52 |
+
print("=====df_summarise=====")
|
| 53 |
+
print(df_summarise)
|
| 54 |
+
print("=====df_summarise=====")
|
| 55 |
|
| 56 |
+
return df_summarise
|
| 57 |
|
| 58 |
def generate_questions(df_string):
|
| 59 |
# 使用 OpenAI 生成基于上传数据的问题
|
|
|
|
| 128 |
# 返回聊天历史和空字符串清空输入框
|
| 129 |
return "", chat_history
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
with gr.Row():
|
| 133 |
with gr.Column():
|
|
|
|
| 137 |
send_button = gr.Button("Send")
|
| 138 |
|
| 139 |
with gr.Column():
|
| 140 |
+
with gr.Group():
|
| 141 |
+
df_string_output = gr.Textbox(label="raw data")
|
| 142 |
+
with gr.Group():
|
| 143 |
+
gr.Markdown("## 這是一張什麼表?")
|
| 144 |
+
df_summarise = gr.Textbox()
|
| 145 |
+
|
| 146 |
with gr.Group():
|
| 147 |
gr.Markdown("## 常用問題")
|
| 148 |
btn_1 = gr.Button()
|
|
|
|
| 155 |
inputs=[msg, df_string_output, chatbot],
|
| 156 |
outputs=[msg, chatbot]
|
| 157 |
)
|
| 158 |
+
# 连接按钮点击事件
|
| 159 |
+
btn_1.click(respond, inputs=[btn_1, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 160 |
+
btn_2.click(respond, inputs=[btn_2, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 161 |
+
btn_3.click(respond, inputs=[btn_3, df_string_output, chatbot], outputs=[msg, chatbot])
|
| 162 |
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|
| 165 |
+
file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
|
| 169 |
|
| 170 |
|
| 171 |
demo.launch()
|