Spaces:
Sleeping
Sleeping
inti
Browse files- app.py +47 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
PASSWORD = os.environ['PASSWORD']
|
| 7 |
+
OPEN_AI_KEY = os.environ['OPEN_AI_KEY']
|
| 8 |
+
|
| 9 |
+
def process_file(file_path, question):
|
| 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 |
+
sys_content = f"你是一個專業的資料分析師,請解讀這份資料"
|
| 16 |
+
prompt = df_string + "\nQ: " + question
|
| 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,
|
| 43 |
+
inputs=[gr.inputs.File(type='file'), gr.inputs.Textbox(label="Your Question")],
|
| 44 |
+
outputs="text"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
openai >= 1.0.0
|