Stuck

I have a big problem with an AI app. It's supposed to give me a color according to the text entered into a TextField. But every time it gives me the same output. I'm new in developing, and I know it must be a tiny bug but I just couldn't find out why. Should I just ... paste all of my code here? I can't upload a .zip :(

Answered by Claude31 in 712683022

Just paste enough code so that we can understand what you are trying to do. Please paste with "Paste and Match Style" and format code with tool formatter.

Please explain what you get and what you expect.

Accepted Answer

Just paste enough code so that we can understand what you are trying to do. Please paste with "Paste and Match Style" and format code with tool formatter.

Please explain what you get and what you expect.

I think this is enough

extension String{

	func predict() -> [String: Double] {

		do {

			let mlModel = try ColorProposer(configuration: MLModelConfiguration()).model

			let sentimentPredictor = try NLModel(mlModel: mlModel)

			print(sentimentPredictor.predictedLabelHypotheses(for: self, maximumCount: 3))

			return sentimentPredictor.predictedLabelHypotheses(for: self, maximumCount: 3)

		} catch {

			fatalError("\(error)")

		}

	}

}
func average(_ value: [String: Double]) ->  (r: Double, g: Double, b: Double){

	var r: Double = 0

	var b: Double = 0

	var g: Double = 0

	for key in value.indices{

		r += Double(value[key].key.split(separator: ",")[0]) ?? 0

		g += Double(value[key].key.split(separator: ",")[1]) ?? 0

		b += Double(value[key].key.split(separator: ",")[2]) ?? 0

	}

	return (r: r / 3, g: r / 3, b: b / 3)

}

extension String{

	func returnColor() -> Color{

		switch self{

		case "red":

			return Color.red

		case "orange":

			return Color.orange

		case "yellow":

			return Color.yellow

		case "green":

			return Color.green

		case "mint":

			return Color.mint

		case "blue":

			return Color.blue

		case "purple":

			return Color.purple

		case "black":

			return Color.black

		case "gray":

			return Color.gray

		case "brown":

			return Color.brown

		case "white":

			return Color.white

		case "magenta":

			return Color(hex: 0xFF007F)   // I embed a framework

		case "cyan":

			return Color.cyan

		default:

			fatalError()

		}

		func returnColorString() -> String{

			switch self{

				case "red":

					return "256,000,000"

				case "orange":

					return "256,128,000"

				case "yellow":

					return "256,256,000"

				case "green":

					return "000,256,000"

				case "blue":

					return "000,000,256"

				case "mint":

					return "000,128,256"

				case "purple":

					return "256,000,256"

				case "black":

					return "000,000,000"

				case "gray":

					return "128,128,128"

				case "brown":

					return "128,064,000"

				case "white":

					return "256,256,256"

				case "magenta":

					return "256,000,128"

				case "cyan":

					return "000,256,256"

				default:

					 fatalError()

			}

		}

	}

}

public func returnColorString(_ value: String) -> String{

	switch value{

		case "red":

			return "256,000,000"

		case "orange":

			return "256,128,000"

		case "yellow":

			return "256,256,000"

		case "green":

			return "000,256,000"

		case "blue":

			return "000,000,256"

		case "mint":

			return "000,128,256"

		case "purple":

			return "256,000,256"

		case "black":

			return "000,000,000"

		case "gray":

			return "128,128,128"

		case "brown":

			return "128,064,000"

		case "white":

			return "256,256,256"

		case "magenta":

			return "256,000,128"

		case "cyan":

			return "000,256,256"

		default:

			 fatalError()

	}

}

func toColorArray(_ value: [String: Double]) -> [String: Double]{

	var returnValue: [String: Double] = [:]

	for item in value {

		if item.key.split(separator: ",").count == 3{

			returnValue[item.key] = item.value

		} else {

			returnValue[returnColorString(item.key)] = item.value

		}

	}

	return returnValue

}

func computeColor(_ value: (r: Double, g: Double, b: Double)) -> Color {

	return Color(red: value.r / 255, green: value.g / 255, blue: value.b / 255)

}
/// MARK - here's the function that's supposed to return the color predicted
func main(_ value: String) -> Color {

	let prediction = value.predict()

	let arrayised = toColorArray(prediction)

	let computedColor = average(arrayised)

	let returnedColor = Color(red: computedColor.r / 255, green: computedColor.g / 255, blue: computedColor.b / 255)

	return returnedColor

}
/// and this is for debugging
func debugFunc(_ value: String) -> String {

	let prediction = value.predict()

	let arrayised = toColorArray(prediction)

	let computedColor = average(arrayised)

	return String(computedColor.r) + String(computedColor.b) + String(computedColor.b)

}

ContentView:

import SwiftUI



struct ContentView: View {

	@State var input: String = ""

	@State var bg: Color = Color.white

    var body: some View {

		TextField("Enter...", text: $input).padding().textFieldStyle(.plain).frame(alignment: .center).onSubmit {

			input = input

			bg = main("input")

			print(input)

			print(colorArray(input))

		}.background{

			bg

		}

    }

}

Here's the function in the framework I was using:

public extension Color {

    init(hex: Int, alpha: Double = 1) {

        let components = (

            R: Double((hex >> 16) & 0xff) / 255,

            G: Double((hex >> 08) & 0xff) / 255,

            B: Double((hex >> 00) & 0xff) / 255

        )

        self.init(

            .sRGB,

            red: components.R,

            green: components.G,

            blue: components.B,

            opacity: alpha

        )

    }

}

oh i almost find out why it must be my mlmodel

but how on earth can i upload a mlmodel here?

I just found out a way I can change the extension to a devforum supported type and someone else could download it and change the extension back

oh no it wont work AAAAAAA

Stuck
 
 
Q