Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,90 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
model = AutoModelForCausalLM.from_pretrained(
|
8 |
-
model_name,
|
9 |
-
device_map="auto",
|
10 |
-
torch_dtype=torch.float16
|
11 |
-
)
|
12 |
|
13 |
-
def
|
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 |
-
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
39 |
-
tokenizer = MBartTokenizer.from_pretrained(model_name)
|
40 |
-
model = pipeline("text2text-generation", model=model_name, tokenizer=tokenizer)
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
return transformed_text[0]['generated_text']
|
46 |
|
47 |
-
# Gradio interface
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
prompt = f"Convert formal Hindi to casual Hindi: {input_text}"
|
61 |
-
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
62 |
-
outputs = model.generate(input_ids, max_length=128, num_beams=5, early_stopping=True)
|
63 |
-
casual_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
-
return casual_text
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
|
|
|
|
|
1 |
+
import openai
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
+
# Set your OpenAI API key
|
5 |
+
openai.api_key = "your_openai_api_key"
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def translate_dutch_to_hindi(dutch_text, model="gpt-4-mini"):
|
8 |
+
"""
|
9 |
+
Translates Dutch text to both conversational and formal Hindi using OpenAI.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
- dutch_text (str): The text in Dutch to be translated.
|
13 |
+
- model (str): The model to use for translation (e.g., gpt-3.5-turbo, gpt-4-mini).
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
- tuple: (Conversational Hindi Translation, Formal Hindi Translation)
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
# Prompt for Conversational Hindi
|
20 |
+
conversational_prompt = (
|
21 |
+
f"Translate the following Dutch text to Hindi in a conversational style:\n\n"
|
22 |
+
f"Dutch: {dutch_text}\n"
|
23 |
+
f"Hindi:"
|
24 |
+
)
|
25 |
|
26 |
+
# Prompt for Formal Hindi
|
27 |
+
formal_prompt = (
|
28 |
+
f"Translate the following Dutch text to Hindi in a formal style:\n\n"
|
29 |
+
f"Dutch: {dutch_text}\n"
|
30 |
+
f"Hindi:"
|
31 |
+
)
|
32 |
|
33 |
+
# OpenAI API call for Conversational Hindi
|
34 |
+
conversational_response = openai.ChatCompletion.create(
|
35 |
+
model=model,
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": "You are a professional translator."},
|
38 |
+
{"role": "user", "content": conversational_prompt}
|
39 |
+
],
|
40 |
+
temperature=0.5
|
41 |
+
)
|
42 |
+
conversational_hindi = conversational_response['choices'][0]['message']['content'].strip()
|
43 |
+
|
44 |
+
# OpenAI API call for Formal Hindi
|
45 |
+
formal_response = openai.ChatCompletion.create(
|
46 |
+
model=model,
|
47 |
+
messages=[
|
48 |
+
{"role": "system", "content": "You are a professional translator."},
|
49 |
+
{"role": "user", "content": formal_prompt}
|
50 |
+
],
|
51 |
+
temperature=0.5
|
52 |
+
)
|
53 |
+
formal_hindi = formal_response['choices'][0]['message']['content'].strip()
|
54 |
|
55 |
+
return conversational_hindi, formal_hindi
|
|
|
|
|
|
|
56 |
|
57 |
+
except openai.error.OpenAIError as e:
|
58 |
+
error_message = f"An error occurred: {e}"
|
59 |
+
return error_message, error_message
|
|
|
60 |
|
61 |
+
# Define the Gradio interface
|
62 |
+
def gradio_interface(dutch_text):
|
63 |
+
conversational_hindi, formal_hindi = translate_dutch_to_hindi(dutch_text)
|
64 |
+
return conversational_hindi, formal_hindi
|
|
|
65 |
|
66 |
+
# Create the Gradio app
|
67 |
+
with gr.Blocks() as app:
|
68 |
+
gr.Markdown("## Dutch to Hindi Translator")
|
69 |
+
gr.Markdown("Enter Dutch text below and click 'Translate' to see both Conversational and Formal Hindi translations.")
|
70 |
|
71 |
+
# Input text box for Dutch text
|
72 |
+
input_text = gr.Textbox(label="Enter Dutch Text", placeholder="Type Dutch text here...")
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
# Output text boxes for Conversational and Formal Hindi
|
75 |
+
output_conversational = gr.Textbox(label="Conversational Hindi", placeholder="Conversational Hindi translation will appear here...")
|
76 |
+
output_formal = gr.Textbox(label="Formal Hindi", placeholder="Formal Hindi translation will appear here...")
|
77 |
+
|
78 |
+
# Translate button
|
79 |
+
translate_button = gr.Button("Translate")
|
80 |
+
|
81 |
+
# Button click event
|
82 |
+
translate_button.click(
|
83 |
+
fn=gradio_interface,
|
84 |
+
inputs=[input_text],
|
85 |
+
outputs=[output_conversational, output_formal]
|
86 |
+
)
|
87 |
|
88 |
+
# Launch the app
|
89 |
+
if __name__ == "__main__":
|
90 |
+
app.launch()
|