Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 LLaMA text generation
|
| 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 |
+
# Load the LLaMA model for text generation
|
| 17 |
+
model_id = "meta-llama/Llama-3.2-1B" # Use LLaMA model for text generation
|
| 18 |
+
pipe = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model=model_id,
|
| 21 |
+
torch_dtype=torch.bfloat16, # Using bfloat16 for reduced memory footprint
|
| 22 |
+
device_map="auto" # Automatically assign devices for multi-GPU or CPU fallback
|
| 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('hf_tokens')}"}
|
| 28 |
+
|
| 29 |
+
# Define the translation, text generation, and image generation function
|
| 30 |
+
def translate_and_generate_image(tamil_text):
|
| 31 |
+
# Step 1: Translate Tamil text to English using mbart-large-50
|
| 32 |
+
tokenizer.src_lang = "ta_IN"
|
| 33 |
+
inputs = tokenizer(tamil_text, return_tensors="pt")
|
| 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 LLaMA model
|
| 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
|
| 41 |
+
def query(payload):
|
| 42 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 43 |
+
return response.content
|
| 44 |
+
|
| 45 |
+
# Generate image using the generated text
|
| 46 |
+
image_bytes = query({"inputs": generated_text})
|
| 47 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 48 |
+
|
| 49 |
+
return translated_text, generated_text, image
|
| 50 |
+
|
| 51 |
+
# Gradio interface setup
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=translate_and_generate_image,
|
| 54 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
|
| 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 with LLaMA, and Image Creation",
|
| 59 |
+
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate descriptive text using Meta's LLaMA model, and create an image using the generated text.",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Launch Gradio app with a shareable link
|
| 63 |
+
iface.launch(share=True)
|