Skip to content

Commit

Permalink
Moderation demo view
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrochabrun committed Oct 24, 2023
1 parent 7c1277d commit 27a2c31
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/jamesrochabrun/SwiftOpenAI",
"state" : {
"branch" : "main",
"revision" : "75cb57017618400ccde01834777f201358839a45"
"revision" : "7c1277d50e05521c7fe61e5ea3c58bbaa4a91aea"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ struct ModerationDemoView: View {
@State private var moderationProvider: ModerationProvider
@State private var isLoading = false
@State private var prompt = ""

@State private var errorMessage = ""

init(service: OpenAIService) {
_moderationProvider = State(initialValue: ModerationProvider(service: service))
}

var body: some View {
VStack {
textArea
if moderationProvider.isFlagged {
Text("That is not a nice thing to say.")
}
if !errorMessage.isEmpty {
Text("Error \(errorMessage)")
.bold()
}
}

.overlay(
Group {
if isLoading {
ProgressView()
} else {
EmptyView()
}
}
)
}

var textArea: some View {
Expand All @@ -36,6 +52,8 @@ struct ModerationDemoView: View {
defer { isLoading = false } // ensure isLoading is set to false when the
do {
try await moderationProvider.createModerationFromText(parameters: .init(input: prompt))
} catch {
errorMessage = "\(error)"
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ struct OptionsListView: View {
ImagesDemoView(service: openAIService)
case .models:
ModelsDemoView(service: openAIService)
default:
EmptyView()
case .moderations:
ModerationDemoView(service: openAIService)
}
}
}
Expand Down

0 comments on commit 27a2c31

Please sign in to comment.