acecalisto3 commited on
Commit
773cf98
·
verified ·
1 Parent(s): c7f6047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -187,8 +187,24 @@ def sentiment_analysis(text):
187
  st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
188
  return sentiment[0]
189
 
190
- def translate_code(code, source_language, target_language):
191
- prompt = f"Translate this code from {source_language} to {target_language}:\n\n{code}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  response = openai.ChatCompletion.create(
193
  model="gpt-4",
194
  messages=[
@@ -197,6 +213,9 @@ def translate_code(code, source_language, target_language):
197
  ]
198
  )
199
  translated_code = response.choices[0].message['content'].strip()
 
 
 
200
  st.session_state.current_state['toolbox']['translated_code'] = translated_code
201
  return translated_code
202
 
 
187
  st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
188
  return sentiment[0]
189
 
190
+ def translate_code(code, input_language, output_language):
191
+ # Define a dictionary to map programming languages to their corresponding file extensions
192
+ language_extensions = {
193
+ # ignore the specific languages right now, and continue to EOF
194
+ }
195
+
196
+ # Add code to handle edge cases such as invalid input and unsupported programming languages
197
+ if input_language not in language_extensions:
198
+ raise ValueError(f"Invalid input language: {input_language}")
199
+ if output_language not in language_extensions:
200
+ raise ValueError(f"Invalid output language: {output_language}")
201
+
202
+ # Use the dictionary to map the input and output languages to their corresponding file extensions
203
+ input_extension = language_extensions[input_language]
204
+ output_extension = language_extensions[output_language]
205
+
206
+ # Translate the code using the OpenAI API
207
+ prompt = f"Translate this code from {input_language} to {output_language}:\n\n{code}"
208
  response = openai.ChatCompletion.create(
209
  model="gpt-4",
210
  messages=[
 
213
  ]
214
  )
215
  translated_code = response.choices[0].message['content'].strip()
216
+
217
+ # Return the translated code
218
+ translated_code = response.choices[0].message['content'].strip()
219
  st.session_state.current_state['toolbox']['translated_code'] = translated_code
220
  return translated_code
221