Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
|
| 8 |
+
# Ensure PyTorch runs on GPU if available
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
# Load translation model (Arabic to English) on GPU
|
| 12 |
+
translator = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en", device=0 if torch.cuda.is_available() else -1)
|
| 13 |
+
|
| 14 |
+
# Load image generation model
|
| 15 |
+
pipe = DiffusionPipeline.from_pretrained("sairajg/Text_To_Image")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def translate_and_generate(arabic_text):
|
| 20 |
+
|
| 21 |
+
translated_text = translator(arabic_text)[0]['translation_text']
|
| 22 |
+
result = pipe(translated_text).images[0]
|
| 23 |
+
|
| 24 |
+
return result, translated_text
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
|
| 28 |
+
# Ensure we get the actual image
|
| 29 |
+
# if isinstance(result, tuple):
|
| 30 |
+
# result = result[0] # Extract first element
|
| 31 |
+
# if isinstance(result, torch.Tensor):
|
| 32 |
+
# result = result.cpu().numpy() # Convert to NumPy array
|
| 33 |
+
# if isinstance(result, np.ndarray):
|
| 34 |
+
# result = Image.fromarray((result * 255).astype(np.uint8)) # Ensure proper pixel range
|
| 35 |
+
# elif not isinstance(result, Image.Image):
|
| 36 |
+
# raise ValueError(f"Unexpected output type: {type(result)}")
|
| 37 |
+
|
| 38 |
+
debug_info += f"Translated Prompt: {translated_text}\nResult Type: {type(result)}"
|
| 39 |
+
return result, debug_info
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return None, debug_info
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as interface:
|
| 45 |
+
gr.Markdown("### Arabic to Image Generator ")
|
| 46 |
+
|
| 47 |
+
text_input = gr.Textbox(label="Enter Arabic Prompt:", placeholder="اكتب هنا...")
|
| 48 |
+
generate_button = gr.Button("Generate Image ")
|
| 49 |
+
image_output = gr.Image(label="Generated Image")
|
| 50 |
+
text_output = gr.Textbox(label="Debug Output")
|
| 51 |
+
|
| 52 |
+
generate_button.click(translate_and_generate, inputs=text_input, outputs=[image_output, text_output])
|
| 53 |
+
|
| 54 |
+
interface.launch()
|