Spaces:
Sleeping
Sleeping
File size: 5,364 Bytes
36e865a |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# Install required packages
import torch
from diffusers import StableDiffusionPipeline
import gradio as gr
import os
from web3 import Web3
import json
from PIL import Image
import io
import ipfshttpclient
import requests
from pathlib import Path
# Function to check GPU availability
def get_device():
if torch.cuda.is_available():
return "cuda"
return "cpu"
# Initialize the model
def initialize_model():
device = get_device()
print(f"Using device: {device}")
model_id = "runwayml/stable-diffusion-v1-5"
pipeline = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
safety_checker=None
)
pipeline = pipeline.to(device)
return pipeline
# Initialize IPFS client
def initialize_ipfs():
try:
# Try to connect to local IPFS daemon
client = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001')
return client
except Exception as e:
print(f"Could not connect to local IPFS: {e}")
# Fallback to Infura IPFS (you'll need to sign up for an Infura account)
return None
# Function to upload to IPFS (with fallback to Infura)
def upload_to_ipfs(image, ipfs_client=None):
try:
if ipfs_client:
# Convert PIL image to bytes
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Upload to IPFS
result = ipfs_client.add(img_byte_arr)
return result['Hash']
else:
# Fallback: Save locally
temp_path = "temp_artwork.png"
image.save(temp_path)
return f"Local file saved: {temp_path}"
except Exception as e:
return f"Error uploading to IPFS: {str(e)}"
# Initialize blockchain connection
def initialize_blockchain():
try:
# Connect to Polygon Mumbai testnet
w3 = Web3(Web3.HTTPProvider('https://rpc-mumbai.maticvigil.com'))
print(f"Connected to blockchain: {w3.is_connected()}")
return w3
except Exception as e:
print(f"Error connecting to blockchain: {e}")
return None
# Combined function to generate and process artwork
def generate_and_process_artwork(prompt, num_inference_steps, guidance_scale):
try:
# Generate the image
image = pipeline(
prompt,
num_inference_steps=int(num_inference_steps),
guidance_scale=float(guidance_scale)
).images[0]
# Try to upload to IPFS
ipfs_result = upload_to_ipfs(image, ipfs_client)
# Create metadata
metadata = {
"name": "AI Generated Artwork",
"description": f"Generated using Stable Diffusion with prompt: {prompt}",
"prompt": prompt,
"generation_params": {
"steps": num_inference_steps,
"guidance_scale": guidance_scale
}
}
success_message = f"Generation successful!\n"
if isinstance(ipfs_result, str) and ipfs_result.startswith("Qm"):
success_message += f"IPFS Hash: {ipfs_result}"
else:
success_message += "Artwork saved locally"
return image, success_message
except Exception as e:
return None, f"Error: {str(e)}"
# Create Gradio interface
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("# AI Art Generation with Blockchain Integration")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
placeholder="Enter your art description here...",
lines=3
)
steps = gr.Slider(
minimum=20,
maximum=100,
value=50,
step=1,
label="Number of Inference Steps"
)
guidance = gr.Slider(
minimum=1,
maximum=20,
value=7.5,
step=0.5,
label="Guidance Scale"
)
generate_btn = gr.Button("Generate Artwork")
with gr.Column():
output_image = gr.Image(label="Generated Artwork")
output_text = gr.Textbox(label="Status")
generate_btn.click(
fn=generate_and_process_artwork,
inputs=[prompt, steps, guidance],
outputs=[output_image, output_text]
)
gr.Markdown("""
## Features
- AI Art Generation using Stable Diffusion
- IPFS Storage Integration
- Automatic Metadata Generation
## Tips for Better Results
- Be specific in your prompts
- Use descriptive adjectives
- Include art style references
""")
return demo
# Initialize components
print("Initializing model...")
pipeline = initialize_model()
print("Initializing IPFS...")
ipfs_client = initialize_ipfs()
print("Initializing blockchain...")
w3 = initialize_blockchain()
# Launch the interface
demo = create_interface()
demo.launch(debug=True, share=True) |