|
import gradio as gr |
|
import pandas as pd |
|
import openai |
|
import tempfile |
|
import os |
|
|
|
|
|
openai.api_key = "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" |
|
|
|
def process_csv(csv_file): |
|
|
|
df = pd.read_csv(csv_file.name) |
|
|
|
|
|
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}) |
|
|
|
|
|
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) |