Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
|
4 |
from PIL import Image
|
5 |
import torch
|
6 |
-
import io
|
7 |
-
import os
|
8 |
-
from typing import Tuple
|
9 |
|
10 |
-
# Load
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Step 3: Generate image
|
45 |
-
def generate_image(prompt: str) -> Image.Image:
|
46 |
-
response = requests.post(IMAGE_GEN_URL, headers=HEADERS, json={"inputs": prompt})
|
47 |
-
if response.status_code == 200 and response.headers.get("content-type", "").startswith("image"):
|
48 |
-
return Image.open(io.BytesIO(response.content))
|
49 |
-
else:
|
50 |
-
return Image.new("RGB", (512, 512), color="gray")
|
51 |
-
|
52 |
-
# Master function
|
53 |
-
def process_input(tamil_text: str) -> Tuple[str, str, Image.Image]:
|
54 |
-
english = translate_tamil_to_english(tamil_text)
|
55 |
-
creative = generate_text(english)
|
56 |
-
image = generate_image(english)
|
57 |
-
return english, creative, image
|
58 |
-
|
59 |
-
# Gradio UI using Blocks API
|
60 |
with gr.Blocks() as demo:
|
61 |
-
gr.Markdown("##
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
with gr.Row():
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
creative_output = gr.Textbox(label="✨ Generated Text")
|
69 |
-
image_output = gr.Image(label="🖼️ Generated Image", type="pil")
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer, BlipProcessor, BlipForConditionalGeneration
|
|
|
3 |
from PIL import Image
|
4 |
import torch
|
|
|
|
|
|
|
5 |
|
6 |
+
# Load the Tamil-to-English translation model
|
7 |
+
model_name = "Helsinki-NLP/opus-mt-ta-en"
|
8 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
9 |
+
translation_model = MarianMTModel.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Load the BLIP model for image captioning
|
12 |
+
caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
13 |
+
caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
14 |
+
|
15 |
+
def translate_tamil_to_english(tamil_text):
|
16 |
+
inputs = tokenizer(tamil_text, return_tensors="pt", padding=True)
|
17 |
+
translated = translation_model.generate(**inputs)
|
18 |
+
english_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
19 |
+
return english_text
|
20 |
+
|
21 |
+
# Generate image using text (stub – replace with actual model if needed)
|
22 |
+
def generate_image_from_text(text_prompt):
|
23 |
+
# Instead of using Stable Diffusion, just show a sample image
|
24 |
+
img = Image.new('RGB', (512, 512), color='lightblue')
|
25 |
+
return img
|
26 |
+
|
27 |
+
def describe_image(image):
|
28 |
+
inputs = caption_processor(images=image, return_tensors="pt")
|
29 |
+
out = caption_model.generate(**inputs)
|
30 |
+
caption = caption_processor.decode(out[0], skip_special_tokens=True)
|
31 |
+
return caption
|
32 |
+
|
33 |
+
def full_pipeline(tamil_text):
|
34 |
+
english_text = translate_tamil_to_english(tamil_text)
|
35 |
+
generated_image = generate_image_from_text(english_text)
|
36 |
+
description = describe_image(generated_image)
|
37 |
+
return english_text, generated_image, description
|
38 |
+
|
39 |
+
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("## Tamil to English → Image → Description")
|
42 |
+
|
43 |
+
with gr.Row():
|
44 |
+
tamil_input = gr.Textbox(label="Enter Tamil Text", lines=2, placeholder="உதாரணம்: ஒரு பூந்தோட்டத்தில் செருப்புகள் இருக்கின்றன")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
translate_btn = gr.Button("Translate and Generate")
|
48 |
|
49 |
with gr.Row():
|
50 |
+
english_output = gr.Textbox(label="Translated English Text")
|
51 |
+
description_output = gr.Textbox(label="Image Description")
|
52 |
|
53 |
+
image_output = gr.Image(label="Generated Image")
|
|
|
|
|
54 |
|
55 |
+
translate_btn.click(
|
56 |
+
fn=full_pipeline,
|
57 |
+
inputs=tamil_input,
|
58 |
+
outputs=[english_output, image_output, description_output]
|
59 |
+
)
|
60 |
|
61 |
demo.launch()
|