Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,73 +12,68 @@ if not hf_api_key:
|
|
12 |
raise ValueError("Hugging Face API key not found! Please set the 'HF_API_KEY' environment variable.")
|
13 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
14 |
|
15 |
-
# Define the text-to-image model
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
translation_model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
|
21 |
translation_model = MBartForConditionalGeneration.from_pretrained(translation_model_name)
|
22 |
|
23 |
-
# Load a text generation model from Hugging Face
|
24 |
text_generation_model_name = "EleutherAI/gpt-neo-2.7B"
|
25 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
26 |
-
text_model = AutoModelForCausalLM.from_pretrained(
|
27 |
-
text_generation_model_name,
|
28 |
-
device_map="auto",
|
29 |
-
torch_dtype=torch.float32
|
30 |
-
)
|
31 |
|
32 |
# Create a pipeline for text generation
|
33 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
34 |
|
35 |
# Function to generate an image using Hugging Face's text-to-image model
|
36 |
def generate_image_from_text(translated_text):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
#
|
49 |
-
def
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
return f"Error during translation: {e}", None, None
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
return translated_text, None, f"Error during image generation: {e}"
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
gr.Image(label="Generated Image"),
|
78 |
-
gr.Textbox(label="Generated Descriptive Text")],
|
79 |
-
title="Tamil to English Translation, Image Creation, and Descriptive Text Generation",
|
80 |
-
description="Translate Tamil text to English using Facebook's mbart-large-50 model, create an image using the translated text, and generate a descriptive text based on the translated content.",
|
81 |
-
)
|
82 |
-
|
83 |
-
# Launch the Gradio app
|
84 |
-
iface.launch()
|
|
|
12 |
raise ValueError("Hugging Face API key not found! Please set the 'HF_API_KEY' environment variable.")
|
13 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
14 |
|
15 |
+
# Define the text-to-image model URLs
|
16 |
+
model_urls = {
|
17 |
+
"stable_diffusion_v1_4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
|
18 |
+
"stable_diffusion_v1_5": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
|
19 |
+
}
|
20 |
+
API_URL = model_urls["stable_diffusion_v1_4"]
|
21 |
|
22 |
+
# Define the translation model for multilingual text inputs
|
23 |
translation_model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
|
25 |
translation_model = MBartForConditionalGeneration.from_pretrained(translation_model_name)
|
26 |
|
27 |
+
# Load a text generation model from Hugging Face
|
28 |
text_generation_model_name = "EleutherAI/gpt-neo-2.7B"
|
29 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
30 |
+
text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name, device_map="auto", torch_dtype=torch.float32)
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Create a pipeline for text generation
|
33 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
34 |
|
35 |
# Function to generate an image using Hugging Face's text-to-image model
|
36 |
def generate_image_from_text(translated_text):
|
37 |
+
payload = {"inputs": translated_text, "options": {"wait_for_model": True}}
|
38 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
39 |
+
if response.status_code == 200:
|
40 |
+
image_data = response.content
|
41 |
+
image = Image.open(io.BytesIO(image_data))
|
42 |
+
return image
|
43 |
+
else:
|
44 |
+
# If the model is loading, check the estimated wait time
|
45 |
+
if response.status_code == 503:
|
46 |
+
error_message = response.json()
|
47 |
+
estimated_time = error_message.get("estimated_time", "Unknown")
|
48 |
+
return f"Model is currently loading. Estimated wait time: {estimated_time} seconds. Try again later."
|
49 |
+
else:
|
50 |
+
return f"Failed to generate image. Error: {response.status_code}, Message: {response.text}"
|
51 |
|
52 |
+
# Function to translate text using the MBart model
|
53 |
+
def translate_text(input_text, src_lang="en"):
|
54 |
+
# Tokenize and translate
|
55 |
+
tokenizer.src_lang = src_lang
|
56 |
+
encoded_input = tokenizer(input_text, return_tensors="pt")
|
57 |
+
translated_tokens = translation_model.generate(**encoded_input)
|
58 |
+
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
59 |
+
return translated_text
|
|
|
60 |
|
61 |
+
# Function to generate text using the GPT-Neo model
|
62 |
+
def generate_text(prompt, max_length=150):
|
63 |
+
generated_texts = text_generator(prompt, max_length=max_length, num_return_sequences=1)
|
64 |
+
return generated_texts[0]["generated_text"]
|
|
|
|
|
65 |
|
66 |
+
# Define the Gradio Interface
|
67 |
+
def app_interface(input_text, src_language="en"):
|
68 |
+
translated_text = translate_text(input_text, src_lang=src_language)
|
69 |
+
generated_image = generate_image_from_text(translated_text)
|
70 |
+
generated_text = generate_text(translated_text)
|
71 |
+
return generated_text, generated_image
|
72 |
|
73 |
+
# Launch the Gradio App
|
74 |
+
gr.Interface(
|
75 |
+
fn=app_interface,
|
76 |
+
inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter text here..."), gr.inputs.Dropdown(["en", "fr", "de", "es"], default="en", label="Source Language")],
|
77 |
+
outputs=[gr.outputs.Textbox(label="Generated Text"), gr.outputs.Image(label="Generated Image")],
|
78 |
+
title="Multilingual Text-to-Image & Text Generation"
|
79 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|