From 0c7e1fc7a2241142ed125a667633bde0e385b03b Mon Sep 17 00:00:00 2001 From: Han Yin Date: Mon, 21 Apr 2025 23:13:35 -0700 Subject: [PATCH] [WIP] UI: replace the HuggingFace stub in Model Management screen with actual API call --- .../ui/screens/ModelsManagementScreen.kt | 19 +++++++++++++++++++ .../viewmodel/ModelsManagementViewModel.kt | 19 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/ui/screens/ModelsManagementScreen.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/ui/screens/ModelsManagementScreen.kt index 96ede3f37f..0c6a6198bd 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/revamp/ui/screens/ModelsManagementScreen.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/ui/screens/ModelsManagementScreen.kt @@ -221,6 +221,25 @@ fun ModelsManagementScreen( is ModelManagementState.Idle -> { /* Idle state, nothing to show */ } } } + + // TODO-han.yin: UI TO BE IMPLEMENTED + val huggingFaceModelsFlow = viewModel.huggingFaceModels + LaunchedEffect(Unit) { + huggingFaceModelsFlow.collect { models -> + val message = models.fold( + StringBuilder("Fetched ${models.size} models from HuggingFace") + ) { builder, model -> + builder.append(model.id).append("\n") + }.toString() + + onScaffoldEvent( + ScaffoldEvent.ShowSnackbar( + message = message, + duration = SnackbarDuration.Short + ) + ) + } + } } @Composable diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/viewmodel/ModelsManagementViewModel.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/viewmodel/ModelsManagementViewModel.kt index 783864d59c..14386069d6 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/revamp/viewmodel/ModelsManagementViewModel.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/viewmodel/ModelsManagementViewModel.kt @@ -10,6 +10,7 @@ import com.example.llama.revamp.data.model.ModelInfo import com.example.llama.revamp.data.model.ModelSortOrder import com.example.llama.revamp.data.model.filterBy import com.example.llama.revamp.data.model.sortByOrder +import com.example.llama.revamp.data.remote.HuggingFaceModel import com.example.llama.revamp.data.repository.InsufficientStorageException import com.example.llama.revamp.data.repository.ModelRepository import com.example.llama.revamp.util.getFileNameFromUri @@ -19,7 +20,9 @@ import com.example.llama.revamp.viewmodel.ModelManagementState.Importation import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest @@ -213,8 +216,20 @@ class ModelsManagementViewModel @Inject constructor( } } - // TODO-han.yin: Stub for now. Would need to investigate HuggingFace APIs - fun importFromHuggingFace() {} + // TODO-han.yin: UI TO BE IMPLEMENTED + private val _huggingFaceModels = MutableSharedFlow>() + val huggingFaceModels: SharedFlow> = _huggingFaceModels + + fun importFromHuggingFace() = viewModelScope.launch { + modelRepository.searchHuggingFaceModels().let { models -> + _huggingFaceModels.emit(models) + + Log.d(TAG, "Fetched ${models.size} models from HuggingFace:") + models.forEachIndexed { index, model -> + Log.d(TAG, "#$index: ${model.id}") + } + } + } /** * First show confirmation instead of starting deletion immediately