Skip to content

Commit

Permalink
Fixing Chatbot after Image Recognition (#66)
Browse files Browse the repository at this point in the history
Fixing Chatbot after Image Recognition# 

## ♻️ Current situation & Problem
Chatbot after image recognition was not working because the open-api key
specification wasnt working


## ⚙️ Release Notes 
changed a line to make the view public

---------
  • Loading branch information
kevinvzhu authored Mar 14, 2024
1 parent d059a47 commit 8a1b03c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Stronger/ProteinTracker/ChatWindowAfterCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ struct ChatWindowAfterCamera: View {
}

@LLMSessionProvider(schema: Self.llmSchema) var session: LLMOpenAISession
@State var showOnboarding = false
@State var showOnboarding = true

var loggedFoodItems: [String]
var body: some View {
let greetingMessage: String = "These are the foods you logged with your camera: \(loggedFoodItems.joined(separator: ", ")). Is this correct?"
Expand Down
6 changes: 3 additions & 3 deletions Stronger/ProteinTracker/FoodClassifierApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// SPDX-FileCopyrightText: 2023 Stanford University
//
// SPDX-License-Identifier: MIT
//
// s

import Combine
import CoreML
Expand Down Expand Up @@ -168,7 +168,7 @@ struct PhotoPicker: UIViewControllerRepresentable {
var imageClassifier: ImageClassifier
var sourceType: UIImagePickerController.SourceType
@Environment(\.presentationMode) var presentationMode

func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
Expand Down Expand Up @@ -244,7 +244,7 @@ struct FoodClassifierApp: View {
nextStepOptions
}
Spacer()

NavigationLink(
destination: ChatWindowAfterCamera(
loggedFoodItems: imageClassifier.loggedFoodItems
Expand Down
39 changes: 19 additions & 20 deletions Stronger/ProteinTracker/ProteinStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ struct ProteinStats: View {
@State private var averageWeeklyProtein: Double = 0.0
@State private var strokeStyle = StrokeStyle(lineWidth: 1.5, lineCap: .round, dash: [4])
@State private var weeklyData: [ProteinDataDaily] = []

@Environment(Account.self) var account

var body: some View {
VStack {
Text("Protein Intake Data")
.font(.title)
Spacer()
Spacer()



Text("Protein intake in the last 7 days")
.font(.headline)
Chart {
Expand All @@ -66,11 +65,11 @@ struct ProteinStats: View {
.chartLegend(position: .bottom, spacing: 20)
.chartForegroundStyleScale(["Daily target": Color.orange, "Weekly average": Color.pink])
.frame(height: 300)

Spacer()

Text(getTextualSummary())

Spacer()
}
.padding()
Expand All @@ -81,7 +80,7 @@ struct ProteinStats: View {
dailyTargetProtein = (try? await getdailyTargetProtein()) ?? 48.0
}
}

@MainActor
private func getTextualSummary() -> String {
let target = (dailyTargetProtein * 10).rounded() / 10
Expand All @@ -104,7 +103,7 @@ of protein per day this week.
return "Hello, " + message
}
}

private func getLastWeekDates() -> [Date] {
var calendar = Calendar(identifier: .gregorian)
if let tzPST = TimeZone(identifier: "America/Los_Angeles") {
Expand All @@ -121,7 +120,7 @@ of protein per day this week.
}
return dates.reversed()
}

private func getUserID() -> String {
if let currentUser = Auth.auth().currentUser {
userID = currentUser.uid
Expand All @@ -131,7 +130,7 @@ of protein per day this week.
}
return userID
}

private func getdailyTargetProtein() async throws -> Double {
guard let details = try await account.details else {
return dailyTargetProtein
Expand All @@ -143,22 +142,22 @@ of protein per day this week.
return dailyTargetProtein
}
}

private func fetchDataFromFirestore() async throws {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateFormatterXLabel = DateFormatter()
dateFormatterXLabel.dateFormat = "MM-dd"

let dates = getLastWeekDates()
var calendar = Calendar(identifier: .gregorian)
if let tzPST = TimeZone(identifier: "America/Los_Angeles") {
calendar.timeZone = tzPST
} else {
}

userID = getUserID()

let collectionRef = Firestore.firestore().collection("users").document(userID).collection("ProteinIntake")
print("Dates = \(dates)")
for date in dates {
Expand All @@ -172,28 +171,28 @@ of protein per day this week.
// let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
let startDateString = dateFormatter.string(from: startOfDay)
let endDateString = dateFormatter.string(from: endOfDay)

let storeDateString = dateFormatterXLabel.string(from: startOfDay)
var proteinContent = 0.0

let result = try await collectionRef
.whereField(FieldPath.documentID(), isGreaterThanOrEqualTo: startDateString)
.whereField(FieldPath.documentID(), isLessThan: endDateString)
.getDocuments()

for document in result.documents {
if let proteinContentString = document.data()["protein content"] as? String {
if let numericValue = proteinContentString.components(separatedBy: " ").first.flatMap(Double.init) {
proteinContent += numericValue
}
}
}

print("Protein content value is \(proteinContent)")
averageWeeklyProtein += (proteinContent / 7)
weeklyData.append(.init(date: storeDateString, protein: proteinContent))
}

weeklyData.sort { $0.date < $1.date }
}
}
Expand Down

0 comments on commit 8a1b03c

Please sign in to comment.