Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Load Moondream model (assuming local inference; for Spaces, ensure GPU access)
|
| 9 |
+
model_id = "vikhyatk/moondream2"
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.float16)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 12 |
+
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
|
| 14 |
+
def analyze_room(image, prompt):
|
| 15 |
+
"""Use Moondream to analyze the uploaded room image and provide design suggestions."""
|
| 16 |
+
if image is None:
|
| 17 |
+
return "Please upload an image of your room."
|
| 18 |
+
|
| 19 |
+
# Process image with Moondream
|
| 20 |
+
enc_image = model.encode_image(image).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
response = model.answer_question(enc_image, prompt, tokenizer)
|
| 22 |
+
return response
|
| 23 |
+
|
| 24 |
+
def generate_color_palette(suggestion_text):
|
| 25 |
+
"""Generate color palette suggestions based on Moondream's output."""
|
| 26 |
+
# Simple mock for color palette; in practice, integrate with a color model or API
|
| 27 |
+
palettes = [
|
| 28 |
+
["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEAA7"],
|
| 29 |
+
["#DDA0DD", "#98D8C8", "#F7DC6F", "#BB8FCE", "#85C1E9"],
|
| 30 |
+
["#F8C471", "#82E0AA", "#F1948A", "#AED6F1", "#D5DBDB"]
|
| 31 |
+
]
|
| 32 |
+
return random.choice(palettes)
|
| 33 |
+
|
| 34 |
+
def furniture_recommendations(style):
|
| 35 |
+
"""Provide furniture recommendations based on style."""
|
| 36 |
+
recommendations = {
|
| 37 |
+
"modern": ["Minimalist sofa", "Glass coffee table", "Geometric rug"],
|
| 38 |
+
"traditional": ["Wooden armchair", "Crystal chandelier", "Oriental vase"],
|
| 39 |
+
"minimalist": ["White shelves", "Black accent chair", "Plant pot"]
|
| 40 |
+
}
|
| 41 |
+
return recommendations.get(style.lower(), ["Generic chair", "Table", "Lamp"])
|
| 42 |
+
|
| 43 |
+
# Onboarding components
|
| 44 |
+
with gr.Blocks(title="AI Interior Design Assistant", theme=gr.themes.Soft()) as demo:
|
| 45 |
+
gr.HTML("""
|
| 46 |
+
<div style="text-align: center; padding: 20px;">
|
| 47 |
+
<h1>🏠 AI Interior Design Assistant</h1>
|
| 48 |
+
<p>Transform your space with AI-powered design suggestions using Moondream vision model.</p>
|
| 49 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007acc; text-decoration: none;">Built with anycoder</a>
|
| 50 |
+
</div>
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
with gr.Tabs() as tabs:
|
| 54 |
+
with gr.TabItem("Welcome", id="welcome"):
|
| 55 |
+
gr.Markdown("""
|
| 56 |
+
## Welcome to Your AI Design Journey! 🎨
|
| 57 |
+
- **Step 1:** Upload a photo of your room.
|
| 58 |
+
- **Step 2:** Describe your vision or ask questions.
|
| 59 |
+
- **Step 3:** Get personalized design suggestions, color palettes, and furniture ideas.
|
| 60 |
+
- **Step 4:** Iterate and refine with follow-up prompts.
|
| 61 |
+
""")
|
| 62 |
+
gr.Image(value="https://via.placeholder.com/600x400?text=Upload+Your+Room+Photo", label="Example Room")
|
| 63 |
+
|
| 64 |
+
with gr.TabItem("Design Studio", id="studio"):
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column(scale=1):
|
| 67 |
+
image_input = gr.Image(label="Upload Room Photo", type="pil")
|
| 68 |
+
prompt_input = gr.Textbox(label="Describe your vision (e.g., 'Make it cozy and modern')", lines=3, placeholder="Ask for design ideas, color schemes, or specific changes...")
|
| 69 |
+
analyze_btn = gr.Button("Analyze & Suggest", variant="primary")
|
| 70 |
+
|
| 71 |
+
with gr.Column(scale=2):
|
| 72 |
+
output_text = gr.Textbox(label="AI Design Suggestions", lines=10, interactive=False)
|
| 73 |
+
with gr.Row():
|
| 74 |
+
palette_output = gr.ColorPicker(label="Suggested Colors", interactive=False)
|
| 75 |
+
furniture_output = gr.Textbox(label="Furniture Recommendations", lines=5, interactive=False)
|
| 76 |
+
|
| 77 |
+
def full_analysis(image, prompt):
|
| 78 |
+
if not image:
|
| 79 |
+
return "Upload an image first!", "", ""
|
| 80 |
+
analysis = analyze_room(image, prompt)
|
| 81 |
+
palette = generate_color_palette(analysis)
|
| 82 |
+
style = "modern" if "modern" in prompt.lower() else "traditional" # Simple heuristic
|
| 83 |
+
furniture = furniture_recommendations(style)
|
| 84 |
+
return analysis, ", ".join(palette), "\n".join(furniture)
|
| 85 |
+
|
| 86 |
+
analyze_btn.click(full_analysis, [image_input, prompt_input], [output_text, palette_output, furniture_output])
|
| 87 |
+
|
| 88 |
+
with gr.TabItem("Gallery", id="gallery"):
|
| 89 |
+
gr.Markdown("## Inspiration Gallery")
|
| 90 |
+
# Mock gallery; in practice, load from a dataset
|
| 91 |
+
gr.Gallery([
|
| 92 |
+
"https://via.placeholder.com/300x200?text=Modern+Living+Room",
|
| 93 |
+
"https://via.placeholder.com/300x200?text=Cozy+Bedroom",
|
| 94 |
+
"https://via.placeholder.com/300x200?text=Kitchen+Renovation"
|
| 95 |
+
], columns=3, height=400)
|
| 96 |
+
|
| 97 |
+
gr.Markdown("Upload your designs to share!")
|
| 98 |
+
gallery_upload = gr.File(file_count="multiple", file_types=["image"])
|
| 99 |
+
|
| 100 |
+
# Footer
|
| 101 |
+
gr.HTML("""
|
| 102 |
+
<div style="text-align: center; padding: 20px; color: gray;">
|
| 103 |
+
<p>Powered by Moondream Vision Model | Built with Gradio</p>
|
| 104 |
+
</div>
|
| 105 |
+
""")
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch()
|