Spaces:
Running
Running
| /** | |
| * API Client für Kommunikation mit dem Backend | |
| * | |
| * Der Backend läuft auf Gradio (port 7860) | |
| * Bei HF Spaces ist kein Proxy nötig, da Gradio client kommuniziert | |
| */ | |
| // Versuche, den Client von Gradio zu laden | |
| let client = null | |
| async function initializeGradioClient() { | |
| try { | |
| // Bei HF Spaces: Gradio lädt seinen Client automatisch global | |
| if (window.gradio_client) { | |
| return window.gradio_client | |
| } | |
| // Als Fallback könnten wir den Client manuell laden | |
| // aber normalerweise ist das nicht nötig | |
| console.warn('Gradio client not available') | |
| return null | |
| } catch (err) { | |
| console.error('Error initializing Gradio client:', err) | |
| return null | |
| } | |
| } | |
| export async function sendMessage(prompt, systemPrompt, temperature, topP) { | |
| try { | |
| // Methode 1: Über Gradio API (empfohlen für HF Spaces) | |
| const response = await fetch('/api/call/generate_response', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ | |
| data: [prompt, systemPrompt, temperature, topP] | |
| }) | |
| }) | |
| if (!response.ok) { | |
| // Fallback: Direkter HTTP Request | |
| return await fallbackRequest(prompt, systemPrompt, temperature, topP) | |
| } | |
| const result = await response.json() | |
| // Parse Gradio response | |
| if (result.data && result.data[0]) { | |
| const data = result.data[0] | |
| return { | |
| response: typeof data === 'string' ? data : data.response || 'No response', | |
| tokens: data.tokens || 0, | |
| time_seconds: data.time_seconds || 0, | |
| } | |
| } | |
| return result | |
| } catch (err) { | |
| console.error('Error sending message:', err) | |
| throw err | |
| } | |
| } | |
| async function fallbackRequest(prompt, systemPrompt, temperature, topP) { | |
| // Fallback für lokale Entwicklung oder andere Szenarien | |
| const response = await fetch('http://localhost:7860/api/predict', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ | |
| data: [prompt, systemPrompt, temperature, topP] | |
| }) | |
| }) | |
| if (!response.ok) { | |
| throw new Error(`API request failed: ${response.statusText}`) | |
| } | |
| const result = await response.json() | |
| return result | |
| } | |
| export async function getStats() { | |
| try { | |
| const response = await fetch('/api/stats') | |
| if (!response.ok) { | |
| throw new Error('Failed to fetch stats') | |
| } | |
| return await response.json() | |
| } catch (err) { | |
| console.error('Error fetching stats:', err) | |
| return null | |
| } | |
| } | |
| export async function loadModel(modelName) { | |
| try { | |
| const response = await fetch('/api/load-model', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ model: modelName }) | |
| }) | |
| if (!response.ok) { | |
| throw new Error('Failed to load model') | |
| } | |
| return await response.json() | |
| } catch (err) { | |
| console.error('Error loading model:', err) | |
| throw err | |
| } | |
| } | |