File size: 2,651 Bytes
878e799
 
 
 
88706c5
 
 
 
878e799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88706c5
 
 
 
 
 
 
 
 
 
 
 
 
878e799
88706c5
878e799
 
 
 
 
 
 
06a12b7
878e799
 
 
 
 
 
 
88706c5
 
 
878e799
 
06a12b7
 
878e799
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import requests
import io
import os
import json
from PIL import Image

# Load Hugging Face API Key from Space Secrets
HF_API_KEY = os.getenv("HF_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/sameedhayat/corgy_dog_LoRA"
headers = {"Authorization": f"Bearer {HF_API_KEY}"}

def query(payload):
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
        response.raise_for_status()
        
        # Check if the response is an image
        content_type = response.headers.get("Content-Type", "")
        if "image" in content_type:
            return response.content
        else:
            return f"API Error: Unexpected response format - {response.text}"
    except requests.exceptions.RequestException as e:
        return f"API Error: {str(e)}"

def generate_image(user_text):
    prompt = f"{user_text} in the style of TOK"
    
    try:
        image_bytes = query({"inputs": prompt})
        
        if isinstance(image_bytes, str) and "API Error" in image_bytes:
            return image_bytes  # Return error message
        
        image = Image.open(io.BytesIO(image_bytes))
        return image
    except Exception as e:
        return f"Error generating image: {str(e)}"

def fetch_showcase_images():
    """Fetch images from the local 'images' directory and use prompts from a JSON file."""
    images_dir = "images"
    json_file = os.path.join(images_dir, "prompts.json")
    showcase_images = []
    
    if os.path.exists(json_file):
        with open(json_file, "r") as f:
            image_prompts = json.load(f)
        
        for filename, prompt in image_prompts.items():
            image_path = os.path.join(images_dir, filename)
            if os.path.exists(image_path):
                showcase_images.append((image_path, prompt))
    
    return showcase_images

def create_interface():
    showcase_images = fetch_showcase_images()
    
    with gr.Blocks() as demo:
        gr.Markdown("## 🧵 EmbroideryAI - AI-Powered Embroidery Design")
        
        with gr.Row():
            user_input = gr.Textbox(label="Describe your embroidery design")
            btn = gr.Button("Generate")
        
        output_image = gr.Image(label="Generated Image")
        btn.click(generate_image, inputs=user_input, outputs=output_image)
        
        gr.Markdown("### 🎨 Showcase of AI-Generated Designs")
        for img_path, prompt in showcase_images:
            gr.Markdown(f"**Prompt:** {prompt}")
            gr.Image(value=img_path, show_label=False)
    
    demo.launch()

if __name__ == "__main__":
    create_interface()