Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,98 +14,80 @@ if hf_token is None:
|
|
14 |
# Login to Hugging Face
|
15 |
login(token=hf_token)
|
16 |
|
17 |
-
# Define
|
18 |
-
|
19 |
-
"
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
29 |
-
|
30 |
-
"Italian": "it"
|
31 |
}
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
#
|
39 |
-
def translate_text(input_text, src_lang):
|
40 |
-
try:
|
41 |
-
src_prefix = f">>{src_lang}<< " + input_text
|
42 |
-
translation = translator(src_prefix, max_length=40)
|
43 |
-
translated_text = translation[0]['translation_text']
|
44 |
-
return translated_text
|
45 |
-
except Exception as e:
|
46 |
-
return f"An error occurred: {str(e)}"
|
47 |
-
|
48 |
-
# API credentials and endpoint for FLUX
|
49 |
flux_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
50 |
flux_headers = {"Authorization": f"Bearer {hf_token}"}
|
51 |
|
52 |
-
# Function
|
53 |
-
def
|
54 |
try:
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
image = Image.open(io.BytesIO(image_bytes))
|
59 |
-
return image
|
60 |
else:
|
61 |
-
|
62 |
-
return None
|
63 |
-
except Exception as e:
|
64 |
-
print(f"An error occurred: {e}")
|
65 |
-
return None
|
66 |
|
67 |
-
|
68 |
-
mistral_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
|
69 |
-
mistral_headers = {"Authorization": f"Bearer {hf_token}"}
|
70 |
|
71 |
-
def generate_creative_text(translated_text):
|
72 |
-
try:
|
73 |
-
response = requests.post(mistral_API_URL, headers=mistral_headers, json={"inputs": translated_text})
|
74 |
-
if response.status_code == 200:
|
75 |
-
creative_text = response.json()[0]['generated_text']
|
76 |
-
return creative_text
|
77 |
-
else:
|
78 |
-
return "Error generating creative text"
|
79 |
except Exception as e:
|
80 |
-
return f"An error occurred: {str(e)}"
|
81 |
-
|
82 |
-
# Main function to handle full workflow
|
83 |
-
def translate_generate_image_and_text(input_text, src_lang):
|
84 |
-
# Step 1: Translate input text
|
85 |
-
translated_text = translate_text(input_text, language_codes[src_lang])
|
86 |
-
|
87 |
-
# Step 2: Generate an image
|
88 |
-
image = generate_image(translated_text)
|
89 |
-
|
90 |
-
# Step 3: Generate creative text based on the translation
|
91 |
-
creative_text = generate_creative_text(translated_text)
|
92 |
-
|
93 |
-
return translated_text, creative_text, image
|
94 |
|
95 |
-
# Gradio interface
|
96 |
interface = gr.Interface(
|
97 |
fn=translate_generate_image_and_text,
|
98 |
inputs=[
|
99 |
gr.Textbox(label="Enter text for translation"),
|
100 |
-
gr.
|
101 |
],
|
102 |
outputs=[
|
103 |
gr.Textbox(label="Translated Text"),
|
104 |
gr.Textbox(label="Creative Text"),
|
105 |
gr.Image(label="Generated Image")
|
106 |
],
|
107 |
-
title="Multilingual Translation,
|
108 |
-
description="
|
109 |
)
|
110 |
|
111 |
# Launch the Gradio app
|
|
|
14 |
# Login to Hugging Face
|
15 |
login(token=hf_token)
|
16 |
|
17 |
+
# Define models for specific languages
|
18 |
+
language_models = {
|
19 |
+
"fra": "Helsinki-NLP/opus-mt-fr-en",
|
20 |
+
"spa": "Helsinki-NLP/opus-mt-es-en",
|
21 |
+
"tam": "Helsinki-NLP/opus-mt-tam-en",
|
22 |
+
"deu": "Helsinki-NLP/opus-mt-de-en",
|
23 |
+
"jpn": "Helsinki-NLP/opus-mt-ja-en",
|
24 |
+
"rus": "Helsinki-NLP/opus-mt-ru-en",
|
25 |
+
"kor": "Helsinki-NLP/opus-mt-ko-en",
|
26 |
+
"hin": "Helsinki-NLP/opus-mt-hi-en",
|
27 |
+
"ita": "Helsinki-NLP/opus-mt-it-en",
|
28 |
+
"por": "Helsinki-NLP/opus-mt-pt-en"
|
29 |
+
# Add more language models as needed
|
|
|
30 |
}
|
31 |
|
32 |
+
# Function to get translator pipeline for specific language
|
33 |
+
def get_translator(language_code):
|
34 |
+
model_name = language_models.get(language_code)
|
35 |
+
if model_name:
|
36 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
37 |
+
model = MarianMTModel.from_pretrained(model_name)
|
38 |
+
return pipeline("translation", model=model, tokenizer=tokenizer)
|
39 |
+
else:
|
40 |
+
raise ValueError(f"No translation model found for language code '{language_code}'.")
|
41 |
|
42 |
+
# FLUX model API settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
flux_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
44 |
flux_headers = {"Authorization": f"Bearer {hf_token}"}
|
45 |
|
46 |
+
# Function for translation, creative text generation, and image creation
|
47 |
+
def translate_generate_image_and_text(input_text, src_lang_code):
|
48 |
try:
|
49 |
+
# Step 1: Get translator and translate text
|
50 |
+
translator = get_translator(src_lang_code)
|
51 |
+
translation = translator(input_text, max_length=40)
|
52 |
+
translated_text = translation[0]['translation_text']
|
53 |
+
|
54 |
+
# Step 2: Generate creative text with Mistral model
|
55 |
+
mistral_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
|
56 |
+
mistral_headers = {"Authorization": f"Bearer {hf_token}"}
|
57 |
+
mistral_response = requests.post(mistral_API_URL, headers=mistral_headers, json={"inputs": translated_text})
|
58 |
+
|
59 |
+
if mistral_response.status_code == 200:
|
60 |
+
creative_text = mistral_response.json()[0]['generated_text']
|
61 |
+
else:
|
62 |
+
creative_text = "Error generating creative text"
|
63 |
+
|
64 |
+
# Step 3: Generate an image with FLUX model
|
65 |
+
flux_response = requests.post(flux_API_URL, headers=flux_headers, json={"inputs": creative_text})
|
66 |
+
if flux_response.status_code == 200:
|
67 |
+
image_bytes = flux_response.content
|
68 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
69 |
else:
|
70 |
+
image = None
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
return translated_text, creative_text, image
|
|
|
|
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
except Exception as e:
|
75 |
+
return f"An error occurred: {str(e)}", None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Gradio interface setup
|
78 |
interface = gr.Interface(
|
79 |
fn=translate_generate_image_and_text,
|
80 |
inputs=[
|
81 |
gr.Textbox(label="Enter text for translation"),
|
82 |
+
gr.Textbox(label="Source Language Code", placeholder="e.g., 'fra' for French, 'spa' for Spanish")
|
83 |
],
|
84 |
outputs=[
|
85 |
gr.Textbox(label="Translated Text"),
|
86 |
gr.Textbox(label="Creative Text"),
|
87 |
gr.Image(label="Generated Image")
|
88 |
],
|
89 |
+
title="Multilingual Translation, Creative Content, and Image Generator",
|
90 |
+
description="Select a language and translate text to English, generate creative content, and produce an image."
|
91 |
)
|
92 |
|
93 |
# Launch the Gradio app
|