Spaces:
Running
Running
File size: 3,110 Bytes
ce72224 | 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 | /**
* 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
}
}
|