Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,73 +2,76 @@ import google.generativeai as genai
|
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
-
# Initialize the
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
| 12 |
-
gemini_model = genai.GenerativeModel("gemini-1.5-flash-8b")
|
| 13 |
-
|
| 14 |
-
# Initialize Stable Diffusion for CPU
|
| 15 |
-
model_id = "CompVis/stable-diffusion-v1-4"
|
| 16 |
-
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 17 |
-
model_id,
|
| 18 |
-
torch_dtype=torch.float32
|
| 19 |
-
)
|
| 20 |
-
print("Model Initialization Done")
|
| 21 |
-
|
| 22 |
-
return gemini_model, pipeline
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
response =
|
| 28 |
-
f"Translate this Tamil text to English: {tamil_text}"
|
| 29 |
-
)
|
| 30 |
-
print("Translation Done")
|
| 31 |
return response.text
|
| 32 |
except Exception as e:
|
| 33 |
return f"Translation error: {str(e)}"
|
| 34 |
|
| 35 |
-
def
|
| 36 |
try:
|
| 37 |
-
image
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return image
|
| 40 |
except Exception as e:
|
|
|
|
| 41 |
return None
|
| 42 |
|
| 43 |
-
def
|
| 44 |
if not tamil_text.strip():
|
| 45 |
return "Please enter some text", None
|
| 46 |
|
| 47 |
try:
|
| 48 |
# Translate Tamil to English
|
| 49 |
-
english_translation =
|
| 50 |
print(english_translation)
|
| 51 |
|
| 52 |
-
# Generate image based on translation
|
| 53 |
if english_translation.startswith("Translation error"):
|
| 54 |
return english_translation, None
|
| 55 |
|
| 56 |
-
image
|
|
|
|
| 57 |
return english_translation, image
|
| 58 |
-
|
| 59 |
except Exception as e:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Initialize models
|
| 63 |
-
try:
|
| 64 |
-
gemini_model, pipeline = initialize_models()
|
| 65 |
-
except Exception as e:
|
| 66 |
-
print(f"Error initializing models: {str(e)}")
|
| 67 |
-
gemini_model, pipeline = None, None
|
| 68 |
|
| 69 |
-
# Create Gradio
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
-
gr.Markdown("### Tamil to English Translator and Image Generator")
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
tamil_input = gr.Textbox(
|
|
@@ -82,19 +85,18 @@ with gr.Blocks() as demo:
|
|
| 82 |
label="English Translation",
|
| 83 |
interactive=False
|
| 84 |
)
|
| 85 |
-
|
| 86 |
with gr.Row():
|
| 87 |
image_output = gr.Image(label="Generated Image")
|
| 88 |
|
|
|
|
| 89 |
btn_process = gr.Button("Generate")
|
| 90 |
-
|
| 91 |
-
# Update process function to include model parameters
|
| 92 |
btn_process.click(
|
| 93 |
-
fn=
|
| 94 |
inputs=[tamil_input],
|
| 95 |
outputs=[english_output, image_output]
|
| 96 |
)
|
| 97 |
|
| 98 |
# Launch the interface
|
| 99 |
if __name__ == "__main__":
|
| 100 |
-
demo.launch(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Initialize the Stable Diffusion model with optimizations
|
| 8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 9 |
+
genai.configure(api_key="AIzaSyCK-tdqs-O7sMUR938ZZy0kF-2vblNJKDo")
|
| 10 |
+
model = genai.GenerativeModel("gemini-1.5-flash-8b")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Initialize optimized pipeline
|
| 13 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
+
revision="fp16",
|
| 17 |
+
safety_checker=None,
|
| 18 |
+
requires_safety_checking=False
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Apply memory optimizations
|
| 22 |
+
pipeline.enable_attention_slicing(slice_size="max")
|
| 23 |
+
pipeline.enable_model_cpu_offload()
|
| 24 |
+
|
| 25 |
+
# Define the Gemini interaction functions
|
| 26 |
+
def translate_tamil_to_english_gemini(tamil_text):
|
| 27 |
try:
|
| 28 |
+
response = model.generate_content(f"Translate this Tamil text to English: {tamil_text}")
|
|
|
|
|
|
|
|
|
|
| 29 |
return response.text
|
| 30 |
except Exception as e:
|
| 31 |
return f"Translation error: {str(e)}"
|
| 32 |
|
| 33 |
+
def generate_image_gemini(prompt):
|
| 34 |
try:
|
| 35 |
+
# Optimized image generation parameters
|
| 36 |
+
image = pipeline(
|
| 37 |
+
prompt,
|
| 38 |
+
num_inference_steps=20, # Reduced steps for faster generation
|
| 39 |
+
guidance_scale=7.0, # Slightly reduced from default
|
| 40 |
+
height=512, # Optimized dimensions
|
| 41 |
+
width=512
|
| 42 |
+
).images[0]
|
| 43 |
+
|
| 44 |
+
# Optimize output image size
|
| 45 |
+
if isinstance(image, Image.Image):
|
| 46 |
+
image = image.resize((384, 384), Image.Resampling.LANCZOS)
|
| 47 |
+
|
| 48 |
return image
|
| 49 |
except Exception as e:
|
| 50 |
+
print(f"Image generation error: {str(e)}")
|
| 51 |
return None
|
| 52 |
|
| 53 |
+
def process_input_gemini(tamil_text):
|
| 54 |
if not tamil_text.strip():
|
| 55 |
return "Please enter some text", None
|
| 56 |
|
| 57 |
try:
|
| 58 |
# Translate Tamil to English
|
| 59 |
+
english_translation = translate_tamil_to_english_gemini(tamil_text)
|
| 60 |
print(english_translation)
|
| 61 |
|
|
|
|
| 62 |
if english_translation.startswith("Translation error"):
|
| 63 |
return english_translation, None
|
| 64 |
|
| 65 |
+
# Generate an image based on the translation
|
| 66 |
+
image = generate_image_gemini(english_translation)
|
| 67 |
return english_translation, image
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"Processing error: {str(e)}")
|
| 70 |
+
return str(e), None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Create Gradio UI
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("### Tamil to English Translator and Image Generator (Optimized)")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
tamil_input = gr.Textbox(
|
|
|
|
| 85 |
label="English Translation",
|
| 86 |
interactive=False
|
| 87 |
)
|
| 88 |
+
|
| 89 |
with gr.Row():
|
| 90 |
image_output = gr.Image(label="Generated Image")
|
| 91 |
|
| 92 |
+
# Button to process
|
| 93 |
btn_process = gr.Button("Generate")
|
|
|
|
|
|
|
| 94 |
btn_process.click(
|
| 95 |
+
fn=process_input_gemini,
|
| 96 |
inputs=[tamil_input],
|
| 97 |
outputs=[english_output, image_output]
|
| 98 |
)
|
| 99 |
|
| 100 |
# Launch the interface
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch(share=True, debug=True)
|