Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
|
|
4 |
|
5 |
def process_csv(file_obj):
|
6 |
-
# アップロードされたCSVファイルをPandas DataFrameとして読み込む
|
7 |
df = pd.read_csv(file_obj)
|
8 |
-
|
9 |
-
# 'age'列の値を2倍にする
|
10 |
-
df['age'] = df['age'].apply(lambda x: x * 2)
|
11 |
-
|
12 |
-
# 結果を文字列として出力するため、StringIOを使用してCSV形式に変換
|
13 |
-
output = StringIO()
|
14 |
-
df.to_csv(output, index=False)
|
15 |
-
output.seek(0)
|
16 |
-
|
17 |
-
# StringIOオブジェクトの内容をバイト列として返す
|
18 |
-
return output.getvalue().encode('utf-8')
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# アプリの起動
|
27 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
|
6 |
def process_csv(file_obj):
|
|
|
7 |
df = pd.read_csv(file_obj)
|
8 |
+
df['age'] = df['age'] * 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# 一時ファイルを作成して結果を書き込む
|
11 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv', mode='w+') as tmp_file:
|
12 |
+
df.to_csv(tmp_file.name, index=False)
|
13 |
+
# 一時ファイルのパスを保持
|
14 |
+
temp_file_path = tmp_file.name
|
15 |
+
|
16 |
+
# 一時ファイルのパスを返却
|
17 |
+
return temp_file_path
|
18 |
+
|
19 |
+
iface = gr.Interface(fn=process_csv,
|
20 |
+
inputs=gr.File(label="Upload CSV"),
|
21 |
+
outputs=gr.File(label="Download Processed CSV", type="file"), # 'type'を'file'に設定
|
22 |
+
description="Upload a CSV with 'name' and 'age' columns. This app will double the 'age' and let you download the processed CSV.")
|
23 |
|
|
|
24 |
iface.launch()
|