Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,20 +14,23 @@ def translate_to_japanese(api_key, text):
|
|
14 |
# Set the OpenAI API key
|
15 |
openai.api_key = api_key
|
16 |
|
17 |
-
# Define the
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
try:
|
21 |
# Call the OpenAI API to get the translation
|
22 |
-
response = openai.
|
23 |
-
|
24 |
-
|
25 |
max_tokens=150, # Adjust token limit for longer outputs
|
26 |
temperature=0.5
|
27 |
)
|
28 |
|
29 |
# Extract the translated text from the response
|
30 |
-
japanese_translation = response.choices[0].
|
31 |
return japanese_translation
|
32 |
|
33 |
except openai.error.OpenAIError as e:
|
|
|
14 |
# Set the OpenAI API key
|
15 |
openai.api_key = api_key
|
16 |
|
17 |
+
# Define the messages for the chat model
|
18 |
+
messages = [
|
19 |
+
{"role": "system", "content": "You are a helpful translator."},
|
20 |
+
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
21 |
+
]
|
22 |
|
23 |
try:
|
24 |
# Call the OpenAI API to get the translation
|
25 |
+
response = openai.ChatCompletion.create(
|
26 |
+
model="gpt-3.5-turbo", # Use the correct endpoint for chat models
|
27 |
+
messages=messages,
|
28 |
max_tokens=150, # Adjust token limit for longer outputs
|
29 |
temperature=0.5
|
30 |
)
|
31 |
|
32 |
# Extract the translated text from the response
|
33 |
+
japanese_translation = response.choices[0].message['content'].strip()
|
34 |
return japanese_translation
|
35 |
|
36 |
except openai.error.OpenAIError as e:
|