Spaces:
Runtime error
Runtime error
client = OpenAI(api_key=OPEN_AI_KEY)
Browse files- app.py +11 -8
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
import os
|
4 |
-
import
|
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 |
-
|
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 |
-
|
75 |
-
model
|
76 |
-
messages
|
77 |
-
max_tokens
|
78 |
-
|
79 |
|
|
|
80 |
print(response)
|
81 |
|
82 |
-
response_text = response.choices[0].message
|
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
|