Spaces:
Sleeping
Sleeping
File size: 3,336 Bytes
1cc4979 b87616e cba821a 57e3221 1cc4979 b005055 57e3221 88d30e6 1cc4979 5430209 1cc4979 77892d4 1cc4979 85cec45 1cc4979 25d1f46 1cc4979 77b13e9 1cc4979 5a9f41f 1cc4979 eddecbd 1cc4979 77b13e9 1cc4979 484b8ed 8a83248 484b8ed 8a83248 5430209 1cc4979 8a83248 1cc4979 560b692 1cc4979 48fa0fc 1cc4979 48fa0fc 1cc4979 48fa0fc 1cc4979 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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()
|