Spaces:
Paused
Paused
Update backend_api.py
Browse files- backend_api.py +25 -19
backend_api.py
CHANGED
|
@@ -3,7 +3,7 @@ FastAPI backend for AnyCoder - provides REST API endpoints
|
|
| 3 |
"""
|
| 4 |
from fastapi import FastAPI, HTTPException, Header, WebSocket, WebSocketDisconnect, Request, Response
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
|
| 7 |
from fastapi.responses import StreamingResponse, RedirectResponse, JSONResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from typing import Optional, List, Dict, AsyncGenerator
|
|
@@ -97,8 +97,21 @@ def get_gemini_model():
|
|
| 97 |
|
| 98 |
# Define models and languages here to avoid importing Gradio UI
|
| 99 |
AVAILABLE_MODELS = [
|
| 100 |
-
{
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
]
|
| 103 |
|
| 104 |
# Cache model lookup for faster access (built after AVAILABLE_MODELS is defined)
|
|
@@ -738,29 +751,22 @@ async def generate_code(request: CodeGenerationRequest, authorization: Optional[
|
|
| 738 |
if not model_id.startswith("models/"):
|
| 739 |
model_id = f"models/{model_id}"
|
| 740 |
|
| 741 |
-
async def event_stream()
|
| 742 |
try:
|
| 743 |
-
#
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
# 3. Gọi API Gemini stream
|
| 751 |
-
response = model.generate_content(full_prompt, stream=True)
|
| 752 |
|
| 753 |
-
# 4. Stream phản hồi về frontend
|
| 754 |
for chunk in response:
|
| 755 |
if chunk.text:
|
| 756 |
yield f"data: {json.dumps({'type': 'chunk', 'content': chunk.text})}\n\n"
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
# Gửi tín hiệu hoàn tất
|
| 760 |
yield f"data: {json.dumps({'type': 'complete'})}\n\n"
|
| 761 |
-
|
| 762 |
except Exception as e:
|
| 763 |
-
# Gửi thông báo lỗi chi tiết để debug
|
| 764 |
yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
|
| 765 |
|
| 766 |
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
|
|
|
| 3 |
"""
|
| 4 |
from fastapi import FastAPI, HTTPException, Header, WebSocket, WebSocketDisconnect, Request, Response
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from google import genai
|
| 7 |
from fastapi.responses import StreamingResponse, RedirectResponse, JSONResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from typing import Optional, List, Dict, AsyncGenerator
|
|
|
|
| 97 |
|
| 98 |
# Define models and languages here to avoid importing Gradio UI
|
| 99 |
AVAILABLE_MODELS = [
|
| 100 |
+
{
|
| 101 |
+
"name": "Gemini 3.5 Flash",
|
| 102 |
+
"id": "gemini-3.5-flash",
|
| 103 |
+
"description": "Tốt nhất để viết code dài, tốc độ nhanh, hỗ trợ context lớn"
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"name": "Gemini 3.1 Pro Preview",
|
| 107 |
+
"id": "gemini-3.1-pro-preview",
|
| 108 |
+
"description": "Thông minh nhất, độ chính xác cao cho logic phức tạp"
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"name": "Gemini 2.0 Flash",
|
| 112 |
+
"id": "gemini-2.0-flash",
|
| 113 |
+
"description": "Cân bằng giữa hiệu năng và ổn định"
|
| 114 |
+
}
|
| 115 |
]
|
| 116 |
|
| 117 |
# Cache model lookup for faster access (built after AVAILABLE_MODELS is defined)
|
|
|
|
| 751 |
if not model_id.startswith("models/"):
|
| 752 |
model_id = f"models/{model_id}"
|
| 753 |
|
| 754 |
+
async def event_stream():
|
| 755 |
try:
|
| 756 |
+
# Dùng client mới để gọi model
|
| 757 |
+
# Lưu ý: Thư viện mới dùng model_id trực tiếp, không cần tiền tố 'models/' nếu gọi qua client
|
| 758 |
+
response = client.models.generate_content_stream(
|
| 759 |
+
model=request.model_id,
|
| 760 |
+
contents=full_prompt
|
| 761 |
+
)
|
|
|
|
|
|
|
|
|
|
| 762 |
|
|
|
|
| 763 |
for chunk in response:
|
| 764 |
if chunk.text:
|
| 765 |
yield f"data: {json.dumps({'type': 'chunk', 'content': chunk.text})}\n\n"
|
| 766 |
+
|
|
|
|
|
|
|
| 767 |
yield f"data: {json.dumps({'type': 'complete'})}\n\n"
|
| 768 |
+
|
| 769 |
except Exception as e:
|
|
|
|
| 770 |
yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
|
| 771 |
|
| 772 |
return StreamingResponse(event_stream(), media_type="text/event-stream")
|