Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,29 @@
|
|
| 1 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 2 |
-
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
import os
|
| 9 |
-
import torch # For
|
| 10 |
|
| 11 |
# Load the translation model and tokenizer
|
| 12 |
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
| 13 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
|
| 14 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
pipe = pipeline(
|
| 19 |
"text-generation",
|
| 20 |
-
model=
|
| 21 |
-
torch_dtype=torch.bfloat16,
|
| 22 |
-
device_map="auto"
|
| 23 |
)
|
| 24 |
|
| 25 |
# Use the Hugging Face API key from environment variables for text-to-image model
|
| 26 |
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
| 27 |
-
headers = {"Authorization": f"Bearer {os.getenv('
|
| 28 |
|
| 29 |
# Define the translation, text generation, and image generation function
|
| 30 |
def translate_and_generate_image(tamil_text):
|
|
@@ -34,7 +33,7 @@ def translate_and_generate_image(tamil_text):
|
|
| 34 |
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
| 35 |
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 36 |
|
| 37 |
-
# Step 2: Generate descriptive English text using
|
| 38 |
generated_text = pipe(translated_text, max_length=100, num_return_sequences=1)[0]['generated_text']
|
| 39 |
|
| 40 |
# Step 3: Use the generated English text to create an image
|
|
@@ -55,8 +54,8 @@ iface = gr.Interface(
|
|
| 55 |
outputs=[gr.Textbox(label="Translated English Text"),
|
| 56 |
gr.Textbox(label="Generated Descriptive Text"),
|
| 57 |
gr.Image(label="Generated Image")],
|
| 58 |
-
title="Tamil to English Translation, Text Generation
|
| 59 |
-
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate descriptive text using
|
| 60 |
)
|
| 61 |
|
| 62 |
# Launch Gradio app with a shareable link
|
|
|
|
| 1 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 2 |
+
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, pipeline
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
+
import torch # For text generation models
|
| 9 |
|
| 10 |
# Load the translation model and tokenizer
|
| 11 |
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
| 12 |
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
|
| 13 |
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 14 |
|
| 15 |
+
# Use GPT-2 for text generation instead of LLaMA
|
| 16 |
+
text_gen_model = "gpt2"
|
| 17 |
pipe = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
+
model=text_gen_model,
|
| 20 |
+
torch_dtype=torch.bfloat16,
|
| 21 |
+
device_map="auto"
|
| 22 |
)
|
| 23 |
|
| 24 |
# Use the Hugging Face API key from environment variables for text-to-image model
|
| 25 |
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
| 26 |
+
headers = {"Authorization": f"Bearer {os.getenv('hf_token')}"}
|
| 27 |
|
| 28 |
# Define the translation, text generation, and image generation function
|
| 29 |
def translate_and_generate_image(tamil_text):
|
|
|
|
| 33 |
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
| 34 |
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 35 |
|
| 36 |
+
# Step 2: Generate descriptive English text using GPT-2
|
| 37 |
generated_text = pipe(translated_text, max_length=100, num_return_sequences=1)[0]['generated_text']
|
| 38 |
|
| 39 |
# Step 3: Use the generated English text to create an image
|
|
|
|
| 54 |
outputs=[gr.Textbox(label="Translated English Text"),
|
| 55 |
gr.Textbox(label="Generated Descriptive Text"),
|
| 56 |
gr.Image(label="Generated Image")],
|
| 57 |
+
title="Tamil to English Translation, Text Generation, and Image Creation",
|
| 58 |
+
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate descriptive text using GPT-2, and create an image using the generated text.",
|
| 59 |
)
|
| 60 |
|
| 61 |
# Launch Gradio app with a shareable link
|