Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import random
|
| 6 |
+
import requests
|
| 7 |
+
import torch
|
| 8 |
+
from torchvision import transforms
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
# Simulated style transfer function - in real app, replace with actual ML model
|
| 13 |
+
def apply_style_transfer(content_img, style_img, style_strength, randomize_strength):
|
| 14 |
+
if content_img is None or style_img is None:
|
| 15 |
+
return None, None, "Empty image"
|
| 16 |
+
|
| 17 |
+
if randomize_strength:
|
| 18 |
+
style_strength = random.uniform(0.1, 1.0)
|
| 19 |
+
|
| 20 |
+
# Simulate processing time
|
| 21 |
+
time.sleep(3)
|
| 22 |
+
|
| 23 |
+
# In a real app, this would be your actual style transfer logic
|
| 24 |
+
# For demo, we'll just blend the images
|
| 25 |
+
content_img = cv2.resize(content_img, (512, 512))
|
| 26 |
+
style_img = cv2.resize(style_img, (512, 512))
|
| 27 |
+
result = cv2.addWeighted(content_img, 1-style_strength, style_img, style_strength, 0)
|
| 28 |
+
|
| 29 |
+
return result, style_strength, "Success"
|
| 30 |
+
|
| 31 |
+
# Constants
|
| 32 |
+
MAX_STYLE_STRENGTH = 1.0
|
| 33 |
+
MIN_STYLE_STRENGTH = 0.1
|
| 34 |
+
|
| 35 |
+
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 36 |
+
|
| 37 |
+
# Assume we have these directories with example images
|
| 38 |
+
content_list = os.listdir(os.path.join(example_path, "content"))
|
| 39 |
+
content_list_path = [os.path.join(example_path, "content", img) for img in content_list]
|
| 40 |
+
|
| 41 |
+
style_list = os.listdir(os.path.join(example_path, "styles"))
|
| 42 |
+
style_list_path = [os.path.join(example_path, "styles", style) for style in style_list]
|
| 43 |
+
|
| 44 |
+
css = """
|
| 45 |
+
#col-content {
|
| 46 |
+
margin: 0 auto;
|
| 47 |
+
max-width: 430px;
|
| 48 |
+
}
|
| 49 |
+
#col-style {
|
| 50 |
+
margin: 0 auto;
|
| 51 |
+
max-width: 430px;
|
| 52 |
+
}
|
| 53 |
+
#col-result {
|
| 54 |
+
margin: 0 auto;
|
| 55 |
+
max-width: 430px;
|
| 56 |
+
}
|
| 57 |
+
#gallery {
|
| 58 |
+
margin: 0 auto;
|
| 59 |
+
max-width: 1100px;
|
| 60 |
+
}
|
| 61 |
+
#transfer-button {
|
| 62 |
+
color: purple;
|
| 63 |
+
font-size: 18px;
|
| 64 |
+
}
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
with gr.Blocks(css=css) as ArtStyleTransfer:
|
| 68 |
+
gr.Markdown("# 🎨 AI Art Style Transfer")
|
| 69 |
+
gr.Markdown("Transform your photos into artistic masterpieces!")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(elem_id="col-content"):
|
| 73 |
+
gr.Markdown("### 1. Choose Your Content Image")
|
| 74 |
+
content_img = gr.Image(label="Photo to transform", sources='upload', type="numpy")
|
| 75 |
+
content_examples = gr.Examples(
|
| 76 |
+
inputs=content_img,
|
| 77 |
+
examples=content_list_path,
|
| 78 |
+
examples_per_page=8
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
with gr.Column(elem_id="col-style"):
|
| 82 |
+
gr.Markdown("### 2. Pick an Artistic Style")
|
| 83 |
+
style_img = gr.Image(label="Style reference", sources='upload', type="numpy")
|
| 84 |
+
style_examples = gr.Examples(
|
| 85 |
+
inputs=style_img,
|
| 86 |
+
examples=style_list_path,
|
| 87 |
+
examples_per_page=8
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
with gr.Column(elem_id="col-result"):
|
| 91 |
+
gr.Markdown("### 3. Generate Artwork")
|
| 92 |
+
result_img = gr.Image(label="Stylized Result")
|
| 93 |
+
with gr.Row():
|
| 94 |
+
style_strength = gr.Slider(
|
| 95 |
+
label="Style Strength",
|
| 96 |
+
minimum=MIN_STYLE_STRENGTH,
|
| 97 |
+
maximum=MAX_STYLE_STRENGTH,
|
| 98 |
+
step=0.1,
|
| 99 |
+
value=0.5,
|
| 100 |
+
)
|
| 101 |
+
randomize_strength = gr.Checkbox(label="Randomize Strength")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
final_strength = gr.Number(label="Applied Style Strength")
|
| 105 |
+
status = gr.Text(label="Status")
|
| 106 |
+
|
| 107 |
+
transfer_button = gr.Button("🎨 Transform!", elem_id="transfer-button")
|
| 108 |
+
|
| 109 |
+
transfer_button.click(
|
| 110 |
+
fn=apply_style_transfer,
|
| 111 |
+
inputs=[content_img, style_img, style_strength, randomize_strength],
|
| 112 |
+
outputs=[result_img, final_strength, status],
|
| 113 |
+
api_name=False,
|
| 114 |
+
concurrency_limit=20
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Column(elem_id="gallery"):
|
| 118 |
+
gr.Markdown("## ✨ Style Transfer Gallery")
|
| 119 |
+
show_case = gr.Examples(
|
| 120 |
+
examples=[
|
| 121 |
+
["assets/examples/photo1.jpg", "assets/examples/style1.jpg", "assets/examples/result1.jpg"],
|
| 122 |
+
["assets/examples/photo2.jpg", "assets/examples/style2.jpg", "assets/examples/result2.jpg"],
|
| 123 |
+
],
|
| 124 |
+
inputs=[content_img, style_img, result_img],
|
| 125 |
+
label=None
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
ArtStyleTransfer.queue(api_open=False).launch(show_api=False)
|