Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,13 @@
|
|
1 |
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from PIL import Image
|
6 |
-
import os
|
7 |
|
8 |
# Load the translation model and tokenizer
|
9 |
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
10 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
|
11 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
12 |
|
13 |
-
# Use the Hugging Face API key from environment variables for text-to-image model
|
14 |
-
hf_api_key = os.getenv("new_hf_token")
|
15 |
-
if hf_api_key is None:
|
16 |
-
raise ValueError("Hugging Face API key not found! Please set 'full_token' environment variable.")
|
17 |
-
else:
|
18 |
-
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
19 |
-
|
20 |
-
# Define the text-to-image model URL (using a faster text-to-image model)
|
21 |
-
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
22 |
-
|
23 |
# Load a smaller text generation model to reduce generation time
|
24 |
text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
|
25 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
@@ -28,45 +16,69 @@ text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name)
|
|
28 |
# Create a pipeline for text generation using the selected model
|
29 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
30 |
|
31 |
-
#
|
|
|
|
|
|
|
|
|
32 |
def generate_image_from_text(translated_text):
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
except Exception as e:
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
def translate_text(input_text, src_lang="en_XX", tgt_lang="hi_IN"):
|
55 |
-
tokenizer.src_lang = src_lang
|
56 |
-
encoded_input = tokenizer(input_text, return_tensors="pt")
|
57 |
-
generated_tokens = model.generate(encoded_input["input_ids"], forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang])
|
58 |
-
return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
59 |
|
60 |
-
# Gradio
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
iface = gr.Interface(fn=translate_and_generate_image,
|
68 |
-
inputs="text",
|
69 |
-
outputs="image",
|
70 |
-
title="Yoga Image Generator",
|
71 |
-
description="Enter a description to translate and generate a high-quality yoga image.")
|
72 |
iface.launch()
|
|
|
1 |
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from diffusers import FluxPipeline
|
|
|
|
|
5 |
|
6 |
# Load the translation model and tokenizer
|
7 |
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
8 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
|
9 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Load a smaller text generation model to reduce generation time
|
12 |
text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
|
13 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
|
|
16 |
# Create a pipeline for text generation using the selected model
|
17 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
18 |
|
19 |
+
# Set up the new FluxPipeline for the text-to-image model
|
20 |
+
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
|
21 |
+
pipe.enable_model_cpu_offload() # Enable CPU offloading to save GPU memory if needed
|
22 |
+
|
23 |
+
# Function to generate an image using the new FluxPipeline model
|
24 |
def generate_image_from_text(translated_text):
|
25 |
try:
|
26 |
+
print(f"Generating image from translated text: {translated_text}")
|
27 |
+
# Use the FluxPipeline to generate an image from the text
|
28 |
+
image = pipe(translated_text).images[0]
|
29 |
+
print("Image generation completed.")
|
30 |
+
return image, None
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error during image generation: {e}")
|
33 |
+
return None, f"Error during image generation: {e}"
|
34 |
+
|
35 |
+
# Function to generate a shorter paragraph based on the translated text
|
36 |
+
def generate_short_paragraph_from_text(translated_text):
|
37 |
+
try:
|
38 |
+
print(f"Generating a short paragraph from translated text: {translated_text}")
|
39 |
+
# Generate a shorter paragraph from the translated text using smaller settings
|
40 |
+
paragraph = text_generator(translated_text, max_length=150, num_return_sequences=1, temperature=0.2, top_p=0.8)[0]['generated_text']
|
41 |
+
print(f"Paragraph generation completed: {paragraph}")
|
42 |
+
return paragraph
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Error during paragraph generation: {e}")
|
45 |
+
return f"Error during paragraph generation: {e}"
|
46 |
|
47 |
+
# Define the function to translate Tamil text, generate a short paragraph, and create an image
|
48 |
+
def translate_generate_paragraph_and_image(tamil_text):
|
49 |
+
# Step 1: Translate Tamil text to English using mbart-large-50
|
50 |
+
try:
|
51 |
+
print("Translating Tamil text to English...")
|
52 |
+
tokenizer.src_lang = "ta_IN"
|
53 |
+
inputs = tokenizer(tamil_text, return_tensors="pt")
|
54 |
+
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
55 |
+
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
56 |
+
print(f"Translation completed: {translated_text}")
|
57 |
except Exception as e:
|
58 |
+
return f"Error during translation: {e}", "", None, None
|
59 |
+
|
60 |
+
# Step 2: Generate a shorter paragraph based on the translated English text
|
61 |
+
paragraph = generate_short_paragraph_from_text(translated_text)
|
62 |
+
if "Error" in paragraph:
|
63 |
+
return translated_text, paragraph, None, None
|
64 |
+
|
65 |
+
# Step 3: Generate an image using the translated English text
|
66 |
+
image, error_message = generate_image_from_text(translated_text)
|
67 |
+
if error_message:
|
68 |
+
return translated_text, paragraph, None, error_message
|
69 |
|
70 |
+
return translated_text, paragraph, image, None
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# Gradio interface setup
|
73 |
+
iface = gr.Interface(
|
74 |
+
fn=translate_generate_paragraph_and_image,
|
75 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
|
76 |
+
outputs=[gr.Textbox(label="Translated English Text"),
|
77 |
+
gr.Textbox(label="Generated Short Paragraph"),
|
78 |
+
gr.Image(label="Generated Image")],
|
79 |
+
title="Tamil to English Translation, Short Paragraph Generation, and Image Creation",
|
80 |
+
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate a short paragraph, and create an image using the translated text.",
|
81 |
+
)
|
82 |
|
83 |
+
# Launch the Gradio app
|
|
|
|
|
|
|
|
|
|
|
84 |
iface.launch()
|