Spaces:
Sleeping
Sleeping
Fix API endpoint: Add named 'chat' endpoint to Gradio ChatInterface and update frontend API calls
Browse files- app.py +1 -0
- frontend/src/lib/api.ts +14 -9
app.py
CHANGED
|
@@ -183,6 +183,7 @@ def create_enhanced_medical_interface():
|
|
| 183 |
],
|
| 184 |
cache_examples=False
|
| 185 |
)
|
|
|
|
| 186 |
|
| 187 |
# Footer with technical info
|
| 188 |
gr.Markdown("""
|
|
|
|
| 183 |
],
|
| 184 |
cache_examples=False
|
| 185 |
)
|
| 186 |
+
chatbot.api_name = "chat"
|
| 187 |
|
| 188 |
# Footer with technical info
|
| 189 |
gr.Markdown("""
|
frontend/src/lib/api.ts
CHANGED
|
@@ -20,20 +20,25 @@ export async function queryAPI(input: string, history: ChatMessage[] = []): Prom
|
|
| 20 |
throw new Error('HF_API_URL is not configured');
|
| 21 |
}
|
| 22 |
|
| 23 |
-
// Convert history to Gradio format
|
| 24 |
-
const gradioHistory
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
// Use Gradio API
|
| 30 |
-
const response = await fetch(`${HF_API_URL}/
|
| 31 |
method: 'POST',
|
| 32 |
headers: {
|
| 33 |
'Content-Type': 'application/json',
|
| 34 |
},
|
| 35 |
body: JSON.stringify({
|
| 36 |
-
data: [
|
|
|
|
|
|
|
|
|
|
| 37 |
}),
|
| 38 |
});
|
| 39 |
|
|
@@ -44,7 +49,7 @@ export async function queryAPI(input: string, history: ChatMessage[] = []): Prom
|
|
| 44 |
const result = await response.json();
|
| 45 |
|
| 46 |
return {
|
| 47 |
-
answer: result?.data?.[0] ||
|
| 48 |
sources: [], // Enhanced backend provides sources within the response text
|
| 49 |
};
|
| 50 |
} catch (error) {
|
|
|
|
| 20 |
throw new Error('HF_API_URL is not configured');
|
| 21 |
}
|
| 22 |
|
| 23 |
+
// Convert history to Gradio's chat format: a list of [user, assistant] pairs.
|
| 24 |
+
const gradioHistory: [string | null, string | null][] = [];
|
| 25 |
+
for (let i = 0; i < history.length; i += 2) {
|
| 26 |
+
if (i + 1 < history.length && history[i].role === 'user' && history[i+1].role === 'assistant') {
|
| 27 |
+
gradioHistory.push([history[i].content, history[i+1].content]);
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
|
| 31 |
+
// Use the specific Gradio API endpoint as per the documentation
|
| 32 |
+
const response = await fetch(`${HF_API_URL}/chat`, {
|
| 33 |
method: 'POST',
|
| 34 |
headers: {
|
| 35 |
'Content-Type': 'application/json',
|
| 36 |
},
|
| 37 |
body: JSON.stringify({
|
| 38 |
+
data: [
|
| 39 |
+
input,
|
| 40 |
+
gradioHistory
|
| 41 |
+
]
|
| 42 |
}),
|
| 43 |
});
|
| 44 |
|
|
|
|
| 49 |
const result = await response.json();
|
| 50 |
|
| 51 |
return {
|
| 52 |
+
answer: result?.data?.[0] || 'No response received from the medical assistant.',
|
| 53 |
sources: [], // Enhanced backend provides sources within the response text
|
| 54 |
};
|
| 55 |
} catch (error) {
|