Spaces:
Sleeping
Sleeping
prgarg007
commited on
Commit
·
7c676e8
1
Parent(s):
aaf9cdd
fix3
Browse files
app.py
CHANGED
|
@@ -1,178 +1,253 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
"""
|
| 15 |
-
Call the private space as a function and return the results
|
| 16 |
-
"""
|
| 17 |
-
if style_transfer_function is None:
|
| 18 |
-
return None, None, "Error: Could not connect to the AI processing service. Please try again later."
|
| 19 |
-
|
| 20 |
-
if not swatch_image or not product_image or not room_image:
|
| 21 |
-
return None, None, "Please upload all three images (swatch, product, and room)."
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def create_public_interface():
|
| 39 |
-
"""Create
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
) as app:
|
| 45 |
-
|
| 46 |
-
gr.Markdown("""
|
| 47 |
-
# 🚀 AI Powered Style Transfer & Room Integration
|
| 48 |
-
## Public Access Portal
|
| 49 |
-
|
| 50 |
-
Transform your furniture with AI-powered style transfer and room integration!
|
| 51 |
-
|
| 52 |
-
**How it works:**
|
| 53 |
-
1. Upload a **swatch image** (fabric/pattern you want to apply)
|
| 54 |
-
2. Upload a **product image** (furniture piece to style)
|
| 55 |
-
3. Upload a **room image** (where you want to place the furniture)
|
| 56 |
-
4. Select your preferred **room style**
|
| 57 |
-
5. Let AI work its magic! ✨
|
| 58 |
-
""")
|
| 59 |
-
|
| 60 |
-
# Check if the private space is loaded
|
| 61 |
-
if style_transfer_function is None:
|
| 62 |
gr.Markdown("""
|
| 63 |
-
⚠️
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
- Authentication problems
|
| 68 |
- Service maintenance
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
""")
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
product_input = gr.Image(
|
| 85 |
-
label="📦 Furniture/Product Image",
|
| 86 |
-
type="pil",
|
| 87 |
-
height=200
|
| 88 |
-
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
label="🏠 Room/Background Image",
|
| 92 |
-
type="pil",
|
| 93 |
-
height=200,
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
"🤖 Transform with AI",
|
| 104 |
-
variant="primary",
|
| 105 |
-
size="lg",
|
| 106 |
-
scale=1
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
gr.Markdown("""
|
| 110 |
-
### 💡 Tips for Best Results:
|
| 111 |
-
- Use high-quality, clear images
|
| 112 |
-
- Ensure good lighting in your photos
|
| 113 |
-
- Choose images with similar perspectives
|
| 114 |
-
- Swatch images work best when the pattern/texture is clearly visible
|
| 115 |
""")
|
| 116 |
-
|
| 117 |
-
with gr.Column(scale=1):
|
| 118 |
-
gr.Markdown("### 🎯 AI Generated Results")
|
| 119 |
-
|
| 120 |
-
styled_product_output = gr.Image(
|
| 121 |
-
label="🎨 Step 1: Styled Product",
|
| 122 |
-
height=300
|
| 123 |
-
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
gr.Markdown("""
|
| 137 |
-
###
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
""")
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
show_progress=True
|
| 152 |
-
)
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
gr.
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
import importlib.util
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from huggingface_hub import hf_hub_download, snapshot_download, HfFileSystem
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
+
# Your private space details
|
| 9 |
+
PRIVATE_SPACE_REPO = "prgarg007/ai-style-transfer-dreamsofa" # Replace with your actual private space name
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
+
def setup_cache_directory():
|
| 13 |
+
"""Setup and return cache directory for private space files"""
|
| 14 |
+
cache_dir = Path("private_space_cache")
|
| 15 |
+
cache_dir.mkdir(exist_ok=True)
|
| 16 |
+
return cache_dir
|
| 17 |
|
| 18 |
+
def download_private_assets(cache_dir):
|
| 19 |
+
"""Download necessary files from private space"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
print("Downloading private space assets...")
|
| 22 |
+
|
| 23 |
+
# Download entire repository snapshot for directory structure
|
| 24 |
+
snapshot_download(
|
| 25 |
+
repo_id=PRIVATE_SPACE_REPO,
|
| 26 |
+
repo_type="space",
|
| 27 |
+
local_dir=cache_dir,
|
| 28 |
+
token=HF_TOKEN
|
| 29 |
)
|
| 30 |
|
| 31 |
+
print("Successfully downloaded private space assets!")
|
| 32 |
+
return True
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
+
print(f"Error downloading private assets: {str(e)}")
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
def load_private_app():
|
| 39 |
+
"""Download and import the private space app"""
|
| 40 |
+
try:
|
| 41 |
+
print("Setting up cache directory...")
|
| 42 |
+
cache_dir = setup_cache_directory()
|
| 43 |
+
|
| 44 |
+
print("Downloading private space files...")
|
| 45 |
+
if not download_private_assets(cache_dir):
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
print("Loading private app code...")
|
| 49 |
+
|
| 50 |
+
# Download the main app.py file from private space
|
| 51 |
+
app_path = hf_hub_download(
|
| 52 |
+
repo_id=PRIVATE_SPACE_REPO,
|
| 53 |
+
filename="app.py",
|
| 54 |
+
repo_type="space",
|
| 55 |
+
token=HF_TOKEN
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
print(f"App file downloaded to: {app_path}")
|
| 59 |
+
|
| 60 |
+
# Add the cache directory to Python path so imports work
|
| 61 |
+
sys.path.insert(0, str(cache_dir))
|
| 62 |
+
|
| 63 |
+
# Import and execute the private app
|
| 64 |
+
spec_app = importlib.util.spec_from_file_location("private_app", app_path)
|
| 65 |
+
private_app_module = importlib.util.module_from_spec(spec_app)
|
| 66 |
+
|
| 67 |
+
# Set environment variables that might be needed
|
| 68 |
+
# Copy any environment variables from current space to the imported module
|
| 69 |
+
env_vars_to_copy = ["OPENAI_API_KEY", "HF_TOKEN"]
|
| 70 |
+
for var in env_vars_to_copy:
|
| 71 |
+
if os.getenv(var):
|
| 72 |
+
os.environ[var] = os.getenv(var)
|
| 73 |
+
|
| 74 |
+
# Execute the module
|
| 75 |
+
spec_app.loader.exec_module(private_app_module)
|
| 76 |
+
|
| 77 |
+
print("Successfully loaded private app module!")
|
| 78 |
+
return private_app_module
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Error loading private app: {str(e)}")
|
| 82 |
+
return None
|
| 83 |
|
| 84 |
def create_public_interface():
|
| 85 |
+
"""Create public interface that uses private app functionality"""
|
| 86 |
+
|
| 87 |
+
# Load the private app
|
| 88 |
+
private_app = load_private_app()
|
| 89 |
|
| 90 |
+
if private_app is None:
|
| 91 |
+
# Fallback interface if loading fails
|
| 92 |
+
with gr.Blocks(title="AI Style Transfer - Loading Error") as error_app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
gr.Markdown("""
|
| 94 |
+
# ⚠️ Service Temporarily Unavailable
|
| 95 |
|
| 96 |
+
We're experiencing technical difficulties loading the AI processing service.
|
| 97 |
+
|
| 98 |
+
**Possible causes:**
|
| 99 |
+
- Network connectivity issues
|
| 100 |
- Authentication problems
|
| 101 |
- Service maintenance
|
| 102 |
|
| 103 |
+
**Solutions:**
|
| 104 |
+
1. Check that HF_TOKEN environment variable is set correctly
|
| 105 |
+
2. Verify the private space name is correct
|
| 106 |
+
3. Ensure you have access to the private space
|
| 107 |
+
4. Try refreshing the page in a few minutes
|
| 108 |
+
|
| 109 |
+
If the problem persists, please contact support.
|
| 110 |
""")
|
| 111 |
+
return error_app
|
| 112 |
+
|
| 113 |
+
# If we successfully loaded the private app, create the interface using its functions
|
| 114 |
+
try:
|
| 115 |
+
# The private app should have a create_interface function
|
| 116 |
+
if hasattr(private_app, 'create_interface'):
|
| 117 |
+
print("Using create_interface from private app...")
|
| 118 |
+
return private_app.create_interface()
|
| 119 |
|
| 120 |
+
# Alternative: if it has the main components, build interface manually
|
| 121 |
+
elif hasattr(private_app, 'process_images'):
|
| 122 |
+
print("Building interface using private app functions...")
|
| 123 |
+
|
| 124 |
+
with gr.Blocks(title="AI Style Transfer & Room Integration", theme=gr.themes.Soft()) as app:
|
| 125 |
+
gr.Markdown("""
|
| 126 |
+
# 🚀 AI Powered Style Transfer & Room Integration
|
| 127 |
+
## Secure Processing Portal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
Transform your furniture with AI-powered style transfer and room integration!
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
**How it works:**
|
| 132 |
+
1. Upload a **swatch image** (fabric/pattern you want to apply)
|
| 133 |
+
2. Upload a **product image** (furniture piece to style)
|
| 134 |
+
3. Upload a **room image** (where you want to place the furniture)
|
| 135 |
+
4. Select your preferred **room style**
|
| 136 |
+
5. Let AI work its magic! ✨
|
| 137 |
|
| 138 |
+
*Your code and processing logic are securely protected*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
with gr.Row():
|
| 142 |
+
with gr.Column():
|
| 143 |
+
gr.Markdown("### 📤 Upload Your Images")
|
| 144 |
+
|
| 145 |
+
swatch_input = gr.Image(
|
| 146 |
+
label="🎨 Swatch/Pattern Image",
|
| 147 |
+
type="pil",
|
| 148 |
+
height=200
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
product_input = gr.Image(
|
| 152 |
+
label="📦 Furniture/Product Image",
|
| 153 |
+
type="pil",
|
| 154 |
+
height=200
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
room_input = gr.Image(
|
| 158 |
+
label="🏠 Room/Background Image",
|
| 159 |
+
type="pil",
|
| 160 |
+
height=200
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
room_style = gr.Dropdown(
|
| 164 |
+
choices=["modern", "vintage", "industrial", "scandinavian", "bohemian", "rustic"],
|
| 165 |
+
value="modern",
|
| 166 |
+
label="🎭 Room Style Theme"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
process_btn = gr.Button("🤖 Transform with AI", variant="primary", size="lg")
|
| 170 |
+
|
| 171 |
+
with gr.Column():
|
| 172 |
+
gr.Markdown("### 🎯 AI Generated Results")
|
| 173 |
+
|
| 174 |
+
styled_product_output = gr.Image(
|
| 175 |
+
label="🎨 Step 1: Styled Product",
|
| 176 |
+
height=300
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
final_output = gr.Image(
|
| 180 |
+
label="🏆 Step 2: Room Integration",
|
| 181 |
+
height=300
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
status_output = gr.Textbox(
|
| 185 |
+
label="🔄 Processing Status",
|
| 186 |
+
value="Ready to transform your furniture with AI...",
|
| 187 |
+
interactive=False
|
| 188 |
+
)
|
| 189 |
|
| 190 |
+
# Connect to the private app's processing function
|
| 191 |
+
process_btn.click(
|
| 192 |
+
fn=private_app.process_images, # Use the function from private space
|
| 193 |
+
inputs=[swatch_input, product_input, room_input, room_style],
|
| 194 |
+
outputs=[styled_product_output, final_output, status_output],
|
| 195 |
+
show_progress=True
|
| 196 |
)
|
| 197 |
|
| 198 |
gr.Markdown("""
|
| 199 |
+
### 🎨 Example Use Cases:
|
| 200 |
+
- **Interior Design**: Preview how different fabrics look on your furniture
|
| 201 |
+
- **E-commerce**: Show products in various styles and room settings
|
| 202 |
+
- **Home Renovation**: Experiment with different design aesthetics
|
| 203 |
+
- **Furniture Customization**: Visualize custom upholstery options
|
| 204 |
+
|
| 205 |
+
### ⚡ Powered by:
|
| 206 |
+
- OpenAI DALL-E for image generation
|
| 207 |
+
- Advanced AI for style analysis and transfer
|
| 208 |
+
- Secure code execution from private repository
|
| 209 |
|
| 210 |
+
### 🔒 Privacy & Security:
|
| 211 |
+
- Your processing code is kept private and secure
|
| 212 |
+
- Images are processed securely without permanent storage
|
| 213 |
+
- Code execution happens in isolated environment
|
| 214 |
""")
|
| 215 |
+
|
| 216 |
+
return app
|
| 217 |
|
| 218 |
+
else:
|
| 219 |
+
raise Exception("Private app doesn't have expected functions")
|
| 220 |
+
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"Error creating interface with private app: {str(e)}")
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
# Final fallback
|
| 225 |
+
with gr.Blocks(title="AI Style Transfer - Configuration Error") as fallback_app:
|
| 226 |
+
gr.Markdown(f"""
|
| 227 |
+
# ⚠️ Configuration Error
|
| 228 |
+
|
| 229 |
+
Successfully connected to private space but encountered a configuration error:
|
| 230 |
+
|
| 231 |
+
`{str(e)}`
|
| 232 |
+
|
| 233 |
+
Please check:
|
| 234 |
+
1. Private space code structure
|
| 235 |
+
2. Required functions are properly defined
|
| 236 |
+
3. Environment variables are set correctly
|
| 237 |
+
|
| 238 |
+
Contact support if this issue persists.
|
| 239 |
+
""")
|
| 240 |
+
return fallback_app
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
"""Main function to create and launch the public interface"""
|
| 244 |
+
print("Starting public interface...")
|
| 245 |
+
print(f"Private space: {PRIVATE_SPACE_REPO}")
|
| 246 |
+
print(f"HF Token available: {'Yes' if HF_TOKEN else 'No'}")
|
| 247 |
+
|
| 248 |
+
# Create the public interface
|
| 249 |
+
app = create_public_interface()
|
| 250 |
|
| 251 |
+
# Launch the app
|
| 252 |
+
print("Launching public interface...")
|
| 253 |
+
app.launch()
|