This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Sticker | |
struct CardView: View { | |
var body: some View { | |
Image("wedding-photo") | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.frame(width: 300, height: 300) | |
.stickerEffect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func handleCLIPPrediction(from request: VNRequest, name: String) -> CardStats? { | |
let result = request.results?.first as! VNCoreMLFeatureValueObservation | |
let imageEmbedding = result.featureValue.multiArrayValue! | |
let floatArray: [Float] = (0..<imageEmbedding.count).map { | |
Float(truncating: imageEmbedding[$0]) | |
} | |
let norm = sqrt(floatArray.reduce(0) { $0 + $1 * $1 }) | |
let normalized = floatArray.map { $0 / norm } | |
let title = predictTitle(from: normalized, name) | |
return CardStats(title: title, stats: stats) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makePredictions(for photo: UIImage, name: String) async throws -> CardStats? { | |
let orientation = CGImagePropertyOrientation(photo.imageOrientation) | |
let photoImage = photo.cgImage! | |
return try await withCheckedThrowingContinuation { continuation in | |
let handler = VNImageRequestHandler(cgImage: photoImage, orientation: orientation) | |
let request = VNCoreMLRequest(model: model) { request, _ in | |
let cardStats = self.handleCLIPPrediction(from: request, name: name)! | |
continuation.resume(returning: cardStats) | |
} | |
request.imageCropAndScaleOption = .centerCrop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func loadCLIPModelAndEmbeddings() { | |
let defaultConfig = MLModelConfiguration() | |
let modelURL = Bundle.main.url(forResource: "mobileclip_blt_image", withExtension: "mlmodelc")! | |
let imageClassifierModel = try! MLModel(contentsOf: modelURL, configuration: defaultConfig) | |
self.imageClassifierVisionModel = try? VNCoreMLModel(for: imageClassifierModel)! | |
let statsURL = Bundle.main.url(forResource: "stats_embeddings", withExtension: "json")! | |
let statsData = try! Data(contentsOf: statsURL) | |
self.statsEmbeddings = try! JSONDecoder().decode([StatEmbedding].self, from: statsData) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"angle": 152.45, | |
"device_name": "Fahrenheit 451Mbps", | |
"timestamp": "2025-04-25T19:20:33.042516", | |
"temperature": 35.4 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
struct DialView: View { | |
let webSocketService = WebSocketServiceImpl.getOrCreateInstance(endpoint: "thermostat") | |
@AppStorage("maxTemperature") private var maxTemperature: Double = 37 | |
@State private var showOverheatWarning = false | |
@State private var currentState: ThermostatState? | |
@State private var cancellables = Set<AnyCancellable>() | |
private let decoder = JSONDecoder() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Charts | |
struct AuctionView: View { | |
let webSocketService = WebSocketServiceImpl.getOrCreateInstance(endpoint: "auction") | |
@State private var highestBid: AuctionBid? | |
@State private var bidHistory: [AuctionBid] = [] | |
@State private var timeRemaining: Int = 120 | |
@State private var timer: Timer.TimerPublisher = Timer.publish(every: 1, on: .main, in: .common) | |
@State private var cancellables = Set<AnyCancellable>() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct GameView: View { | |
let webSocketService: WebSocketService | |
@State private var currentGame: GameState? | |
@State private var cancellables = Set<AnyCancellable>() | |
@State private var hasStarted = false | |
private let decoder = JSONDecoder() | |
init(webSocketService: WebSocketService = WebSocketServiceImpl.getOrCreateInstance(endpoint: "game")) { | |
self.webSocketService = webSocketService | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Preview { | |
GameView(webSocketService: MockWebsocketService(characters: [ | |
.init(name: "Mage", emoji: "🧙♂️", x: 100, y: 200), | |
.init(name: "Warrior", emoji: "⚔", x: 400, y: 300), | |
.init(name: "Rogue", emoji: "🧝♀️", x: 200, y: 100), | |
.init(name: "Archer", emoji: "🏹", x: 500, y: 400) | |
])) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MockWebsocketService: WebSocketService { | |
let publisher: AnyPublisher<Data, Error> | |
init(characters: [GameState.GameCharacter]) { | |
let state = GameState(characters: characters) | |
let data = try! JSONEncoder().encode(state) | |
self.publisher = Just(data) | |
.setFailureType(to: Error.self) | |
.eraseToAnyPublisher() | |
} |