File size: 8,680 Bytes
d3f86d8
 
 
 
 
 
 
 
 
4c355e8
 
 
d3f86d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c355e8
 
d3f86d8
 
 
 
 
 
 
 
4c355e8
 
d3f86d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c355e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3f86d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c355e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3f86d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<script>
  import '../app.css';
  import { Home, Settings, History, Github, Menu } from 'lucide-svelte';
  import { onMount } from 'svelte';
  
  let currentPage = 'tts';
  let sidebarOpen = true;
  let isLoggedIn = false;
  let username = '';
  let showTokenInput = false;
  let tokenInput = '';
  let tokenError = '';
  
  onMount(() => {
    // Check if user is logged in
    checkLoginStatus();
    
    // Re-check login status when page becomes visible (e.g., after OAuth redirect)
    document.addEventListener('visibilitychange', () => {
      if (!document.hidden) {
        checkLoginStatus();
      }
    });
    
    // Listen for storage changes (e.g., when token is set from OAuth callback)
    window.addEventListener('storage', checkLoginStatus);
    
    // Also check periodically to catch cases where localStorage changes in same tab
    const interval = setInterval(checkLoginStatus, 1000);
    
    return () => {
      window.removeEventListener('storage', checkLoginStatus);
      clearInterval(interval);
    };
  });
  
  function checkLoginStatus() {
    const token = localStorage.getItem('hf_access_token');
    if (token) {
      // Fetch user info from HuggingFace API
      fetchUserInfo(token);
    } else {
      isLoggedIn = false;
      username = '';
    }
  }
  
  async function fetchUserInfo(token) {
    try {
      // Validate token using the correct whoami-v2 endpoint
      const response = await fetch('https://huggingface.co/api/whoami-v2', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      });
      
      if (response.ok) {
        const userData = await response.json();
        isLoggedIn = true;
        const fullName = userData.name || userData.fullname || userData.login || userData.username || 'User';
        username = fullName.split(' ')[0];
      } else {
        // Token might be invalid, remove it
        localStorage.removeItem('hf_access_token');
        isLoggedIn = false;
        username = '';
      }
    } catch (error) {
      // Token might be invalid, remove it
      localStorage.removeItem('hf_access_token');
      isLoggedIn = false;
      username = '';
    }
  }
  
  function handleAuthAction() {
    if (isLoggedIn) {
      // Logout
      localStorage.removeItem('hf_access_token');
      sessionStorage.removeItem('oauth_state');
      isLoggedIn = false;
      username = '';
    } else {
      // Show token input instead of OAuth
      showTokenInput = true;
      tokenInput = '';
      tokenError = '';
    }
  }

  function closeTokenInput() {
    showTokenInput = false;
    tokenInput = '';
    tokenError = '';
  }

  async function submitToken() {
    if (!tokenInput.trim()) {
      tokenError = 'Please enter a token';
      return;
    }

    if (!tokenInput.startsWith('hf_')) {
      tokenError = 'Token should start with "hf_"';
      return;
    }

    try {
      // Validate token using the correct whoami-v2 endpoint
      const response = await fetch('https://huggingface.co/api/whoami-v2', {
        headers: {
          'Authorization': `Bearer ${tokenInput.trim()}`
        }
      });

      if (response.ok) {
        const userData = await response.json();
        localStorage.setItem('hf_access_token', tokenInput.trim());
        isLoggedIn = true;
        const fullName = userData.name || userData.fullname || userData.login || userData.username || 'User';
        username = fullName.split(' ')[0];
        closeTokenInput();
      } else {
        const errorText = await response.text();
        console.log('Token validation error:', response.status, errorText);
        tokenError = `Invalid token (${response.status}). Please check your token and try again.`;
      }
    } catch (error) {
      console.error('Token validation error:', error);
      tokenError = 'Error validating token. Please try again.';
    }
  }
</script>

