Upload app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,7 @@ import pathlib
|
|
| 29 |
from pathlib import Path
|
| 30 |
from matplotlib.font_manager import FontProperties
|
| 31 |
import seaborn as sns
|
|
|
|
| 32 |
|
| 33 |
os.environ["OPENAI_API_KEY"] = os.environ['user_token']
|
| 34 |
openai.api_key = os.environ['user_token']
|
|
@@ -54,17 +55,17 @@ radio_2 = col2.radio(label='模式选择', options=[
|
|
| 54 |
|
| 55 |
# with tab2:
|
| 56 |
def upload_file(uploaded_file):
|
| 57 |
-
# uploaded_file = st.file_uploader(
|
| 58 |
-
# "选择一个文件", type=(["csv", "xlsx", "xls"]))
|
| 59 |
if uploaded_file is not None:
|
| 60 |
filename = uploaded_file.name
|
| 61 |
st.write(filename) ## print out the whole file name to validate.
|
| 62 |
try:
|
| 63 |
if '.csv' in filename:
|
| 64 |
csv_file = pd.read_csv(uploaded_file)
|
|
|
|
| 65 |
st.write(csv_file[:3]) # 这里只是显示文件,后面需要定位文件所在的绝对路径。
|
| 66 |
else:
|
| 67 |
xls_file = pd.read_excel(uploaded_file)
|
|
|
|
| 68 |
st.write(xls_file[:3])
|
| 69 |
except Exception as e:
|
| 70 |
st.write(e)
|
|
@@ -73,14 +74,29 @@ def upload_file(uploaded_file):
|
|
| 73 |
temp_dir = tempfile.TemporaryDirectory()
|
| 74 |
# ! working.
|
| 75 |
uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name
|
| 76 |
-
with open(
|
| 77 |
-
# output_temporary_file.write(uploaded_file.read())
|
| 78 |
# ! 必须用这种格式读入内容,然后才可以写入temporary文件夹中。
|
|
|
|
| 79 |
output_temporary_file.write(uploaded_file.getvalue())
|
| 80 |
-
st.write(uploaded_file_path) # * 可以查看文件是否真实存在,然后是否可以
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
bing_search_api_key = os.environ['bing_api_key']
|
|
@@ -259,10 +275,10 @@ async def text_mode():
|
|
| 259 |
|
| 260 |
async def data_mode(uploaded_file_path):
|
| 261 |
print('数据分析模式启动!')
|
| 262 |
-
|
| 263 |
## TODO: validate the existence of uploaded file.
|
| 264 |
-
st.write(uploaded_file_path)
|
| 265 |
-
tmp1 = pd.read_csv(
|
| 266 |
st.write(tmp1[:5])
|
| 267 |
|
| 268 |
# Initialize chat history
|
|
|
|
| 29 |
from pathlib import Path
|
| 30 |
from matplotlib.font_manager import FontProperties
|
| 31 |
import seaborn as sns
|
| 32 |
+
from time import sleep
|
| 33 |
|
| 34 |
os.environ["OPENAI_API_KEY"] = os.environ['user_token']
|
| 35 |
openai.api_key = os.environ['user_token']
|
|
|
|
| 55 |
|
| 56 |
# with tab2:
|
| 57 |
def upload_file(uploaded_file):
|
|
|
|
|
|
|
| 58 |
if uploaded_file is not None:
|
| 59 |
filename = uploaded_file.name
|
| 60 |
st.write(filename) ## print out the whole file name to validate.
|
| 61 |
try:
|
| 62 |
if '.csv' in filename:
|
| 63 |
csv_file = pd.read_csv(uploaded_file)
|
| 64 |
+
csv_file.to_csv('./upload.csv', encoding='utf-8', index=False)
|
| 65 |
st.write(csv_file[:3]) # 这里只是显示文件,后面需要定位文件所在的绝对路径。
|
| 66 |
else:
|
| 67 |
xls_file = pd.read_excel(uploaded_file)
|
| 68 |
+
xls_file.to_csv('./upload.csv', index=False)
|
| 69 |
st.write(xls_file[:3])
|
| 70 |
except Exception as e:
|
| 71 |
st.write(e)
|
|
|
|
| 74 |
temp_dir = tempfile.TemporaryDirectory()
|
| 75 |
# ! working.
|
| 76 |
uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name
|
| 77 |
+
with open('./upload.csv', 'wb') as output_temporary_file:
|
|
|
|
| 78 |
# ! 必须用这种格式读入内容,然后才可以写入temporary文件夹中。
|
| 79 |
+
# output_temporary_file.write(uploaded_file.getvalue())
|
| 80 |
output_temporary_file.write(uploaded_file.getvalue())
|
| 81 |
+
# st.write(uploaded_file_path) # * 可以查看文件是否真实存在,然后是否可以
|
| 82 |
+
|
| 83 |
+
# output_temporary_file.close()
|
| 84 |
+
# new_file = tempfile.NamedTemporaryFile(delete=False)
|
| 85 |
+
# new_file.write(uploaded_file.getvalue())
|
| 86 |
|
| 87 |
+
# os.write(output_temporary_file, uploaded_file.getvalue())
|
| 88 |
+
# temp_file = tempfile.NamedTemporaryFile(dir=str(uploaded_file_path), delete=False)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
st.write('Now file saved successfully.')
|
| 92 |
+
# sleep(1000)
|
| 93 |
+
# fd = os.open(uploaded_file_path, os.O_WRONLY)
|
| 94 |
+
# os.write(fd, uploaded_file.getvalue())
|
| 95 |
+
# os.close(fd)
|
| 96 |
+
|
| 97 |
+
# return uploaded_file_path
|
| 98 |
+
# return new_file
|
| 99 |
+
# return new_file
|
| 100 |
|
| 101 |
|
| 102 |
bing_search_api_key = os.environ['bing_api_key']
|
|
|
|
| 275 |
|
| 276 |
async def data_mode(uploaded_file_path):
|
| 277 |
print('数据分析模式启动!')
|
| 278 |
+
uploaded_file_path = './upload.csv'
|
| 279 |
## TODO: validate the existence of uploaded file.
|
| 280 |
+
# st.write(f"passed file path in data_mode: {uploaded_file_path}")
|
| 281 |
+
tmp1 = pd.read_csv('./upload.csv')
|
| 282 |
st.write(tmp1[:5])
|
| 283 |
|
| 284 |
# Initialize chat history
|