Spaces:
Sleeping
Sleeping
Anupam251272
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required packages
|
2 |
+
!pip install diffusers transformers torch gradio web3 ipfshttpclient requests
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from diffusers import StableDiffusionPipeline
|
6 |
+
import gradio as gr
|
7 |
+
import os
|
8 |
+
from web3 import Web3
|
9 |
+
import json
|
10 |
+
from PIL import Image
|
11 |
+
import io
|
12 |
+
import ipfshttpclient
|
13 |
+
import requests
|
14 |
+
from pathlib import Path
|
15 |
+
|
16 |
+
# Function to check GPU availability
|
17 |
+
def get_device():
|
18 |
+
if torch.cuda.is_available():
|
19 |
+
return "cuda"
|
20 |
+
return "cpu"
|
21 |
+
|
22 |
+
# Initialize the model
|
23 |
+
def initialize_model():
|
24 |
+
device = get_device()
|
25 |
+
print(f"Using device: {device}")
|
26 |
+
|
27 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
28 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
29 |
+
model_id,
|
30 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
31 |
+
safety_checker=None
|
32 |
+
)
|
33 |
+
pipeline = pipeline.to(device)
|
34 |
+
return pipeline
|
35 |
+
|
36 |
+
# Initialize IPFS client
|
37 |
+
def initialize_ipfs():
|
38 |
+
try:
|
39 |
+
# Try to connect to local IPFS daemon
|
40 |
+
client = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001')
|
41 |
+
return client
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Could not connect to local IPFS: {e}")
|
44 |
+
# Fallback to Infura IPFS (you'll need to sign up for an Infura account)
|
45 |
+
return None
|
46 |
+
|
47 |
+
# Function to upload to IPFS (with fallback to Infura)
|
48 |
+
def upload_to_ipfs(image, ipfs_client=None):
|
49 |
+
try:
|
50 |
+
if ipfs_client:
|
51 |
+
# Convert PIL image to bytes
|
52 |
+
img_byte_arr = io.BytesIO()
|
53 |
+
image.save(img_byte_arr, format='PNG')
|
54 |
+
img_byte_arr = img_byte_arr.getvalue()
|
55 |
+
|
56 |
+
# Upload to IPFS
|
57 |
+
result = ipfs_client.add(img_byte_arr)
|
58 |
+
return result['Hash']
|
59 |
+
else:
|
60 |
+
# Fallback: Save locally
|
61 |
+
temp_path = "temp_artwork.png"
|
62 |
+
image.save(temp_path)
|
63 |
+
return f"Local file saved: {temp_path}"
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error uploading to IPFS: {str(e)}"
|
67 |
+
|
68 |
+
# Initialize blockchain connection
|
69 |
+
def initialize_blockchain():
|
70 |
+
try:
|
71 |
+
# Connect to Polygon Mumbai testnet
|
72 |
+
w3 = Web3(Web3.HTTPProvider('https://rpc-mumbai.maticvigil.com'))
|
73 |
+
print(f"Connected to blockchain: {w3.is_connected()}")
|
74 |
+
return w3
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Error connecting to blockchain: {e}")
|
77 |
+
return None
|
78 |
+
|
79 |
+
# Combined function to generate and process artwork
|
80 |
+
def generate_and_process_artwork(prompt, num_inference_steps, guidance_scale):
|
81 |
+
try:
|
82 |
+
# Generate the image
|
83 |
+
image = pipeline(
|
84 |
+
prompt,
|
85 |
+
num_inference_steps=int(num_inference_steps),
|
86 |
+
guidance_scale=float(guidance_scale)
|
87 |
+
).images[0]
|
88 |
+
|
89 |
+
# Try to upload to IPFS
|
90 |
+
ipfs_result = upload_to_ipfs(image, ipfs_client)
|
91 |
+
|
92 |
+
# Create metadata
|
93 |
+
metadata = {
|
94 |
+
"name": "AI Generated Artwork",
|
95 |
+
"description": f"Generated using Stable Diffusion with prompt: {prompt}",
|
96 |
+
"prompt": prompt,
|
97 |
+
"generation_params": {
|
98 |
+
"steps": num_inference_steps,
|
99 |
+
"guidance_scale": guidance_scale
|
100 |
+
}
|
101 |
+
}
|
102 |
+
|
103 |
+
success_message = f"Generation successful!\n"
|
104 |
+
if isinstance(ipfs_result, str) and ipfs_result.startswith("Qm"):
|
105 |
+
success_message += f"IPFS Hash: {ipfs_result}"
|
106 |
+
else:
|
107 |
+
success_message += "Artwork saved locally"
|
108 |
+
|
109 |
+
return image, success_message
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
return None, f"Error: {str(e)}"
|
113 |
+
|
114 |
+
# Create Gradio interface
|
115 |
+
def create_interface():
|
116 |
+
with gr.Blocks() as demo:
|
117 |
+
gr.Markdown("# AI Art Generation with Blockchain Integration")
|
118 |
+
|
119 |
+
with gr.Row():
|
120 |
+
with gr.Column():
|
121 |
+
prompt = gr.Textbox(
|
122 |
+
label="Prompt",
|
123 |
+
placeholder="Enter your art description here...",
|
124 |
+
lines=3
|
125 |
+
)
|
126 |
+
steps = gr.Slider(
|
127 |
+
minimum=20,
|
128 |
+
maximum=100,
|
129 |
+
value=50,
|
130 |
+
step=1,
|
131 |
+
label="Number of Inference Steps"
|
132 |
+
)
|
133 |
+
guidance = gr.Slider(
|
134 |
+
minimum=1,
|
135 |
+
maximum=20,
|
136 |
+
value=7.5,
|
137 |
+
step=0.5,
|
138 |
+
label="Guidance Scale"
|
139 |
+
)
|
140 |
+
generate_btn = gr.Button("Generate Artwork")
|
141 |
+
|
142 |
+
with gr.Column():
|
143 |
+
output_image = gr.Image(label="Generated Artwork")
|
144 |
+
output_text = gr.Textbox(label="Status")
|
145 |
+
|
146 |
+
generate_btn.click(
|
147 |
+
fn=generate_and_process_artwork,
|
148 |
+
inputs=[prompt, steps, guidance],
|
149 |
+
outputs=[output_image, output_text]
|
150 |
+
)
|
151 |
+
|
152 |
+
gr.Markdown("""
|
153 |
+
## Features
|
154 |
+
- AI Art Generation using Stable Diffusion
|
155 |
+
- IPFS Storage Integration
|
156 |
+
- Automatic Metadata Generation
|
157 |
+
|
158 |
+
## Tips for Better Results
|
159 |
+
- Be specific in your prompts
|
160 |
+
- Use descriptive adjectives
|
161 |
+
- Include art style references
|
162 |
+
""")
|
163 |
+
|
164 |
+
return demo
|
165 |
+
|
166 |
+
# Initialize components
|
167 |
+
print("Initializing model...")
|
168 |
+
pipeline = initialize_model()
|
169 |
+
print("Initializing IPFS...")
|
170 |
+
ipfs_client = initialize_ipfs()
|
171 |
+
print("Initializing blockchain...")
|
172 |
+
w3 = initialize_blockchain()
|
173 |
+
|
174 |
+
# Launch the interface
|
175 |
+
demo = create_interface()
|
176 |
+
demo.launch(debug=True, share=True)
|