<div class="flex h-screen bg-white">
  <!-- Sidebar -->
  <aside class="w-56 border-r border-gray-200 bg-white flex-shrink-0 {sidebarOpen ? '' : 'hidden'}">
    <div class="p-4 border-b border-gray-200">
      <div class="flex items-center gap-3">
        <img src="/assets/hf-studio-logo.png" alt="HF Logo" class="w-8 h-8" />
        <h1 class="text-xl font-semibold">HFStudio<sup class="text-xs text-gray-500 ml-1">BETA</sup></h1>
      </div>
    </div>
    
    <nav class="p-2 text-sm">      
      <div class="mt-2 mb-1 px-2 text-xs font-medium text-gray-500 uppercase">
        Tasks
      </div>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md hover:bg-gray-100 transition-colors text-left
               {currentPage === 'tts' ? 'bg-gray-100' : ''}"
        on:click={() => currentPage = 'tts'}
      >
        <span>πŸŽ™οΈ</span>
        <span>Text to Speech</span>
      </button>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left opacity-40 cursor-not-allowed"
        disabled
      >
        <span>🎡</span>
        <span>Voice Cloning</span>
      </button>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left opacity-40 cursor-not-allowed"
        disabled
      >
        <span>🎧</span>
        <span>Speech to Text</span>
      </button>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left opacity-40 cursor-not-allowed"
        disabled
      >
        <span>🎼</span>
        <span>Sound Effects</span>
      </button>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left opacity-40 cursor-not-allowed"
        disabled
      >
        <span>🎸</span>
        <span>Music Generation</span>
      </button>
      
      <button
        class="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left opacity-40 cursor-not-allowed"
        disabled
      >
        <span>πŸ”Š</span>
        <span>Audio Enhancement</span>
      </button>
      
    </nav>
    
    <!-- Sign in with Hugging Face at bottom -->
    <div class="absolute bottom-4 left-2 right-2 w-52">
      <button
        on:click={handleAuthAction}
        class="w-full px-6 py-3 bg-black text-white rounded-lg font-medium hover:bg-gray-800 transition-colors shadow-sm flex items-center justify-center gap-2 text-sm"
      >
        <img src="/assets/hf-logo.png" alt="HF Logo" class="w-5 h-5" />
        {#if isLoggedIn}
          <span>Sign Out, {username}</span>
        {:else}
          <span>Sign In</span>
        {/if}
      </button>
    </div>
  </aside>
  
  <!-- Main content -->
  <main class="flex-1 overflow-auto">
    <slot />
  </main>

  <!-- Token Input Modal -->
  {#if showTokenInput}
    <div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <div class="bg-white rounded-lg p-6 max-w-md w-full mx-4 shadow-xl">
        <h2 class="text-xl font-semibold mb-4">Sign In with HuggingFace Token</h2>
        
        <div class="mb-4 p-3 bg-blue-50 rounded-md text-sm">
          <p class="text-blue-800 mb-2">
            <strong>Temporary:</strong> OAuth is being fixed. For now, please use a HuggingFace token.
          </p>
          <p class="text-blue-700">
            1. Go to <a href="https://huggingface.co/settings/tokens" target="_blank" class="underline text-blue-600">HuggingFace Settings</a><br>
            2. Create a new token with "Inference API" permissions<br>
            3. Copy and paste it below
          </p>
        </div>

        <div class="mb-4">
          <label for="token" class="block text-sm font-medium text-gray-700 mb-2">
            HuggingFace Token
          </label>
          <input
            id="token"
            type="password"
            bind:value={tokenInput}
            placeholder="hf_..."
            class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500 focus:border-transparent"
            on:keydown={(e) => e.key === 'Enter' && submitToken()}
          />
          {#if tokenError}
            <p class="text-red-600 text-sm mt-1">{tokenError}</p>
          {/if}
        </div>

        <div class="flex justify-end gap-3">
          <button
            on:click={closeTokenInput}
            class="px-4 py-2 text-gray-600 hover:text-gray-800 transition-colors"
          >
            Cancel
          </button>
          <button
            on:click={submitToken}
            class="px-4 py-2 bg-orange-500 text-white rounded-md hover:bg-orange-600 transition-colors"
          >
            Sign In
          </button>
        </div>
      </div>
    </div>
  {/if}
</div>