import gradio as gr import pandas as pd import json import openai # OpenAI APIキーを設定(あなたのAPIキーをここに入力してください) openai.api_key = 'sk-proj-O9Qlw9lhCqMrWx4TNtLCT3BlbkFJDqrx6zKPfAv3ljoutKhi' def excel_to_json_and_convert(file, model): # エクセルファイルを読み込み df = pd.read_excel(file) # C列の2行目以降の値を取得 column_values = df.iloc[1:, 2].tolist() # JSON形式に変換 json_result = json.dumps(column_values, ensure_ascii=False, indent=2) # OpenAIのAPIに送信するプロンプトを作成 prompt = "次の内容を半角カタカナに変換してください:\n" + json_result # OpenAI GPT-3にリクエストを送信 response = openai.ChatCompletion.create( model=model, messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} ] ) # レスポンスからテキストを抽出 converted_text = response.choices[0].message['content'].strip() return converted_text # Gradioインターフェースの設定 interface = gr.Interface( fn=excel_to_json_and_convert, inputs=[ gr.File(label="Upload Excel File"), gr.Radio(choices=["gpt-3.5-turbo", "gpt-4o"], label="Select OpenAI Model") ], outputs=gr.Textbox(label="Converted Output"), title="Excel to JSON Converter and OpenAI GPT-3/4o Converter", description="Upload an Excel file and get JSON output of column C values converted to half-width Katakana by OpenAI GPT-3/4." ) # Gradioアプリの実行 interface.launch()