File size: 1,408 Bytes
a05078c
c702c67
44d92db
3d3b90b
d9500e2
d8d00f5
cda9e4a
c3befcf
 
58f3ac2
c3befcf
 
cda9e4a
c3befcf
 
 
 
cda9e4a
 
 
 
c3befcf
 
 
 
44d92db
c3befcf
 
44d92db
657f685
c3befcf
 
657f685
3d3b90b
d8d00f5
 
c3befcf
0ba4622
 
 
 
 
 
 
 
 
44d92db
3d3b90b
e347e02
234d720
c702c67
56b96d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
import pandas as pd
import tempfile
import os
import requests

from chatgpt_api import get_chatgpt_response

def process_csv(csv_file):
    # CSVファイルを読み込む
    df = pd.read_csv(csv_file.name)

    # ChatGPT APIに入力を送信し、出力を取得
    outputs = []
    for _, row in df.iterrows():
        id = row['id']
        input_text = row['input']

        # ChatGPT APIから応答を取得
        output_text = get_chatgpt_response(input_text)

        outputs.append({'id': id, 'output': output_text})

    # 出力をCSVファイルに書き込む
    output_df = pd.DataFrame(outputs)
    with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
        output_df.to_csv(tmp.name, index=False)
        output_path = tmp.name

    # ファイル名を変更
    new_path = os.path.join(os.path.dirname(output_path), "output.csv")
    os.rename(output_path, new_path)

    return new_path

interface = gr.Interface(
    fn=process_csv,
    inputs=[
        gr.File(label="CSVファイルをアップロード", file_count="single"),
        gr.File(
            source="open",
            label="サンプルCSVファイル",
            file_count="single",
            file_name="sample.csv",
        ),
    ],
    outputs=gr.File(label="ダウンロード", file_count="singular"),
    allow_flagging="never",

)

interface.launch(share=True)