Spaces:
Running
Running
add new gemini model
Browse files- backend_api.py +25 -4
- backend_models.py +8 -2
- frontend/src/components/LandingPage.tsx +1 -1
backend_api.py
CHANGED
|
@@ -99,10 +99,11 @@ def get_cached_client(model_id: str, provider: str = "auto"):
|
|
| 99 |
|
| 100 |
# Define models and languages here to avoid importing Gradio UI
|
| 101 |
AVAILABLE_MODELS = [
|
| 102 |
-
{"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral 2512 - Expert code generation model via OpenRouter
|
| 103 |
{"name": "GLM-4.6V ๐๏ธ", "id": "zai-org/GLM-4.6V:zai-org", "description": "GLM-4.6V vision model - supports image uploads for visual understanding", "supports_images": True},
|
| 104 |
{"name": "DeepSeek V3.2", "id": "deepseek-ai/DeepSeek-V3.2-Exp", "description": "DeepSeek V3.2 Experimental - Fast model for code generation via HuggingFace Router with Novita provider", "supports_images": False},
|
| 105 |
{"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model for code generation", "supports_images": False},
|
|
|
|
| 106 |
{"name": "Gemini 3.0 Pro", "id": "gemini-3.0-pro", "description": "Google Gemini 3.0 Pro via Poe with advanced reasoning", "supports_images": False},
|
| 107 |
{"name": "Grok 4.1 Fast", "id": "x-ai/grok-4.1-fast", "description": "Grok 4.1 Fast model via OpenRouter (20 req/min on free tier)", "supports_images": False},
|
| 108 |
{"name": "MiniMax M2", "id": "MiniMaxAI/MiniMax-M2", "description": "MiniMax M2 model via HuggingFace InferenceClient with Novita provider", "supports_images": False},
|
|
@@ -200,7 +201,7 @@ async def startup_event():
|
|
| 200 |
class CodeGenerationRequest(BaseModel):
|
| 201 |
query: str
|
| 202 |
language: str = "html"
|
| 203 |
-
model_id: str = "
|
| 204 |
provider: str = "auto"
|
| 205 |
history: List[List[str]] = []
|
| 206 |
agent_mode: bool = False
|
|
@@ -851,7 +852,20 @@ async def generate_code(
|
|
| 851 |
messages=messages,
|
| 852 |
max_tokens=10000
|
| 853 |
)
|
| 854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
|
| 856 |
# All other models use OpenAI-compatible API
|
| 857 |
else:
|
|
@@ -865,6 +879,7 @@ async def generate_code(
|
|
| 865 |
|
| 866 |
chunk_count = 0
|
| 867 |
is_mistral = is_mistral_model(selected_model_id)
|
|
|
|
| 868 |
|
| 869 |
# Only process stream if it exists (not None for Conversations API)
|
| 870 |
if stream:
|
|
@@ -872,7 +887,13 @@ async def generate_code(
|
|
| 872 |
for chunk in stream:
|
| 873 |
chunk_content = None
|
| 874 |
|
| 875 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 876 |
# Mistral format: chunk.data.choices[0].delta.content
|
| 877 |
try:
|
| 878 |
if chunk.data and chunk.data.choices and chunk.data.choices[0].delta.content:
|
|
|
|
| 99 |
|
| 100 |
# Define models and languages here to avoid importing Gradio UI
|
| 101 |
AVAILABLE_MODELS = [
|
| 102 |
+
{"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral 2512 - Expert code generation model via OpenRouter", "supports_images": False},
|
| 103 |
{"name": "GLM-4.6V ๐๏ธ", "id": "zai-org/GLM-4.6V:zai-org", "description": "GLM-4.6V vision model - supports image uploads for visual understanding", "supports_images": True},
|
| 104 |
{"name": "DeepSeek V3.2", "id": "deepseek-ai/DeepSeek-V3.2-Exp", "description": "DeepSeek V3.2 Experimental - Fast model for code generation via HuggingFace Router with Novita provider", "supports_images": False},
|
| 105 |
{"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model for code generation", "supports_images": False},
|
| 106 |
+
{"name": "Gemini 3.0 Flash Preview", "id": "gemini-3-flash-preview", "description": "Google Gemini 3.0 Flash Preview with Thinking Mode (High) (Default)", "supports_images": False},
|
| 107 |
{"name": "Gemini 3.0 Pro", "id": "gemini-3.0-pro", "description": "Google Gemini 3.0 Pro via Poe with advanced reasoning", "supports_images": False},
|
| 108 |
{"name": "Grok 4.1 Fast", "id": "x-ai/grok-4.1-fast", "description": "Grok 4.1 Fast model via OpenRouter (20 req/min on free tier)", "supports_images": False},
|
| 109 |
{"name": "MiniMax M2", "id": "MiniMaxAI/MiniMax-M2", "description": "MiniMax M2 model via HuggingFace InferenceClient with Novita provider", "supports_images": False},
|
|
|
|
| 201 |
class CodeGenerationRequest(BaseModel):
|
| 202 |
query: str
|
| 203 |
language: str = "html"
|
| 204 |
+
model_id: str = "gemini-3-flash-preview"
|
| 205 |
provider: str = "auto"
|
| 206 |
history: List[List[str]] = []
|
| 207 |
agent_mode: bool = False
|
|
|
|
| 852 |
messages=messages,
|
| 853 |
max_tokens=10000
|
| 854 |
)
|
| 855 |
+
|
| 856 |
+
# Handle Native SDK models (Gemini 3)
|
| 857 |
+
elif is_native_sdk_model(selected_model_id):
|
| 858 |
+
print(f"[Generate] Using Native SDK (Gemini) for {selected_model_id}")
|
| 859 |
+
|
| 860 |
+
if selected_model_id == "gemini-3-flash-preview":
|
| 861 |
+
contents, config = create_gemini3_messages(messages)
|
| 862 |
+
stream = client.models.generate_content_stream(
|
| 863 |
+
model=selected_model_id,
|
| 864 |
+
contents=contents,
|
| 865 |
+
config=config
|
| 866 |
+
)
|
| 867 |
+
else:
|
| 868 |
+
raise ValueError(f"Unknown native SDK model: {selected_model_id}")
|
| 869 |
|
| 870 |
# All other models use OpenAI-compatible API
|
| 871 |
else:
|
|
|
|
| 879 |
|
| 880 |
chunk_count = 0
|
| 881 |
is_mistral = is_mistral_model(selected_model_id)
|
| 882 |
+
is_native = is_native_sdk_model(selected_model_id)
|
| 883 |
|
| 884 |
# Only process stream if it exists (not None for Conversations API)
|
| 885 |
if stream:
|
|
|
|
| 887 |
for chunk in stream:
|
| 888 |
chunk_content = None
|
| 889 |
|
| 890 |
+
if is_native:
|
| 891 |
+
# Native SDK format (Gemini)
|
| 892 |
+
try:
|
| 893 |
+
chunk_content = chunk.text
|
| 894 |
+
except (AttributeError, ValueError):
|
| 895 |
+
continue
|
| 896 |
+
elif is_mistral:
|
| 897 |
# Mistral format: chunk.data.choices[0].delta.content
|
| 898 |
try:
|
| 899 |
if chunk.data and chunk.data.choices and chunk.data.choices[0].delta.content:
|
backend_models.py
CHANGED
|
@@ -247,6 +247,12 @@ def get_inference_client(model_id: str, provider: str = "auto"):
|
|
| 247 |
base_url=base_url,
|
| 248 |
)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
else:
|
| 251 |
# Unknown model - try HuggingFace Inference API
|
| 252 |
return OpenAI(
|
|
@@ -326,7 +332,7 @@ def create_gemini3_messages(messages: list) -> tuple:
|
|
| 326 |
# Configure tools and thinking
|
| 327 |
tools = [types.Tool(googleSearch=types.GoogleSearch())]
|
| 328 |
config = types.GenerateContentConfig(
|
| 329 |
-
|
| 330 |
tools=tools,
|
| 331 |
max_output_tokens=16384
|
| 332 |
)
|
|
@@ -336,7 +342,7 @@ def create_gemini3_messages(messages: list) -> tuple:
|
|
| 336 |
|
| 337 |
def is_native_sdk_model(model_id: str) -> bool:
|
| 338 |
"""Check if model uses native SDK (not OpenAI-compatible)"""
|
| 339 |
-
return
|
| 340 |
|
| 341 |
|
| 342 |
def is_mistral_model(model_id: str) -> bool:
|
|
|
|
| 247 |
base_url=base_url,
|
| 248 |
)
|
| 249 |
|
| 250 |
+
elif model_id == "gemini-3-flash-preview":
|
| 251 |
+
# Use native Google GenAI client for Gemini 3.0 Flash Preview
|
| 252 |
+
if not GEMINI_AVAILABLE:
|
| 253 |
+
raise ImportError("google-genai package required for Gemini 3")
|
| 254 |
+
return genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
|
| 255 |
+
|
| 256 |
else:
|
| 257 |
# Unknown model - try HuggingFace Inference API
|
| 258 |
return OpenAI(
|
|
|
|
| 332 |
# Configure tools and thinking
|
| 333 |
tools = [types.Tool(googleSearch=types.GoogleSearch())]
|
| 334 |
config = types.GenerateContentConfig(
|
| 335 |
+
thinking_config=types.ThinkingConfig(thinking_level="HIGH"),
|
| 336 |
tools=tools,
|
| 337 |
max_output_tokens=16384
|
| 338 |
)
|
|
|
|
| 342 |
|
| 343 |
def is_native_sdk_model(model_id: str) -> bool:
|
| 344 |
"""Check if model uses native SDK (not OpenAI-compatible)"""
|
| 345 |
+
return model_id == "gemini-3-flash-preview"
|
| 346 |
|
| 347 |
|
| 348 |
def is_mistral_model(model_id: str) -> bool:
|
frontend/src/components/LandingPage.tsx
CHANGED
|
@@ -31,7 +31,7 @@ export default function LandingPage({
|
|
| 31 |
onImport,
|
| 32 |
isAuthenticated,
|
| 33 |
initialLanguage = 'html',
|
| 34 |
-
initialModel = '
|
| 35 |
onAuthChange,
|
| 36 |
setPendingPR,
|
| 37 |
pendingPRRef
|
|
|
|
| 31 |
onImport,
|
| 32 |
isAuthenticated,
|
| 33 |
initialLanguage = 'html',
|
| 34 |
+
initialModel = 'gemini-3-flash-preview',
|
| 35 |
onAuthChange,
|
| 36 |
setPendingPR,
|
| 37 |
pendingPRRef
|