Spaces:
Sleeping
Sleeping
| import openai | |
| import gradio as gr | |
| import os | |
| # Set your OpenAI API key | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def translate_dutch_to_hindi(dutch_text, model="gpt-4o-mini"): | |
| """ | |
| Translates Dutch text to both conversational and formal Hindi using OpenAI. | |
| Args: | |
| - dutch_text (str): The text in Dutch to be translated. | |
| - model (str): The model to use for translation (e.g., gpt-3.5-turbo, gpt-4-mini). | |
| Returns: | |
| - tuple: (Conversational Hindi Translation, Formal Hindi Translation) | |
| """ | |
| try: | |
| # Prompt for Conversational Hindi | |
| conversational_prompt = ( | |
| f"Translate the following Dutch text to Hindi in a conversational style used in Suriname country:\n\n" | |
| f"Dutch: {dutch_text}\n" | |
| f"Hindi:" | |
| ) | |
| # Prompt for Formal Hindi | |
| formal_prompt = ( | |
| f"Translate the following Dutch text to Hindi in a formal style:\n\n" | |
| f"Dutch: {dutch_text}\n" | |
| f"Hindi:" | |
| ) | |
| # OpenAI API call for Conversational Hindi | |
| conversational_response = openai.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| {"role": "system", "content": "You are a translator converting dutch text to conversational hindi just like two common people talking between them."}, | |
| {"role": "user", "content": conversational_prompt} | |
| ], | |
| temperature=0.5 | |
| ) | |
| conversational_hindi = conversational_response.choices[0].message.content | |
| # OpenAI API call for Formal Hindi | |
| formal_response = openai.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| {"role": "system", "content": "You are a professional translator."}, | |
| {"role": "user", "content": formal_prompt} | |
| ], | |
| temperature=0.5 | |
| ) | |
| formal_hindi = formal_response.choices[0].message.content | |
| return conversational_hindi, formal_hindi | |
| except openai.OpenAIError as e: # Corrected exception class | |
| error_message = f"An error occurred: {e}" | |
| return error_message, error_message | |
| # Define the Gradio interface | |
| def gradio_interface(dutch_text): | |
| conversational_hindi, formal_hindi = translate_dutch_to_hindi(dutch_text) | |
| return conversational_hindi, formal_hindi | |
| # Create the Gradio app | |
| with gr.Blocks() as app: | |
| gr.Markdown("## Dutch to Hindi Translator") | |
| gr.Markdown("Enter Dutch text below and click 'Translate' to see both Conversational and Formal Hindi translations.") | |
| # Input text box for Dutch text | |
| input_text = gr.Textbox(label="Enter Dutch Text", placeholder="Type Dutch text here...") | |
| # Output text boxes for Conversational and Formal Hindi | |
| output_conversational = gr.Textbox(label="Conversational Hindi", placeholder="Conversational Hindi translation will appear here...") | |
| output_formal = gr.Textbox(label="Formal Hindi", placeholder="Formal Hindi translation will appear here...") | |
| # Translate button | |
| translate_button = gr.Button("Translate") | |
| # Button click event | |
| translate_button.click( | |
| fn=gradio_interface, | |
| inputs=[input_text], | |
| outputs=[output_conversational, output_formal] | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| app.launch() | |