Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from diffusers import DiffusionPipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load environment variables from .env file
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Access the API key from the environment
|
| 13 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 14 |
+
|
| 15 |
+
# Error handling (optional)
|
| 16 |
+
if not GOOGLE_API_KEY:
|
| 17 |
+
raise ValueError("Missing GOOGLE_API_KEY environment variable. Please set it in your .env file.")
|
| 18 |
+
|
| 19 |
+
# Configure the genai library
|
| 20 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 21 |
+
|
| 22 |
+
# Initialize Gemini models
|
| 23 |
+
model1 = genai.GenerativeModel('gemini-1.0-pro-latest')
|
| 24 |
+
model2 = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 25 |
+
|
| 26 |
+
# Define the function to transform images
|
| 27 |
+
|
| 28 |
+
model_path = "GiantAnalytics/sdxl_fine_tuned_model_aditya_2"
|
| 29 |
+
pipe = DiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
| 30 |
+
|
| 31 |
+
# Set the device based on CUDA availability
|
| 32 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
+
pipe.to(device)
|
| 34 |
+
|
| 35 |
+
def enhance_prompt_and_generate_images(image, prompt):
|
| 36 |
+
if isinstance(image, np.ndarray):
|
| 37 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 38 |
+
try:
|
| 39 |
+
prompt11='''provide me all the information about texture of the design how it is looking and design of the input textile image in descriptive format
|
| 40 |
+
It should provide like this Texture Details: , Design Details: and overall description of image'''
|
| 41 |
+
# Step 1: Get an enhanced prompt using the Gemini API
|
| 42 |
+
response1 = model2.generate_content([prompt11, image], stream=False)
|
| 43 |
+
response1.resolve()
|
| 44 |
+
initial_description = response1.text
|
| 45 |
+
|
| 46 |
+
if initial_description:
|
| 47 |
+
enhanced_prompt = f'''First, identify the user's specifications provided in the prompt: {user_input}.
|
| 48 |
+
Understand the image details: {initial_description}. Now, generate a detailed prompt that combines the user inputs with the image details in a suitable way.
|
| 49 |
+
This new prompt will help generate a new image with the SDXL model. The prompt should be concise and less than 100 tokens; curate it carefully.
|
| 50 |
+
Focus on maintaining the theme and the overall feel of the design, incorporating subtle changes that enhance its uniqueness and visual appeal.'''
|
| 51 |
+
response2 = model1.generate_content([enhanced_prompt], stream=False)
|
| 52 |
+
response2.resolve()
|
| 53 |
+
final_prompt = response2.text if response2.text else prompt
|
| 54 |
+
else:
|
| 55 |
+
final_prompt = prompt
|
| 56 |
+
print(final_prompt) # Use original prompt if no description is available
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Failed to enhance prompt via Gemini API: {e}")
|
| 60 |
+
final_prompt = prompt # Use original prompt on any error
|
| 61 |
+
|
| 62 |
+
# Step 2: Generate three image variations
|
| 63 |
+
image_variations = []
|
| 64 |
+
settings = [(7.5, 0.5), (8.0, 0.6), (6.0, 0.4)] # Custom settings for guidance_scale and strength
|
| 65 |
+
for i, (guidance, strength) in enumerate(settings): # Different settings for variations
|
| 66 |
+
generator = torch.Generator(device=device).manual_seed(i * 100)
|
| 67 |
+
output = pipe(prompt=final_prompt, image=image, guidance_scale=guidance, strength=strength, generator=generator).images[0]
|
| 68 |
+
image_variations.append(output)
|
| 69 |
+
|
| 70 |
+
return image_variations
|
| 71 |
+
|
| 72 |
+
# Path to your local logo image
|
| 73 |
+
logo_path = '/content/RCD-Final Logosmall size.jpg' # Replace with your image path
|
| 74 |
+
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column(scale=10):
|
| 78 |
+
gr.Markdown(
|
| 79 |
+
"""
|
| 80 |
+
<div id="logo-container">
|
| 81 |
+
<h1>Text Guided Image-to-Image Generation</h1>
|
| 82 |
+
<p>Enter a text prompt with required parameters to transform the Input Image using the Fine-Tuned SDXL Model.</p>
|
| 83 |
+
</div>
|
| 84 |
+
""",
|
| 85 |
+
elem_id="logo-container"
|
| 86 |
+
)
|
| 87 |
+
with gr.Column(scale=1, elem_id="logo-column"):
|
| 88 |
+
logo = gr.Image(value=logo_path, elem_id="logo", height=128, width=128)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
img_input = gr.Image(label="Upload Image")
|
| 92 |
+
prompt_input = gr.Textbox(label="Enter your prompt")
|
| 93 |
+
submit_btn = gr.Button("Generate")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
output_image1 = gr.Image(label="Variation 1")
|
| 97 |
+
output_image2 = gr.Image(label="Variation 2")
|
| 98 |
+
output_image3 = gr.Image(label="Variation 3")
|
| 99 |
+
|
| 100 |
+
submit_btn.click(
|
| 101 |
+
enhance_prompt_and_generate_images,
|
| 102 |
+
inputs=[img_input, prompt_input],
|
| 103 |
+
outputs=[output_image1, output_image2, output_image3]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch(debug=True)#inline=False)
|