yosuke-i commited on
Commit
d9500e2
1 Parent(s): 2b297d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,27 +1,34 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import openai
4
  import tempfile
5
  import os
 
6
 
7
- # サンプルのOpenAI APIキー
8
- openai.api_key = "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"
9
 
10
  def process_csv(csv_file):
11
  # CSVファイルを読み込む
12
  df = pd.read_csv(csv_file.name)
13
 
14
- # GPT-4に入力を送信し、出力を取得
15
  outputs = []
16
  for _, row in df.iterrows():
17
  id = row['id']
18
  input_text = row['input']
19
 
20
- response = openai.ChatCompletion.create(
21
- model="gpt-4",
22
- messages=[{"role": "user", "content": input_text}]
23
- )
24
- output_text = response.choices[0].message.content
 
 
 
 
 
 
 
25
 
26
  outputs.append({'id': id, 'output': output_text})
27
 
 
1
  import gradio as gr
2
  import pandas as pd
 
3
  import tempfile
4
  import os
5
+ import requests
6
 
7
+ # OpenAI API キーを設定する
8
+ OPENAI_API_KEY = "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" # ここにあなたのOpenAI API キーを入力してください
9
 
10
  def process_csv(csv_file):
11
  # CSVファイルを読み込む
12
  df = pd.read_csv(csv_file.name)
13
 
14
+ # ChatGPT API に入力を送信し、出力を取得
15
  outputs = []
16
  for _, row in df.iterrows():
17
  id = row['id']
18
  input_text = row['input']
19
 
20
+ # ChatGPT API を直接呼び出す
21
+ headers = {
22
+ "Content-Type": "application/json",
23
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
24
+ }
25
+ data = {
26
+ "model": "gpt-3.5-turbo",
27
+ "messages": [{"role": "user", "content": input_text}]
28
+ }
29
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=data)
30
+ response_json = response.json()
31
+ output_text = response_json["choices"][0]["message"]["content"]
32
 
33
  outputs.append({'id': id, 'output': output_text})
34