Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,77 @@
|
|
|
|
1 |
import requests
|
2 |
-
import io
|
3 |
from PIL import Image
|
|
|
4 |
import gradio as gr
|
5 |
-
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
|
6 |
-
import os
|
7 |
|
8 |
-
# Load the translation
|
9 |
model_name = "Helsinki-NLP/opus-mt-mul-en"
|
10 |
translation_model = MarianMTModel.from_pretrained(model_name)
|
11 |
translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
|
12 |
|
13 |
-
# Load GPT-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
def translate_text(tamil_text):
|
19 |
-
inputs = translation_tokenizer(tamil_text, return_tensors="pt")
|
20 |
translated_tokens = translation_model.generate(**inputs)
|
21 |
translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
22 |
return translation
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
return creative_text
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# Translate the input text
|
48 |
-
translated_output = translate_text(tamil_input)
|
49 |
-
|
50 |
-
# Generate creative text using GPT-2
|
51 |
-
creative_output = query_gpt_2(translated_output)
|
52 |
-
|
53 |
-
# Generate an image using Hugging Face's FLUX model
|
54 |
-
image_bytes = query_image({"inputs": translated_output})
|
55 |
-
image = Image.open(io.BytesIO(image_bytes))
|
56 |
-
|
57 |
-
return translated_output, creative_output, image
|
58 |
-
except Exception as e:
|
59 |
-
return f"Error occurred: {str(e)}", "", None
|
60 |
|
61 |
-
# Create
|
62 |
interface = gr.Interface(
|
63 |
-
fn=
|
64 |
-
inputs=
|
65 |
outputs=[
|
66 |
-
gr.Textbox(label="Translated Text"),
|
67 |
-
gr.Textbox(label="Creative Text")
|
68 |
-
gr.Image(label="Generated Image")
|
69 |
],
|
70 |
-
title="
|
71 |
-
description="Enter Tamil text to translate to English
|
72 |
)
|
73 |
|
74 |
-
|
|
|
|
1 |
+
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
|
2 |
import requests
|
|
|
3 |
from PIL import Image
|
4 |
+
import io
|
5 |
import gradio as gr
|
|
|
|
|
6 |
|
7 |
+
# Load the MarianMT model and tokenizer for translation (Tamil to English)
|
8 |
model_name = "Helsinki-NLP/opus-mt-mul-en"
|
9 |
translation_model = MarianMTModel.from_pretrained(model_name)
|
10 |
translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
|
11 |
|
12 |
+
# Load GPT-Neo for creative text generation
|
13 |
+
text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
|
14 |
+
text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name)
|
15 |
+
text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
16 |
+
|
17 |
+
# Add padding token to GPT-Neo tokenizer if not present
|
18 |
+
if text_generation_tokenizer.pad_token is None:
|
19 |
+
text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
20 |
+
|
21 |
+
# Hugging Face API for FLUX.1 image generation
|
22 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
23 |
+
headers = {"Authorization": "HUGGINGFACE_API_KEY"} # Replace with your API key
|
24 |
+
|
25 |
+
# Query Hugging Face API to generate image
|
26 |
+
def query(payload):
|
27 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
28 |
+
return response.content
|
29 |
|
30 |
+
# Translate Tamil text to English
|
31 |
def translate_text(tamil_text):
|
32 |
+
inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True, truncation=True)
|
33 |
translated_tokens = translation_model.generate(**inputs)
|
34 |
translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
35 |
return translation
|
36 |
|
37 |
+
# Generate an image based on the translated text
|
38 |
+
def generate_image(prompt):
|
39 |
+
image_bytes = query({"inputs": prompt})
|
40 |
+
image = Image.open(io.BytesIO(image_bytes))
|
41 |
+
return image
|
|
|
42 |
|
43 |
+
# Generate creative text based on the translated English text
|
44 |
+
def generate_creative_text(translated_text):
|
45 |
+
inputs = text_generation_tokenizer(translated_text, return_tensors="pt", padding=True, truncation=True)
|
46 |
+
generated_tokens = text_generation_model.generate(**inputs, max_length=100)
|
47 |
+
creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
48 |
+
return creative_text
|
49 |
|
50 |
+
# Function to handle the full workflow
|
51 |
+
def translate_generate_image_and_text(tamil_text):
|
52 |
+
# Step 1: Translate Tamil to English
|
53 |
+
translated_text = translate_text(tamil_text)
|
54 |
|
55 |
+
# Step 2: Generate an image from the translated text
|
56 |
+
image = generate_image(translated_text)
|
57 |
+
|
58 |
+
# Step 3: Generate creative text from the translated text
|
59 |
+
creative_text = generate_creative_text(translated_text)
|
60 |
+
|
61 |
+
return translated_text, creative_text, image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Create Gradio interface
|
64 |
interface = gr.Interface(
|
65 |
+
fn=translate_generate_image_and_text,
|
66 |
+
inputs=gr.Textbox(label="Enter Tamil Text"), # Input for Tamil text
|
67 |
outputs=[
|
68 |
+
gr.Textbox(label="Translated Text"), # Output for translated text
|
69 |
+
gr.Textbox(label="Creative Generated Text"),# Output for creative text
|
70 |
+
gr.Image(label="Generated Image") # Output for generated image
|
71 |
],
|
72 |
+
title="Tamil to English Translation, Image Generation & Creative Text",
|
73 |
+
description="Enter Tamil text to translate to English, generate an image, and create creative text based on the translation."
|
74 |
)
|
75 |
|
76 |
+
# Launch Gradio app
|
77 |
+
interface.launch()
|