risdom / app.py
yosuke-i's picture
Update app.py
7af415b verified
raw
history blame
1.37 kB
import gradio as gr
import pandas as pd
import openai
import tempfile
import os
# サンプルのOpenAI APIキー
openai.api_key = "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"
def process_csv(csv_file):
# CSVファイルを読み込む
df = pd.read_csv(csv_file.name)
# GPT-4に入力を送信し、出力を取得
outputs = []
for _, row in df.iterrows():
id = row['id']
input_text = row['input']
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": input_text}]
)
output_text = response.choices[0].message.content
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ファイルをアップロード"),
outputs=gr.File(label="ダウンロード", file_count="singular"),
allow_flagging="never",
)
interface.launch(share=True)