import os import requests def translate_text(text, source_lang, target_lang): url = "https://google-translator9.p.rapidapi.com/v2" payload = { "q": text, "source": source_lang, "target": target_lang, "format": "text" } headers = { "x-rapidapi-key": os.getenv("RAPIDAPI_KEY"), "x-rapidapi-host": "google-translator9.p.rapidapi.com", "Content-Type": "application/json" } try: response = requests.post(url, json=payload, headers=headers) response.raise_for_status() print('=====================', response.json()) translations = response.json().get('data', {}).get('translations', []) if translations: translated_text = translations[0].get('translatedText', '') return translated_text else: return text except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") return text if __name__ == "__main__": text_to_translate = "Dans cette image donne moi l'œdème" source_language = "fr" target_language = "en" translation = translate_text(text_to_translate, source_language, target_language) if translation: print(translation)