Spaces:
Runtime error
Runtime error
switch to openrouter
Browse filesFiles changed (2)
- backend_api.py+7-55
- backend_models.py+17-3
98 unmodified lines99100101102103104105739 unmodified lines84584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390498 unmodified lines# Define models and languages here to avoid importing Gradio UIAVAILABLE_MODELS = [{"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral Medium 2512 - Expert code generation model via Mistral Conversations API (Default)", "supports_images": False},{"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},{"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},{"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model for code generation", "supports_images": False},739 unmodified linesif is_mistral_model(selected_model_id):print(f"[Generate] Using Mistral SDK for {selected_model_id}")# devstral-medium-2512 uses the beta Conversations APIif selected_model_id == "devstral-medium-2512":# Convert messages to inputs format for Conversations API# Extract system instruction from messagesinstructions = ""inputs = []for msg in messages:if msg["role"] == "system":instructions = msg["content"]else:inputs.append({"role": msg["role"],"content": msg["content"]})# Use beta Conversations APIresponse = client.beta.conversations.start(inputs=inputs,model=actual_model_id,instructions=instructions,completion_args={"temperature": 0.7,"max_tokens": 10000,"top_p": 1},tools=[],)# For non-streaming response, yield the complete content# Note: Conversations API might not support streaming in the same way# We'll yield the complete response as chunks for consistencyfull_response = str(response)generated_code = full_response# Yield in chunks to maintain consistency with streaming APIchunk_size = 100for i in range(0, len(full_response), chunk_size):chunk_content = full_response[i:i+chunk_size]event_data = json.dumps({"type": "chunk","content": chunk_content})yield f"data: {event_data}\\n\\n"await asyncio.sleep(0)# Skip the normal streaming loopstream = Noneelse:# Other Mistral models use the standard chat.stream APIstream = client.chat.stream(model=actual_model_id,messages=messages,max_tokens=10000)# All other models use OpenAI-compatible API98 unmodified lines99100101102103104105739 unmodified lines84584684784884985085185285385485585698 unmodified lines# Define models and languages here to avoid importing Gradio UIAVAILABLE_MODELS = [{"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral 2512 - Expert code generation model via OpenRouter (Default)", "supports_images": False},{"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},{"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},{"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model for code generation", "supports_images": False},739 unmodified linesif is_mistral_model(selected_model_id):print(f"[Generate] Using Mistral SDK for {selected_model_id}")# Mistral models use the standard chat.stream APIstream = client.chat.stream(model=actual_model_id,messages=messages,max_tokens=10000)# All other models use OpenAI-compatible API
148 unmodified lines14915015115215315415590 unmodified lines24624724824925025125274 unmodified lines327328329330331148 unmodified linesbase_url="https://api.stepfun.com/v1")elif model_id == "codestral-2508" or model_id == "mistral-medium-2508" or model_id == "devstral-medium-2512":# Use Mistral client for Mistral modelsreturn Mistral(api_key=os.getenv("MISTRAL_API_KEY"))90 unmodified linesdef get_real_model_id(model_id: str) -> str:"""Get the real model ID with provider suffixes if needed"""if model_id == "stealth-model-1":# Get the real model ID from environment variablereal_model_id = os.getenv("STEALTH_MODEL_1_ID")if not real_model_id:74 unmodified linesdef is_mistral_model(model_id: str) -> bool:"""Check if model uses Mistral SDK"""return model_id in ["codestral-2508", "mistral-medium-2508", "devstral-medium-2512"]148 unmodified lines14915015115215315415515615715815916016116216316416516690 unmodified lines25725825926026126226326426526674 unmodified lines341342343344345148 unmodified linesbase_url="https://api.stepfun.com/v1")elif model_id == "devstral-medium-2512":# Use OpenRouter for Devstralreturn OpenAI(base_url="https://openrouter.ai/api/v1",api_key=os.getenv("OPENROUTER_API_KEY"),default_headers={"HTTP-Referer": "https://anycoder.app","X-Title": "AnyCoder",})elif model_id == "codestral-2508" or model_id == "mistral-medium-2508":# Use Mistral client for Mistral modelsreturn Mistral(api_key=os.getenv("MISTRAL_API_KEY"))90 unmodified linesdef get_real_model_id(model_id: str) -> str:"""Get the real model ID with provider suffixes if needed"""if model_id == "devstral-medium-2512":return "mistralai/devstral-2512:free"elif model_id == "stealth-model-1":# Get the real model ID from environment variablereal_model_id = os.getenv("STEALTH_MODEL_1_ID")if not real_model_id:74 unmodified linesdef is_mistral_model(model_id: str) -> bool:"""Check if model uses Mistral SDK"""return model_id in ["codestral-2508", "mistral-medium-2508"]
backend_api.py+7-55
backend_models.py+17-3