Raveheart1 commited on
Commit
60efbef
·
verified ·
1 Parent(s): 0d87c72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -1,18 +1,16 @@
1
  import requests
2
  import io
3
- from PIL import Image
4
  import gradio as gr
5
  from transformers import MarianMTModel, MarianTokenizer
6
  import os
7
 
8
- # Model and language settings
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  model = MarianMTModel.from_pretrained(model_name)
11
  tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
-
14
  def translate_text(input_text, language):
15
-
16
  language_map = {
17
  "Tamil": "ta",
18
  "French": "fr",
@@ -20,12 +18,10 @@ def translate_text(input_text, language):
20
  "German": "de"
21
  }
22
 
23
-
24
- lang_prefix = f">>{language_map[language]}<<"
25
  text_with_lang = lang_prefix + input_text
26
  inputs = tokenizer(text_with_lang, return_tensors="pt", padding=True)
27
-
28
-
29
  translated_tokens = model.generate(**inputs)
30
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
31
  return translation
@@ -56,27 +52,34 @@ def query_image(payload):
56
  return response.content
57
 
58
 
59
- def process_input(input_text, language):
60
  gemini_api_key = os.getenv('GEMINI_API_KEY')
61
- translated_output = translate_text(input_text, language)
62
- creative_output = query_gemini_api(translated_output, gemini_api_key)
63
- image_bytes = query_image({"inputs": translated_output})
64
- image = Image.open(io.BytesIO(image_bytes))
65
-
66
- return translated_output, creative_output, image
 
 
67
 
 
68
 
 
69
  iface = gr.Interface(
70
  fn=process_input,
71
- inputs=[gr.Textbox(label="Enter text in the selected language"),
72
- gr.Dropdown(choices=["Tamil", "French", "Hindi", "German"], label="Select Source Language")],
 
 
73
  outputs=[
74
- gr.Textbox(label="Translated Text (English)"),
75
  gr.Textbox(label="Creative Text"),
76
  gr.Image(label="Generated Image")
77
  ],
78
- title="TRANSART🎨 by Sakthi",
79
- description="Select a language, enter text, and get the English translation along with creative content and image."
80
  )
81
 
82
  iface.launch()
 
 
1
  import requests
2
  import io
3
+ from PIL import Image, UnidentifiedImageError
4
  import gradio as gr
5
  from transformers import MarianMTModel, MarianTokenizer
6
  import os
7
 
8
+
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  model = MarianMTModel.from_pretrained(model_name)
11
  tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
 
13
  def translate_text(input_text, language):
 
14
  language_map = {
15
  "Tamil": "ta",
16
  "French": "fr",
 
18
  "German": "de"
19
  }
20
 
21
+ lang_prefix = f">>{language_map[language]}<< "
 
22
  text_with_lang = lang_prefix + input_text
23
  inputs = tokenizer(text_with_lang, return_tensors="pt", padding=True)
24
+
 
25
  translated_tokens = model.generate(**inputs)
26
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
27
  return translation
 
52
  return response.content
53
 
54
 
55
+ def process_input(tamil_input, language):
56
  gemini_api_key = os.getenv('GEMINI_API_KEY')
57
+ translated_output = translate_text(tamil_input, language)
58
+ creative_output = query_gemini_api(translated_output, gemini_api_key)
59
+ image_bytes = query_image({"inputs": translated_output})
60
+
61
+ try:
62
+ image = Image.open(io.BytesIO(image_bytes))
63
+ except UnidentifiedImageError:
64
+ image = None
65
 
66
+ return translated_output, creative_output, image
67
 
68
+ # Gradio interface setup
69
  iface = gr.Interface(
70
  fn=process_input,
71
+ inputs=[
72
+ gr.Textbox(label="Input Text"),
73
+ gr.Dropdown(label="Select Language", choices=["Tamil", "French", "Hindi", "German"])
74
+ ],
75
  outputs=[
76
+ gr.Textbox(label="Translated Text"),
77
  gr.Textbox(label="Creative Text"),
78
  gr.Image(label="Generated Image")
79
  ],
80
+ title="TRANSART🎨 BY Sakthi",
81
+ description="Enter text to translate into English and generate an image based on the translated text."
82
  )
83
 
84
  iface.launch()
85
+