Spaces:
Runtime error
Runtime error
Raveheart1
commited on
Update app.py
Browse files
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 |
-
|
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(
|
60 |
gemini_api_key = os.getenv('GEMINI_API_KEY')
|
61 |
-
translated_output = translate_text(
|
62 |
-
creative_output = query_gemini_api(translated_output, gemini_api_key)
|
63 |
-
image_bytes = query_image({"inputs": translated_output})
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
iface = gr.Interface(
|
70 |
fn=process_input,
|
71 |
-
inputs=[
|
72 |
-
|
|
|
|
|
73 |
outputs=[
|
74 |
-
gr.Textbox(label="Translated Text
|
75 |
gr.Textbox(label="Creative Text"),
|
76 |
gr.Image(label="Generated Image")
|
77 |
],
|
78 |
-
title="TRANSART🎨
|
79 |
-
description="
|
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 |
+
|