ContentView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import SwiftUI
  2. struct ContentView: View {
  3. @StateObject var llamaState = LlamaState()
  4. @State private var multiLineText = ""
  5. @State private var showingHelp = false // To track if Help Sheet should be shown
  6. var body: some View {
  7. NavigationView {
  8. VStack {
  9. ScrollView(.vertical, showsIndicators: true) {
  10. Text(llamaState.messageLog)
  11. .font(.system(size: 12))
  12. .frame(maxWidth: .infinity, alignment: .leading)
  13. .padding()
  14. .onTapGesture {
  15. UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
  16. }
  17. }
  18. TextEditor(text: $multiLineText)
  19. .frame(height: 80)
  20. .padding()
  21. .border(Color.gray, width: 0.5)
  22. HStack {
  23. Button("Send") {
  24. sendText()
  25. }
  26. Button("Bench") {
  27. bench()
  28. }
  29. Button("Clear") {
  30. clear()
  31. }
  32. Button("Copy") {
  33. UIPasteboard.general.string = llamaState.messageLog
  34. }
  35. }
  36. .buttonStyle(.bordered)
  37. .padding()
  38. NavigationLink(destination: DrawerView(llamaState: llamaState)) {
  39. Text("View Models")
  40. }
  41. .padding()
  42. }
  43. .padding()
  44. .navigationBarTitle("Model Settings", displayMode: .inline)
  45. }
  46. }
  47. func sendText() {
  48. Task {
  49. await llamaState.complete(text: multiLineText)
  50. multiLineText = ""
  51. }
  52. }
  53. func bench() {
  54. Task {
  55. await llamaState.bench()
  56. }
  57. }
  58. func clear() {
  59. Task {
  60. await llamaState.clear()
  61. }
  62. }
  63. struct DrawerView: View {
  64. @ObservedObject var llamaState: LlamaState
  65. @State private var showingHelp = false
  66. func delete(at offsets: IndexSet) {
  67. offsets.forEach { offset in
  68. let model = llamaState.downloadedModels[offset]
  69. let fileURL = getDocumentsDirectory().appendingPathComponent(model.filename)
  70. do {
  71. try FileManager.default.removeItem(at: fileURL)
  72. } catch {
  73. print("Error deleting file: \(error)")
  74. }
  75. }
  76. // Remove models from downloadedModels array
  77. llamaState.downloadedModels.remove(atOffsets: offsets)
  78. }
  79. func getDocumentsDirectory() -> URL {
  80. let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
  81. return paths[0]
  82. }
  83. var body: some View {
  84. List {
  85. Section(header: Text("Download Models From Hugging Face")) {
  86. HStack {
  87. InputButton(llamaState: llamaState)
  88. }
  89. }
  90. Section(header: Text("Downloaded Models")) {
  91. ForEach(llamaState.downloadedModels) { model in
  92. DownloadButton(llamaState: llamaState, modelName: model.name, modelUrl: model.url, filename: model.filename)
  93. }
  94. .onDelete(perform: delete)
  95. }
  96. Section(header: Text("Default Models")) {
  97. ForEach(llamaState.undownloadedModels) { model in
  98. DownloadButton(llamaState: llamaState, modelName: model.name, modelUrl: model.url, filename: model.filename)
  99. }
  100. }
  101. }
  102. .listStyle(GroupedListStyle())
  103. .navigationBarTitle("Model Settings", displayMode: .inline).toolbar {
  104. ToolbarItem(placement: .navigationBarTrailing) {
  105. Button("Help") {
  106. showingHelp = true
  107. }
  108. }
  109. }.sheet(isPresented: $showingHelp) { // Sheet for help modal
  110. VStack(alignment: .leading) {
  111. VStack(alignment: .leading) {
  112. Text("1. Make sure the model is in GGUF Format")
  113. .padding()
  114. Text("2. Copy the download link of the quantized model")
  115. .padding()
  116. }
  117. Spacer()
  118. }
  119. }
  120. }
  121. }
  122. }
  123. struct ContentView_Previews: PreviewProvider {
  124. static var previews: some View {
  125. ContentView()
  126. }
  127. }