virtual-tryon / app.py
AnshSrivastava003's picture
Upload 2 files
811a06a verified
import base64
import os
import gradio as gr
from google import genai
from google.genai import types
from google.genai.types import HarmBlockThreshold
from PIL import Image
from io import BytesIO
import tempfile
import warnings
import io
# Load environment variables from .env file
def swap_clothing(person_image, clothing_image):
"""
Generate an image where the person from the first image is wearing clothing from the second image.
Args:
person_image: The image containing the person
clothing_image: The image containing the clothing to swap
Returns:
The generated image with the clothing swapped and any relevant messages
"""
# Capture warnings in a string buffer
warning_buffer = io.StringIO()
warnings.filterwarnings('always') # Ensure all warnings are shown
# Initialize variables outside the try block
temp_files = []
uploaded_files = []
client = None
output_image = None
output_text = ""
with warnings.catch_warnings(record=True) as warning_list:
try:
# Check if both images are provided
if person_image is None or clothing_image is None:
return None, "Please upload both images."
# Get API key from environment variables
api_key = "AIzaSyCIrNCyWF0AO0EYVd7bJ37lBjPozQOrUjE"
if not api_key:
return None, "GEMINI_API_KEY not found in environment variables."
# Create a fresh client instance for each request
client = genai.Client(api_key=api_key)
# Save both uploaded images to temporary files
for img, prefix in [(person_image, "person"), (clothing_image, "clothing")]:
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
img.save(temp_file.name)
temp_files.append(temp_file.name)
# Upload both files to Gemini with fresh file uploads
uploaded_files = [
client.files.upload(file=temp_files[0]), # person image
client.files.upload(file=temp_files[1]), # clothing image
]
# Create the prompt
prompt = """
Perform a precise clothing swap between two images while maintaining strict fidelity to original elements:
1. PERSON IMAGE (SOURCE TO KEEP):
- Retain EXACT pose, body proportions, and background
- Preserve 100% of facial features, hair, and skin details
- Maintain original lighting conditions and environment shadows
2. CLOTHING IMAGE (TARGET TO TRANSFER):
- Extract clothing item with complete pattern/texture details
- Match garment cut/style exactly while maintaining fabric texture
- Preserve original color palette and material appearance
3. OUTPUT REQUIREMENTS:
- Seamlessly overlay clothing from [Clothing Image] onto [Person Image]
- Perfectly align garment with body posture/muscle contours
- Maintain original image resolution and sharpness
- No alterations to facial features/body shape/background elements
- Ensure natural fold/wrinkle physics matching original pose
- Keep accessories/body jewelry unchanged unless covered by new clothing
4. SPECIAL INSTRUCTIONS:
- Priority 1: Clothing accuracy from target image
- Priority 2: Person/image integrity from source photo
- Priority 3: Seamless visual integration
- Reject any style transfer that requires pose/background modification
"""
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text="This is the person image. Do not change the face or features of the person. Pay attention and retain the face, environment, background, pose, facial features."),
types.Part.from_uri(
file_uri=uploaded_files[0].uri,
mime_type=uploaded_files[0].mime_type,
),
types.Part.from_text(text="This is the clothing image. Swap the clothing onto the person image."),
types.Part.from_uri(
file_uri=uploaded_files[1].uri,
mime_type=uploaded_files[1].mime_type,
),
types.Part.from_text(text=prompt),
types.Part.from_uri(
file_uri=uploaded_files[0].uri,
mime_type=uploaded_files[0].mime_type,
),
],
),
]
generate_content_config = types.GenerateContentConfig(
temperature=0.099,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_modalities=[
"image",
"text",
],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HARASSMENT",
threshold=HarmBlockThreshold.BLOCK_NONE,
),
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold=HarmBlockThreshold.BLOCK_NONE,
),
types.SafetySetting(
category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold=HarmBlockThreshold.BLOCK_NONE,
),
types.SafetySetting(
category="HARM_CATEGORY_DANGEROUS_CONTENT",
threshold=HarmBlockThreshold.BLOCK_NONE,
),
],
response_mime_type="text/plain",
)
response = client.models.generate_content(
model="models/gemini-2.0-flash-exp",
contents=contents,
config=generate_content_config,
)
# Add any warnings to the output text
if warning_list:
output_text += "\nWarnings:\n"
for warning in warning_list:
output_text += f"- {warning.message}\n"
# Process the response
if response and hasattr(response, 'candidates') and response.candidates:
candidate = response.candidates[0]
if hasattr(candidate, 'content') and candidate.content:
for part in candidate.content.parts:
if part.text is not None:
output_text += part.text + "\n"
elif part.inline_data is not None:
try:
if isinstance(part.inline_data.data, bytes):
image_data = part.inline_data.data
else:
image_data = base64.b64decode(part.inline_data.data)
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
temp_file.write(image_data)
temp_file_path = temp_file.name
output_image = Image.open(temp_file_path)
os.unlink(temp_file_path)
except Exception as img_error:
output_text += f"Error processing image: {str(img_error)}\n"
else:
output_text = "The model did not generate a valid response. Please try again with different images."
except Exception as e:
error_details = f"Error: {str(e)}\n\nType: {type(e).__name__}"
if warning_list:
error_details += "\n\nWarnings:\n"
for warning in warning_list:
error_details += f"- {warning.message}\n"
print(f"Exception occurred: {error_details}")
return None
finally:
# Clean up all temporary files
for temp_file in temp_files:
if os.path.exists(temp_file):
os.unlink(temp_file)
# Clean up any uploaded files if possible
for uploaded_file in uploaded_files:
try:
if hasattr(client.files, 'delete') and uploaded_file:
client.files.delete(uploaded_file.uri)
except:
pass # Best effort cleanup
# Clear the client
client = None
return output_image
# Create the Gradio interface
def create_interface():
custom_css = """
@font-face {
font-family: "XKCD";
src: url("https://raw.githubusercontent.com/ipython/xkcd-font/refs/heads/master/xkcd-script/font/xkcd-script.ttf") format("opentype");
}
.gradio-container {
font-family: "XKCD", "Courier New", monospace;
}
"""
with gr.Blocks(css = custom_css,title="Virtual Clothing Try-On", theme ='gstaff/xkcd') as app:
gr.Markdown("# Virtual Clothing Try-On 👕 🩳 🧢")
gr.Markdown("Upload a photo of yourself and a photo of clothing you'd like to try on!")
with gr.Row():
with gr.Column():
person_image = gr.Image(label="Your Photo", type="pil", image_mode="RGB", height=300)
person_examples = [
"https://i.postimg.cc/MGxQDGQP/440050401-46f62432-634e-49e6-b841-25d43945e4ed.png",
"https://i.postimg.cc/fyHC49xR/440050411-c3bc6a7c-496f-493f-aad7-2e434e32fb86.png",
"https://i.postimg.cc/cJNQbmVc/440050418-46dbca08-003b-4a14-8fcf-5a61b4c91e12.png",
"https://i.postimg.cc/SskfJrdK/440050443-2abac8ca-f5b9-4ba9-9437-36e5ccf027d5.png",
"https://i.postimg.cc/LXDBf1Q3/440050446-fc8c2be6-f33a-4ebb-a6a2-0a1d514a219c.png",
"https://i.postimg.cc/kXBF6DQW/440050457-318b1e52-46af-4b29-9b21-6281a6687302.png",
"https://i.postimg.cc/3xGX16bR/440050469-a5113e2c-a644-4484-a4de-2b6914796deb.png",
"https://i.postimg.cc/m25j87Ld/440050479-2bfaa190-8211-4e2f-a9f0-8b1263d22e3a.jpg"
]
gr.Examples(examples=person_examples, inputs=person_image, label="Person Image Examples")
with gr.Column():
clothing_image = gr.Image(label="Clothing Photo", type="pil", image_mode="RGB", height=300)
clothing_examples = [
"https://i.postimg.cc/2SM7QWd4/0012.jpg",
"https://i.postimg.cc/TPbqCPQB/07-upper.png",
"https://i.postimg.cc/Znb8bxZh/440052371-e34d38d2-88cd-4ee9-a07d-57f544fcbab1.png",
"https://i.postimg.cc/c41h0048/440052384-aa986119-61ee-406e-a246-29ac83f323e6.png",
"https://i.postimg.cc/qq18z6M6/440052392-d63adf74-abe2-4f57-a3b7-5ce464824e54.jpg",
"https://i.postimg.cc/1tZ0TZjX/garment-1.png",
"https://i.postimg.cc/hjb8R3kr/garment-2.jpg",
"https://i.postimg.cc/SxJGYd9S/garment-3.jpg"
]
gr.Examples(examples=clothing_examples, inputs=clothing_image, label="Garment Image Examples")
with gr.Column():
output_image = gr.Image(label="Result", type="pil")
submit_btn = gr.Button("Generate")
submit_btn.click(
fn=swap_clothing,
inputs=[person_image, clothing_image],
outputs=output_image
)
return app
if __name__ == "__main__":
app = create_interface()
app.launch()