parent
9543b2d742
commit
9541b078eb
@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"filename" : "sample1_512x512.png",
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 555 KiB |
@ -0,0 +1,78 @@
|
||||
//
|
||||
// ImageToImageView.swift
|
||||
// imggensd2
|
||||
//
|
||||
// Created by Yasuhito Nagatomo on 2023/02/11.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ImageToImageView: View {
|
||||
static let prompt = "happy smile snow winter"
|
||||
static let negativePrompt =
|
||||
"""
|
||||
lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits,
|
||||
cropped, worst quality, low quality, normal quality, jpeg artifacts, blurry, multiple legs, malformation
|
||||
"""
|
||||
static let startImageName = "sample1_512x512"
|
||||
|
||||
@ObservedObject var imageGenerator: ImageGenerator
|
||||
@State private var generationParameter =
|
||||
ImageGenerator.GenerationParameter(mode: .imageToImage,
|
||||
prompt: prompt,
|
||||
negativePrompt: negativePrompt,
|
||||
guidanceScale: 8.0,
|
||||
seed: 1_000_000,
|
||||
stepCount: 20,
|
||||
imageCount: 1, disableSafety: false,
|
||||
startImage: UIImage(named: startImageName)?.cgImage,
|
||||
strength: 0.5)
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack {
|
||||
Text("Image to image").font(.title3).bold().padding(6)
|
||||
Text("Sample App using apple/ml-stable-diffusion")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
.padding(.bottom)
|
||||
|
||||
Image(ImageToImageView.startImageName)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(height: 200)
|
||||
|
||||
PromptView(parameter: $generationParameter)
|
||||
.disabled(imageGenerator.generationState != .idle)
|
||||
|
||||
if imageGenerator.generationState == .idle {
|
||||
Button(action: generate) {
|
||||
Text("Generate").font(.title)
|
||||
}.buttonStyle(.borderedProminent)
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
|
||||
if let generatedImages = imageGenerator.generatedImages {
|
||||
ForEach(generatedImages.images) {
|
||||
Image(uiImage: $0.uiImage)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
func generate() {
|
||||
imageGenerator.generateImages(generationParameter)
|
||||
}
|
||||
}
|
||||
|
||||
struct ImageToImageView_Previews: PreviewProvider {
|
||||
static let imageGenerator = ImageGenerator()
|
||||
static var previews: some View {
|
||||
ImageToImageView(imageGenerator: imageGenerator)
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
//
|
||||
// PromptView.swift
|
||||
// imggensd2
|
||||
//
|
||||
// Created by Yasuhito Nagatomo on 2023/02/11.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct PromptView: View {
|
||||
@Binding var parameter: ImageGenerator.GenerationParameter
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack { Text("Prompt:"); Spacer() }
|
||||
TextField("Prompt:", text: $parameter.prompt)
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
HStack { Text("Negative Prompt:"); Spacer() }
|
||||
TextField("Negative Prompt:", text: $parameter.negativePrompt)
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
Stepper(value: $parameter.guidanceScale, in: 0.0...40.0, step: 0.5) {
|
||||
Text("Guidance scale: \(parameter.guidanceScale, specifier: "%.1f") ")
|
||||
}
|
||||
Stepper(value: $parameter.imageCount, in: 1...10) {
|
||||
Text("Image Count: \(parameter.imageCount)")
|
||||
}
|
||||
Stepper(value: $parameter.stepCount, in: 1...100) {
|
||||
Text("Iteration steps: \(parameter.stepCount)")
|
||||
}
|
||||
HStack { Text("Seed:"); Spacer() }
|
||||
TextField("Seed number (0 ... 4_294_967_295)",
|
||||
value: $parameter.seed,
|
||||
formatter: NumberFormatter())
|
||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||
.onSubmit {
|
||||
if parameter.seed < 0 {
|
||||
parameter.seed = 0
|
||||
} else if parameter.seed > UInt32.max {
|
||||
parameter.seed = Int(UInt32.max)
|
||||
} else {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
if parameter.mode == .imageToImage {
|
||||
Stepper(value: $parameter.strength, in: 0.0...0.9, step: 0.1) {
|
||||
Text("Strength: \(parameter.strength, specifier: "%.1f") ")
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
struct PromptView_Previews: PreviewProvider {
|
||||
@State static var param = ImageGenerator.GenerationParameter(mode: .imageToImage,
|
||||
prompt: "a prompt",
|
||||
negativePrompt: "a negative prompt",
|
||||
guidanceScale: 0.5,
|
||||
seed: 1_000,
|
||||
stepCount: 20,
|
||||
imageCount: 1,
|
||||
disableSafety: false,
|
||||
strength: 0.5)
|
||||
static var previews: some View {
|
||||
PromptView(parameter: $param)
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
//
|
||||
// TextToImageView.swift
|
||||
// imggensd2
|
||||
//
|
||||
// Created by Yasuhito Nagatomo on 2023/02/11.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct TextToImageView: View {
|
||||
static let prompt = "a photo of an astronaut riding a horse on mars"
|
||||
static let negativePrompt =
|
||||
"""
|
||||
lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits,
|
||||
cropped, worst quality, low quality, normal quality, jpeg artifacts, blurry, multiple legs, malformation
|
||||
"""
|
||||
|
||||
@ObservedObject var imageGenerator: ImageGenerator
|
||||
@State private var generationParameter =
|
||||
ImageGenerator.GenerationParameter(mode: .textToImage,
|
||||
prompt: prompt,
|
||||
negativePrompt: negativePrompt,
|
||||
guidanceScale: 8.0,
|
||||
seed: 1_000_000,
|
||||
stepCount: 20,
|
||||
imageCount: 1, disableSafety: false)
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack {
|
||||
Text("Text to image").font(.title3).bold().padding(6)
|
||||
Text("Sample App using apple/ml-stable-diffusion")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
.padding(.bottom)
|
||||
|
||||
PromptView(parameter: $generationParameter)
|
||||
.disabled(imageGenerator.generationState != .idle)
|
||||
|
||||
if imageGenerator.generationState == .idle {
|
||||
Button(action: generate) {
|
||||
Text("Generate").font(.title)
|
||||
}.buttonStyle(.borderedProminent)
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
|
||||
if let generatedImages = imageGenerator.generatedImages {
|
||||
ForEach(generatedImages.images) {
|
||||
Image(uiImage: $0.uiImage)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
func generate() {
|
||||
imageGenerator.generateImages(generationParameter)
|
||||
}
|
||||
}
|
||||
|
||||
struct TextToImageView_Previews: PreviewProvider {
|
||||
static let imageGenerator = ImageGenerator()
|
||||
static var previews: some View {
|
||||
TextToImageView(imageGenerator: imageGenerator)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue