|
| 1 | +<template> |
| 2 | + <v-card height="100%"> |
| 3 | + <v-card-title class="font-weight-light secondary--text">Create a Giskard Client</v-card-title> |
| 4 | + <v-card-text v-if="giskardClientTemplate != null"> |
| 5 | + <CodeSnippet :code-content="giskardClientTemplate"/> |
| 6 | + </v-card-text> |
| 7 | + <v-card-text v-else-if="needFetchWithHFAccessToken == null"> |
| 8 | + <LoadingFullscreen :name="'Giskard Client creating instructions'"/> |
| 9 | + </v-card-text> |
| 10 | + <v-card-text v-else-if="!state.workerStatus.connected && !props.internalHFAccessToken"> |
| 11 | + <div class="mb-2"> |
| 12 | + <v-btn small tile color="primaryLight" class="primaryLightBtn" @click="generateHFSpacesToken">Generate</v-btn> |
| 13 | + </div> |
| 14 | + <v-row> |
| 15 | + <v-col> |
| 16 | + Please enter your Hugging Face access token in Giskard Settings page to generate the instructions. |
| 17 | + </v-col> |
| 18 | + </v-row> |
| 19 | + </v-card-text> |
| 20 | + <v-card-text v-else> |
| 21 | + <HuggingFaceTokenCard @submit="fetchAndSaveHFSpacesTokenWithAccessToken"/> |
| 22 | + </v-card-text> |
| 23 | + </v-card> |
| 24 | +</template> |
| 25 | + |
| 26 | +<script setup lang="ts"> |
| 27 | +import { ref, onMounted } from "vue"; |
| 28 | +import { useMainStore } from "@/stores/main"; |
| 29 | +import { TYPE } from "vue-toastification"; |
| 30 | +
|
| 31 | +import CodeSnippet from "@/components/CodeSnippet.vue"; |
| 32 | +import LoadingFullscreen from "./LoadingFullscreen.vue"; |
| 33 | +import HuggingFaceTokenCard from "./HuggingFaceTokenCard.vue"; |
| 34 | +
|
| 35 | +import { saveLocalHFToken } from "@/utils"; |
| 36 | +import { useHFSpacesTokenStore } from "@/stores/hfspaces"; |
| 37 | +
|
| 38 | +import { apiURL } from "@/env"; |
| 39 | +import { state } from "@/socket"; |
| 40 | +
|
| 41 | +import { useApiKeyStore } from "@/stores/api-key-store"; |
| 42 | +
|
| 43 | +const apiKeyStore = useApiKeyStore(); |
| 44 | +const mainStore = useMainStore(); |
| 45 | +
|
| 46 | +interface Props { |
| 47 | + internalHFAccessToken: boolean, |
| 48 | +} |
| 49 | +
|
| 50 | +const props = withDefaults(defineProps<Props>(), { |
| 51 | + internalHFAccessToken: false, |
| 52 | +}); |
| 53 | +
|
| 54 | +function generateGiskardClientInstruction(hf_token: string | null = null) { |
| 55 | + let snippet = `from giskard import GiskardClient |
| 56 | +
|
| 57 | +url = "${apiURL}" |
| 58 | +api_token = "${apiKeyStore.getFirstApiKey ? apiKeyStore.getFirstApiKey : '<Generate your API Key first>'}" |
| 59 | +`; |
| 60 | + if (hf_token) { |
| 61 | + snippet += `hf_token = "${hf_token}" |
| 62 | +`; |
| 63 | + } |
| 64 | + snippet += ` |
| 65 | +# Create a giskard client to communicate with Giskard |
| 66 | +client = GiskardClient(url, api_token`; |
| 67 | + if (hf_token) { |
| 68 | + snippet += `, hf_token`; |
| 69 | + } |
| 70 | + snippet += `)`; |
| 71 | + return snippet; |
| 72 | +}; |
| 73 | +
|
| 74 | +// Hugging Face Spaces token (Giskard Space token) |
| 75 | +const giskardClientTemplate = ref<string | null>(null); |
| 76 | +const needFetchWithHFAccessToken = ref<boolean | null>(null); |
| 77 | +
|
| 78 | +async function fetchAndSaveHFSpacesTokenWithAccessToken(accessToken: string) { |
| 79 | + if (mainStore.appSettings!.isRunningOnHfSpaces) { |
| 80 | + saveLocalHFToken(accessToken); |
| 81 | + const hfSpacesTokenStore = useHFSpacesTokenStore(); |
| 82 | + const token = await hfSpacesTokenStore.getHFSpacesToken(); |
| 83 | + if (token === null) { |
| 84 | + needFetchWithHFAccessToken.value = true; |
| 85 | + mainStore.addNotification({content: 'Invalid Hugging Face access token', color: TYPE.ERROR}); |
| 86 | + } else { |
| 87 | + giskardClientTemplate.value = generateGiskardClientInstruction(token); |
| 88 | + needFetchWithHFAccessToken.value = false; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +async function generateHFSpacesToken() { |
| 94 | + if (mainStore.appSettings?.isRunningOnHfSpaces) { |
| 95 | + const hfSpacesTokenStore = useHFSpacesTokenStore(); |
| 96 | + const token = await hfSpacesTokenStore.getHFSpacesToken(); |
| 97 | + if (token === null) { |
| 98 | + // Private HFSpaces or no valid HF access token |
| 99 | + needFetchWithHFAccessToken.value = true; |
| 100 | + return; |
| 101 | + } else if (!hfSpacesTokenStore.publicSpace) { |
| 102 | + giskardClientTemplate.value = generateGiskardClientInstruction(token); |
| 103 | + } else { |
| 104 | + giskardClientTemplate.value = generateGiskardClientInstruction(); |
| 105 | + } |
| 106 | + needFetchWithHFAccessToken.value = false; |
| 107 | + } |
| 108 | +} |
| 109 | +
|
| 110 | +onMounted(async () => { |
| 111 | + await apiKeyStore.getAll(); |
| 112 | + if (mainStore.appSettings?.isRunningOnHfSpaces) { |
| 113 | + await generateHFSpacesToken(); |
| 114 | + } else { |
| 115 | + needFetchWithHFAccessToken.value = false; |
| 116 | + giskardClientTemplate.value = generateGiskardClientInstruction(); |
| 117 | + } |
| 118 | +}); |
| 119 | +
|
| 120 | +</script> |
| 121 | + |
0 commit comments