youngtsai commited on
Commit
8109133
·
1 Parent(s): 1e07170

response = openai.Completion.create(

Browse files

engine="gpt-3.5-turbo",
prompt=prompt,
max_tokens=150 * rounds
)

Files changed (2) hide show
  1. app.py +31 -1
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,13 +2,43 @@ import gradio as gr
2
  from gtts import gTTS
3
  import json
4
  import os
 
5
 
6
  PASSWORD = os.environ['PASSWORD']
7
  OPEN_AI_KEY = os.environ['OPEN_AI_KEY']
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def generate_dialogue(rounds, method, role1, role2):
10
  if method == "auto":
11
- dialogue = [{"role": role1 if i % 2 == 0 else role2, "content": f"自動文本 {i+1}"} for i in range(rounds)]
12
  else:
13
  dialogue = [{"role": role1, "content": "手動輸入文本 1"}, {"role": role2, "content": "手動輸入文本 2"}]
14
  return dialogue
 
2
  from gtts import gTTS
3
  import json
4
  import os
5
+ import openai
6
 
7
  PASSWORD = os.environ['PASSWORD']
8
  OPEN_AI_KEY = os.environ['OPEN_AI_KEY']
9
 
10
+
11
+ def generate_dialogue_with_openai(rounds, role1, role2):
12
+ openai.api_key = os.environ["OPEN_AI_KEY"] # 從環境變數中取得API鑰匙
13
+
14
+ dialogue = []
15
+
16
+ # 這裡,我們使用一個基本的prompt,您可以根據需要修改它
17
+ prompt = "Write a conversation between two people, " + role1 + " and " + role2 + "."
18
+
19
+ response = openai.Completion.create(
20
+ engine="gpt-3.5-turbo",
21
+ prompt=prompt,
22
+ max_tokens=150 * rounds
23
+ )
24
+
25
+ # 解析OpenAI的回應以獲取對話內容
26
+ # 這只是一個基本的方法,您可能需要根據實際的回應內容進行調整
27
+ lines = response.choices[0].text.strip().split("\n")
28
+
29
+ for i, line in enumerate(lines[:rounds]):
30
+ if i % 2 == 0:
31
+ dialogue.append({"role": role1, "content": line.split(":")[1].strip()})
32
+ else:
33
+ dialogue.append({"role": role2, "content": line.split(":")[1].strip()})
34
+
35
+ return dialogue
36
+
37
+
38
+
39
  def generate_dialogue(rounds, method, role1, role2):
40
  if method == "auto":
41
+ dialogue = generate_dialogue_with_openai(rounds, role1, role2)
42
  else:
43
  dialogue = [{"role": role1, "content": "手動輸入文本 1"}, {"role": role2, "content": "手動輸入文本 2"}]
44
  return dialogue
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio
2
- gtts
 
 
1
  gradio
2
+ gtts
3
+ openai