Ali-C137 commited on
Commit
de0aa6a
·
1 Parent(s): 00a5e07

updated Chat completion for openai

Browse files
Files changed (1) hide show
  1. app.py +26 -6
app.py CHANGED
@@ -159,12 +159,32 @@ with tab3:
159
  # Get 10 samples from the dataset for translation
160
  samples_to_translate = st.session_state.data.sample(10)['sentence'].tolist()
161
 
162
- # Perform automatic translation using OpenAI GPT-4 model
163
- auto_translations = [openai.Completion.create(
164
- engine="text-davinci-002", # Change engine if needed
165
- prompt=sentence,
166
- max_tokens=50 # Adjust max_tokens as needed
167
- )['choices'][0]['text'] for sentence in samples_to_translate]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  # Update the dataset with auto-translations
170
  st.session_state.data.loc[
 
159
  # Get 10 samples from the dataset for translation
160
  samples_to_translate = st.session_state.data.sample(10)['sentence'].tolist()
161
 
162
+ # System prompt for translation assistant
163
+ translation_prompt = """
164
+ You are a helpful AI-powered translation assistant designed for users seeking reliable translation assistance. Your primary function is to provide context-aware translations from Moroccan Arabic (Darija) to English.
165
+ """
166
+
167
+ auto_translations = []
168
+
169
+ for sentence in samples_to_translate:
170
+ # Create messages for the chat model
171
+ messages = [
172
+ {"role": "system", "content": translation_prompt},
173
+ {"role": "user", "content": f"Translate the following sentence to English: '{sentence}'"}
174
+ ]
175
+
176
+ # Perform automatic translation using OpenAI GPT-3.5-turbo model
177
+ response = openai.ChatCompletion.create(
178
+ model="gpt-3.5-turbo",
179
+ messages=messages,
180
+ api_key=openai_api_key
181
+ )
182
+
183
+ # Extract the translated text from the response
184
+ translated_text = response.choices[0].message['content'].strip()
185
+
186
+ # Append the translated text to the list
187
+ auto_translations.append(translated_text)
188
 
189
  # Update the dataset with auto-translations
190
  st.session_state.data.loc[