youngtsai commited on
Commit
00605bc
1 Parent(s): ea01f67

client = OpenAI(api_key=OPEN_AI_KEY)

Browse files
Files changed (2) hide show
  1. app.py +11 -8
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import json
3
  import os
4
- import openai
5
  import re
6
  from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, AudioConfig
7
 
@@ -14,6 +14,8 @@ AZURE_REGION = os.environ['AZURE_REGION']
14
  AZURE_API_KEY = os.environ['AZURE_API_KEY']
15
 
16
 
 
 
17
  def validate_and_correct_chat(data, roles=["A", "B"], rounds=2):
18
  """
19
  Corrects the chat data to ensure proper roles and number of rounds.
@@ -55,7 +57,7 @@ def extract_json_from_response(response_text):
55
 
56
 
57
  def create_chat_dialogue(rounds, role1, role1_gender, role2, role2_gender, theme, language, cefr_level):
58
- openai.api_key = os.environ["OPEN_AI_KEY"]
59
 
60
  # 初始化對話
61
  sentenses_count = int(rounds) * 2
@@ -71,15 +73,16 @@ def create_chat_dialogue(rounds, role1, role1_gender, role2, role2_gender, theme
71
  print("=====messages=====")
72
 
73
 
74
- response = openai.ChatCompletion.create(
75
- model="gpt-3.5-turbo",
76
- messages=messages,
77
- max_tokens=int(500 * int(rounds)) # 設定一個較大的值,可根據需要調整
78
- )
79
 
 
80
  print(response)
81
 
82
- response_text = response.choices[0].message['content'].strip()
83
  extract_json = extract_json_from_response(response_text)
84
  dialogue = validate_and_correct_chat(data=extract_json, roles=[role1, role2], rounds=rounds)
85
 
 
1
  import gradio as gr
2
  import json
3
  import os
4
+ from openai import OpenAI
5
  import re
6
  from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, AudioConfig
7
 
 
14
  AZURE_API_KEY = os.environ['AZURE_API_KEY']
15
 
16
 
17
+
18
+
19
  def validate_and_correct_chat(data, roles=["A", "B"], rounds=2):
20
  """
21
  Corrects the chat data to ensure proper roles and number of rounds.
 
57
 
58
 
59
  def create_chat_dialogue(rounds, role1, role1_gender, role2, role2_gender, theme, language, cefr_level):
60
+ client = OpenAI(api_key=OPEN_AI_KEY)
61
 
62
  # 初始化對話
63
  sentenses_count = int(rounds) * 2
 
73
  print("=====messages=====")
74
 
75
 
76
+ request_payload = {
77
+ "model": "gpt-4-1106-preview",
78
+ "messages": messages,
79
+ "max_tokens": int(500 * int(rounds)) # 設定一個較大的值,可根據需要調整
80
+ }
81
 
82
+ response = client.chat.completions.create(**request_payload)
83
  print(response)
84
 
85
+ response_text = response.choices[0].message.content.strip()
86
  extract_json = extract_json_from_response(response_text)
87
  dialogue = validate_and_correct_chat(data=extract_json, roles=[role1, role2], rounds=rounds)
88
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  gradio
2
  gtts
3
- openai
4
  azure-cognitiveservices-speech
 
1
  gradio
2
  gtts
3
+ openai >= 1.0.0
4
  azure-cognitiveservices-speech