Spaces:
Runtime error
Runtime error
File size: 19,894 Bytes
4ea8388 ca5cb45 4ea8388 |
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
import gradio as gr
from PIL import Image
from pathlib import Path
from transformers import CLIPTokenizer
import torch
import subprocess
import os
import random
import model_loader, pipeline
# Configure devices
DEVICE = "cpu"
ALLOW_CUDA = False
ALLOW_MPS = True
if torch.cuda.is_available() and ALLOW_CUDA:
DEVICE = "cuda"
elif torch.backends.mps.is_available() and ALLOW_MPS:
DEVICE = "mps"
print(f"Using device: {DEVICE}")
# Load Stable Diffusion model
tokenizer_vocab_path = Path("C:\\Users\\Esmail\\Desktop\\nanograd\\nanograd\\models\\stable_diffusion\\sd_data\\tokenizer_vocab.json")
tokenizer_merges_path = Path("C:\\Users\\Esmail\\Desktop\\nanograd\\nanograd\\models\\stable_diffusion\\sd_data\\tokenizer_merges.txt")
model_file = Path("C:\\Users\\Esmail\\Desktop\\nanograd\\nanograd\\models\\stable_diffusion\\sd_data\\v1-5-pruned-emaonly.ckpt")
tokenizer = CLIPTokenizer(str(tokenizer_vocab_path), merges_file=str(tokenizer_merges_path))
models = model_loader.preload_models_from_standard_weights(str(model_file), DEVICE)
# Blueprints for image generation and text generation
blueprints = {
"Visual Story": {
"sd_prompts": [
"A futuristic city skyline at dusk, flying cars, neon lights, cyberpunk style",
"A bustling marketplace in a futuristic city, holograms, diverse crowd",
"A serene park in a futuristic city with advanced technology blending with nature"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a futuristic city that blends natural elements with advanced technology.",
"Write about an advanced cityscape with unique technological elements.",
"Imagine a futuristic metropolis where nature and technology harmoniously coexist."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
# Other blueprints with similar structure...
"Nature & Poetry": {
"sd_prompts": [
"A peaceful mountain landscape at sunrise, photorealistic, serene",
"A tranquil lake surrounded by autumn trees, soft light, misty atmosphere",
"A hidden waterfall in a dense jungle, lush greenery, crystal clear water"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Write a short poem about a tranquil sunrise over the mountains.",
"Describe the beauty of a hidden waterfall in a jungle.",
"Compose a poetic reflection on the serenity of a lake at dawn."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
# Additional blueprints with multiple prompts...
"Dreamscape": {
"sd_prompts": [
"A surreal dreamscape with floating islands and bioluminescent creatures",
"An endless horizon of strange landscapes, blending day and night",
"A fantastical world with floating rocks and neon-lit skies"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a dreamlike world filled with wonder and mystery.",
"Write about a place where time doesn't exist, only dreams.",
"Create a story where reality and fantasy blur together."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Abstract Art": {
"sd_prompts": [
"Abstract painting with vibrant colors and dynamic shapes",
"A digital artwork with chaotic patterns and bold contrasts",
"Geometric abstraction with a focus on form and color"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Write a short description of an abstract painting.",
"Describe a piece of modern art that defies traditional norms.",
"Imagine a world where art is created by emotions, not hands."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Fashion Design": {
"sd_prompts": [
"A high-fashion model wearing a futuristic outfit, neon colors, catwalk pose",
"A chic ensemble blending classic elegance with modern flair",
"Avant-garde fashion with bold textures and unconventional shapes"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a unique and innovative fashion design.",
"Write about a new fashion trend inspired by nature.",
"Imagine a clothing line that combines style with sustainability."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Food & Recipe": {
"sd_prompts": [
"Abstract painting with vibrant colors and dynamic shapes",
"A digital artwork with chaotic patterns and bold contrasts",
"Geometric abstraction with a focus on form and color"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Write a short description of an abstract painting.",
"Describe a piece of modern art that defies traditional norms.",
"Imagine a world where art is created by emotions, not hands."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Interior Design": {
"sd_prompts": [
"A modern living room with sleek furniture, minimalist design, and natural light",
"A cozy study room with rich textures, warm colors, and elegant decor",
"An open-plan kitchen with contemporary appliances and stylish finishes"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe an interior design that combines modern and classic elements.",
"Write about a space that enhances productivity and relaxation through design.",
"Imagine a luxurious interior design for a high-end apartment."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Historical Fiction": {
"sd_prompts": [
"A bustling Victorian-era street with horse-drawn carriages and period architecture",
"A grand historical ballroom with opulent decor and elegantly dressed guests",
"An ancient battlefield with detailed historical accuracy and dramatic scenery"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a significant historical event as if it were a scene in a novel.",
"Write about a character navigating the challenges of a historical setting.",
"Imagine a historical figure interacting with modern technology."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Science Fiction": {
"sd_prompts": [
"A futuristic cityscape with flying cars, neon lights, and towering skyscrapers",
"An alien planet with unique landscapes, strange flora, and advanced technology",
"A space station with cutting-edge design and high-tech equipment"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a futuristic world where technology has reshaped society.",
"Write about an encounter with an alien civilization.",
"Imagine a story set in a distant future with advanced technology and space exploration."
],
"ollama_models": ["llama3", "aya", "codellama"]
},
"Character Design": {
"sd_prompts": [
"A detailed fantasy character with elaborate costumes and accessories",
"A sci-fi hero with futuristic armor and high-tech gadgets",
"A historical figure portrayed with accurate attire and realistic features"
],
"sd_cfg_scales": [9, 8, 7],
"sd_num_inference_steps": [60, 50, 45],
"sd_samplers": ["ddpm", "k_euler_ancestral", "euler"],
"ollama_prompts": [
"Describe a unique character from a fantasy novel, focusing on their appearance and personality.",
"Write about a futuristic character with advanced technology and a compelling backstory.",
"Imagine a historical figure as a character in a modern setting."
],
"ollama_models": ["llama3", "aya", "codellama"]
}
}
# Define functions for each feature
def generate_image(prompt, cfg_scale, num_inference_steps, sampler):
uncond_prompt = ""
do_cfg = True
input_image = None
strength = 0.9
seed = 42
output_image = pipeline.generate(
prompt=prompt,
uncond_prompt=uncond_prompt,
input_image=input_image,
strength=strength,
do_cfg=do_cfg,
cfg_scale=cfg_scale,
sampler_name=sampler,
n_inference_steps=num_inference_steps,
seed=seed,
models=models,
device=DEVICE,
idle_device="cpu",
tokenizer=tokenizer,
)
output_image = Image.fromarray(output_image)
return output_image
def apply_blueprint(blueprint_name):
if blueprint_name in blueprints:
bp = blueprints[blueprint_name]
sd_prompts = random.choice(bp["sd_prompts"])
sd_cfg_scale = random.choice(bp["sd_cfg_scales"])
sd_num_inference_steps = random.choice(bp["sd_num_inference_steps"])
sd_sampler = random.choice(bp["sd_samplers"])
ollama_prompts = random.choice(bp["ollama_prompts"])
ollama_model = random.choice(bp["ollama_models"])
return (
sd_prompts, sd_cfg_scale, sd_num_inference_steps, sd_sampler,
ollama_model, ollama_prompts
)
return "", 7, 20, "ddpm", "aya", ""
def download_checkpoint(checkpoint):
try:
# Run the litgpt download command
command = ["litgpt", "download", checkpoint]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output, error = process.communicate()
if process.returncode == 0:
return f"Checkpoint '{checkpoint}' downloaded successfully.\n{output}"
else:
return f"Error downloading checkpoint '{checkpoint}':\n{error}"
except Exception as e:
return f"Unexpected error: {str(e)}"
def chat_with_ollama(model_name, prompt):
command = ['ollama', 'run', model_name, prompt]
result = subprocess.run(command, capture_output=True, text=True)
return result.stdout
def install_ollama():
try:
# Command to install Ollama silently
installer_path = "OllamaSetup.exe"
if not os.path.exists(installer_path):
# Download the installer if not already available
subprocess.run(["curl", "-o", installer_path, "https://ollama.com/download/OllamaSetup.exe"], check=True)
# Run the installer silently
subprocess.run([installer_path, "/S"], check=True)
return "Ollama installed successfully."
except Exception as e:
return f"Installation failed: {str(e)}"
def welcome(name):
return f"Welcome to nanograd Engine, {name}!"
js = """
function createGradioAnimation() {
var container = document.createElement('div');
container.id = 'gradio-animation';
container.style.fontSize = '2em';
container.style.fontWeight = 'bold';
container.style.textAlign = 'center';
container.style.marginBottom = '20px';
var text = 'Welcome to nanograd Engine!';
for (var i = 0; i < text.length; i++) {
(function(i){
setTimeout(function(){
var letter = document.createElement('span');
letter.style.opacity = '0';
letter.style.transition = 'opacity 0.5s';
letter.innerText = text[i];
container.appendChild(letter);
setTimeout(function() {
letter.style.opacity = '1';
}, 50);
}, i * 250);
})(i);
}
var gradioContainer = document.querySelector('.gradio-container');
gradioContainer.insertBefore(container, gradioContainer.firstChild);
return 'Animation created';
}
"""
# Gradio interface
def gradio_interface():
with gr.Blocks('ParityError/Interstellar', js=js) as demo:
with gr.Tab("nano-Engine"):
with gr.Row():
with gr.Column(scale=1):
# Text Generation with Ollama
gr.Markdown("### Generate Text with Ollama")
ollama_model_name = gr.Dropdown(label="Select Ollama Model", choices=["aya", "llama3", "codellama"], value="aya")
ollama_prompts = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
ollama_output = gr.Textbox(label="Output", placeholder="Model output will appear here", interactive=True)
ollama_btn = gr.Button("Generate", variant="primary")
ollama_btn.click(fn=chat_with_ollama, inputs=[ollama_model_name, ollama_prompts], outputs=ollama_output)
gr.Markdown("### GPT Checkpoints Management")
checkpoint_dropdown = gr.Dropdown(label="Select Checkpoint", choices=["EleutherAI/gpt-neo-125M", "EleutherAI/gpt-neo-1.3B", "microsoft/phi-2", "codellama/CodeLlama-13b-hf"], value="EleutherAI/gpt-neo-125M")
download_btn = gr.Button("Download Checkpoint", variant="primary")
checkpoint_status = gr.Textbox(label="Download Status", placeholder="Status will appear here", interactive=True)
download_btn.click(fn=download_checkpoint, inputs=checkpoint_dropdown, outputs=checkpoint_status)
gr.Markdown("### Install Ollama")
install_ollama_btn = gr.Button("Install Ollama", variant="primary")
installation_status = gr.Textbox(label="Installation Status", placeholder="Status will appear here", interactive=True)
install_ollama_btn.click(fn=install_ollama, outputs=installation_status)
with gr.Column(scale=1):
gr.Markdown("### Stable Diffusion Image Generation")
prompt_input = gr.Textbox(label="Prompt", placeholder="A cat stretching on the floor, highly detailed, ultra sharp, cinematic, 100mm lens, 8k resolution")
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
num_inference_steps = gr.Slider(label="Sampling Steps", minimum=10, maximum=100, value=20, step=5)
sampler = gr.Radio(label="Sampling Method", choices=["ddpm", "Euler a", "Euler", "LMS", "Heun", "DPM2 a", "PLMS"], value="ddpm")
generate_img_btn = gr.Button("Generate", variant="primary")
output_image = gr.Image(label="Output", show_label=False, height=700, width=750)
generate_img_btn.click(fn=generate_image, inputs=[prompt_input, cfg_scale, num_inference_steps, sampler], outputs=output_image)
with gr.Tab("Blueprints"):
with gr.Row():
blueprint_dropdown = gr.Dropdown(label="Select Blueprint", choices=list(blueprints.keys()), value=list(blueprints.keys())[0])
load_blueprint_btn = gr.Button("Load Blueprint", variant="primary")
# Blueprint Outputs
sd_prompt_output = gr.Textbox(label="SD Prompt", interactive=True)
sd_cfg_output = gr.Slider(label="SD CFG Scale", minimum=1, maximum=20, step=1, interactive=True)
sd_steps_output = gr.Slider(label="SD Sampling Steps", minimum=10, maximum=100, step=5, interactive=True)
sd_sampler_output = gr.Radio(label="SD Sampler", choices=["ddpm", "Euler a", "Euler", "LMS", "Heun", "DPM2 a", "PLMS"], value="ddpm", interactive=True)
ollama_model_output = gr.Dropdown(label="Ollama Model", choices=["aya", "llama3", "codellama"], value="aya", interactive=True)
ollama_prompt_output = gr.Textbox(label="Ollama Prompt", interactive=True)
def load_blueprint(blueprint_name):
if blueprint_name in blueprints:
bp = blueprints[blueprint_name]
sd_prompts = random.choice(bp["sd_prompts"])
sd_cfg_scale = random.choice(bp["sd_cfg_scales"])
sd_num_inference_steps = random.choice(bp["sd_num_inference_steps"])
sd_sampler = random.choice(bp["sd_samplers"])
ollama_prompts = random.choice(bp["ollama_prompts"])
ollama_model = random.choice(bp["ollama_models"])
return (
sd_prompts, sd_cfg_scale, sd_num_inference_steps, sd_sampler,
ollama_model, ollama_prompts
)
return "", 7, 20, "ddpm", "aya", ""
def apply_loaded_blueprint(prompt, cfg_scale, num_inference_steps, sampler, model, ollama_prompts):
return (
gr.update(value=prompt),
gr.update(value=cfg_scale),
gr.update(value=num_inference_steps),
gr.update(value=sampler),
gr.update(value=model),
gr.update(value=ollama_prompts)
)
load_blueprint_btn.click(fn=load_blueprint, inputs=blueprint_dropdown, outputs=[sd_prompt_output, sd_cfg_output, sd_steps_output, sd_sampler_output, ollama_model_output, ollama_prompt_output])
load_blueprint_btn.click(fn=apply_loaded_blueprint, inputs=[sd_prompt_output, sd_cfg_output, sd_steps_output, sd_sampler_output, ollama_model_output, ollama_prompt_output], outputs=[prompt_input, cfg_scale, num_inference_steps, sampler, ollama_model_name, ollama_prompts])
with gr.Tab("Chatbot-Prompts"):
with gr.Row():
with gr.Column(scale=1):
from nanograd.models.GPT.tokenizer import tokenize
gr.Markdown("<h1><center>BPE Tokenizer</h1></center>")
iface = gr.Interface(fn=tokenize, inputs="text", outputs="json")
with gr.Column(scale=1):
from examples import ollama_prompted
gr.Markdown("<h1><center>Chatbot (لغة عربية)</h1></center>")
i = gr.Interface(
fn=ollama_prompted.run,
inputs=gr.Textbox(lines=1, placeholder="Ask a question about travel or airlines"),
outputs=gr.Textbox(label="Aya's response"),
)
demo.launch()
# Run the Gradio interface
gradio_interface() |