Spaces:
Sleeping
Sleeping
csv
Browse files
app.py
CHANGED
|
@@ -1,42 +1,21 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
from openai import OpenAI
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
client = OpenAI(api_key=OPEN_AI_KEY)
|
| 11 |
-
df = pd.read_csv(file_path) if file_path.endswith('.csv') else pd.read_excel(file_path)
|
| 12 |
df_string = df.to_string()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
messages = [
|
| 18 |
-
{"role": "system", "content": sys_content},
|
| 19 |
-
{"role": "user", "content": prompt}
|
| 20 |
-
]
|
| 21 |
-
|
| 22 |
-
print("=====messages=====")
|
| 23 |
-
print(messages)
|
| 24 |
-
print("=====messages=====")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
request_payload = {
|
| 28 |
-
"model": "gpt-4-1106-preview",
|
| 29 |
-
"messages": messages,
|
| 30 |
-
"max_tokens": 2046,
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
response = client.chat.completions.create(**request_payload)
|
| 34 |
-
print(response)
|
| 35 |
-
|
| 36 |
-
response_text = response.choices[0].message.content.strip()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return response_text
|
| 40 |
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=process_file,
|
|
|
|
| 1 |
+
#
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def process_file(file, question):
|
| 7 |
+
# 读取文件
|
| 8 |
+
if file.name.endswith('.csv'):
|
| 9 |
+
df = pd.read_csv(file)
|
| 10 |
+
else:
|
| 11 |
+
df = pd.read_excel(file)
|
| 12 |
|
| 13 |
+
# 将 DataFrame 转换为字符串
|
|
|
|
|
|
|
| 14 |
df_string = df.to_string()
|
| 15 |
|
| 16 |
+
# 这里只是输出 DataFrame 字符串和用户问题
|
| 17 |
+
# 您可以根据需要添加额外的逻辑
|
| 18 |
+
return df_string + "\nQ: " + question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=process_file,
|