Redmind commited on
Commit
1cc4979
·
verified ·
1 Parent(s): edcc7cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -63
app.py CHANGED
@@ -1,75 +1,90 @@
1
- """from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import gradio as gr
3
- import torch
4
 
5
- model_name = "tiiuae/falcon-7b-instruct"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(
8
- model_name,
9
- device_map="auto",
10
- torch_dtype=torch.float16
11
- )
12
 
13
- def convert_to_spoken_hindi(formal_hindi_text):
14
- prompt = f"Convert the following formal Hindi text into conversational spoken Hindi:\n\nFormal Hindi: {formal_hindi_text}\n\nSpoken Hindi:"
15
- inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
16
- outputs = model.generate(
17
- inputs["input_ids"],
18
- attention_mask=inputs["attention_mask"],
19
- max_length=150,
20
- num_beams=5,
21
- temperature=0.7
22
- )
23
- spoken_hindi = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
- return spoken_hindi.split("Spoken Hindi:")[-1].strip()
 
 
 
 
 
 
25
 
26
- iface = gr.Interface(
27
- fn=convert_to_spoken_hindi,
28
- inputs="text",
29
- outputs="text",
30
- title="Hindi Text Converter"
31
- )
32
 
33
- iface.launch()
34
- from transformers import pipeline, MBartTokenizer
35
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Load the pre-trained mBART model and tokenizer
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
- def convert_to_casual_hindi(text):
43
- # Use the model to transform the text dynamically
44
- transformed_text = model(text)
45
- return transformed_text[0]['generated_text']
46
 
47
- # Gradio interface for deployment in Hugging Face Spaces
48
- iface = gr.Interface(fn=convert_to_casual_hindi, inputs="text", outputs="text", title="Formal to Casual Hindi Converter")
49
- iface.launch()
50
- """
51
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
52
 
53
- # Use the IndicTrans model
54
- model_name = "ai4bharat/indictrans-hin-eng" # IndicTrans for Hindi
55
- tokenizer = AutoTokenizer.from_pretrained(model_name)
56
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
57
 
58
- def formal_to_casual_hindi(input_text):
59
- # Prepare input for IndicTrans
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
- # Gradio interface for the deployment
67
- iface = gr.Interface(
68
- fn=formal_to_casual_hindi,
69
- inputs="text",
70
- outputs="text",
71
- title="Formal to Casual Hindi Converter",
72
- description="Convert formal Hindi text into conversational Hindi using AI."
73
- )
 
 
 
 
 
74
 
75
- iface.launch()
 
 
 
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()