add devstral and react updates
Browse filesFiles changed (5)
- Dockerfile+1
- backend_api.py+94-40
- backend_deploy.py+117
- backend_models.py+2-2
- backend_search_replace.pyAdded+214
58 unmodified lines5960616263646558 unmodified linesCOPY --chown=user:user backend_prompts.py .COPY --chown=user:user backend_parsers.py .COPY --chown=user:user backend_deploy.py .COPY --chown=user:user backend_search_replace.py .COPY --chown=user:user project_importer.py .COPY --chown=user:user app.py .
98 unmodified lines9910010110210210310410510693 unmodified lines200201202202203204205206636 unmodified lines8438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958458468478488498508968978988999009019029039049058 unmodified lines91491591686586686786886987087187287387487587687787887988088188288388488588691791891992092192288888989089192392492592692792892993093193293393493593693793893994094194294394494594689389489589689789894794894995095195295395495598 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 (Default)", "supports_images": True}, {"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}, {"name": "Gemini 3.0 Pro", "id": "gemini-3.0-pro", "description": "Google Gemini 3.0 Pro via Poe with advanced reasoning", "supports_images": False},93 unmodified linesclass CodeGenerationRequest(BaseModel): query: str language: str = "html" model_id: str = "zai-org/GLM-4.6V:zai-org" model_id: str = "devstral-medium-2512" provider: str = "auto" history: List[List[str]] = [] agent_mode: bool = False636 unmodified lines try: # Handle Mistral models with different API if is_mistral_model(selected_model_id): print(f"[Generate] Using Mistral SDK for {selected_model_id}") # devstral-medium-2512 uses the beta Conversations API if selected_model_id == "devstral-medium-2512": # Convert messages to inputs format for Conversations API # Extract system instruction from messages instructions = "" inputs = [] for msg in messages: if msg["role"] == "system": instructions = msg["content"] else: inputs.append({ "role": msg["role"], "content": msg["content"] }) # Use beta Conversations API response = 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 consistency full_response = str(response) generated_code = full_response # Yield in chunks to maintain consistency with streaming API chunk_size = 100 for 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 loop stream = None else: print("[Generate] Using Mistral SDK") stream = client.chat.stream( model=actual_model_id, messages=messages, max_tokens=10000 ) # Other Mistral models use the standard chat.stream API stream = client.chat.stream( model=actual_model_id, messages=messages, max_tokens=10000 ) # All other models use OpenAI-compatible API else:8 unmodified lines chunk_count = 0 is_mistral = is_mistral_model(selected_model_id) # Optimized chunk processing - reduce attribute lookups for chunk in stream: chunk_content = None if is_mistral: # Mistral format: chunk.data.choices[0].delta.content try: if chunk.data and chunk.data.choices and chunk.data.choices[0].delta.content: chunk_content = chunk.data.choices[0].delta.content except (AttributeError, IndexError): continue else: # OpenAI format: chunk.choices[0].delta.content try: if chunk.choices and chunk.choices[0].delta.content: chunk_content = chunk.choices[0].delta.content except (AttributeError, IndexError): continue if chunk_content: generated_code += chunk_content chunk_count += 1 # Only process stream if it exists (not None for Conversations API) if stream: # Optimized chunk processing - reduce attribute lookups for chunk in stream: chunk_content = None # Send chunk immediately - optimized JSON serialization # Only yield control every 5 chunks to reduce overhead if chunk_count % 5 == 0: await asyncio.sleep(0) if is_mistral: # Mistral format: chunk.data.choices[0].delta.content try: if chunk.data and chunk.data.choices and chunk.data.choices[0].delta.content: chunk_content = chunk.data.choices[0].delta.content except (AttributeError, IndexError): continue else: # OpenAI format: chunk.choices[0].delta.content try: if chunk.choices and chunk.choices[0].delta.content: chunk_content = chunk.choices[0].delta.content except (AttributeError, IndexError): continue if chunk_content: generated_code += chunk_content chunk_count += 1 # Send chunk immediately - optimized JSON serialization # Only yield control every 5 chunks to reduce overhead if chunk_count % 5 == 0: await asyncio.sleep(0) # Build event data efficiently event_data = json.dumps({ "type": "chunk", "content": chunk_content }) yield f"data: {event_data}\n\n" # Build event data efficiently event_data = json.dumps({ "type": "chunk", "content": chunk_content }) yield f"data: {event_data}\n\n" # Clean up generated code (remove LLM explanatory text and markdown) generated_code = cleanup_generated_code(generated_code, language)554 unmodified lines555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677554 unmodified lines print(f"[Deploy] language: {language}") print(f"[Deploy] ============================================") # For React space updates (followup changes), handle SEARCH/REPLACE blocks if is_update and language == "react": print(f"[Deploy] React space update - checking for search/replace blocks") # Import search/replace utilities from backend_search_replace import has_search_replace_blocks, parse_file_specific_changes, apply_search_replace_changes from huggingface_hub import hf_hub_download # Check if code contains search/replace blocks if has_search_replace_blocks(code): print(f"[Deploy] Detected SEARCH/REPLACE blocks - applying targeted changes") # Parse file-specific changes from code file_changes = parse_file_specific_changes(code) # Download existing files from the space try: print(f"[Deploy] Downloading existing files from space: {existing_repo_id}") # Get list of files in the space space_files = api.list_repo_files(repo_id=existing_repo_id, repo_type="space") print(f"[Deploy] Found {len(space_files)} files in space: {space_files}") # Download relevant files (React/Next.js files) react_file_patterns = ['.js', '.jsx', '.ts', '.tsx', '.css', '.json', 'Dockerfile'] existing_files = {} for file_path in space_files: # Skip non-code files if any(file_path.endswith(ext) or ext in file_path for ext in react_file_patterns): try: downloaded_path = hf_hub_download( repo_id=existing_repo_id, filename=file_path, repo_type="space", token=token ) with open(downloaded_path, 'r', encoding='utf-8') as f: existing_files[file_path] = f.read() print(f"[Deploy] Downloaded: {file_path} ({len(existing_files[file_path])} chars)") except Exception as e: print(f"[Deploy] Warning: Could not download {file_path}: {e}") if not existing_files: print(f"[Deploy] Warning: No React files found in space, falling back to full deployment") else: # Apply search/replace changes to the appropriate files updated_files = [] # Check if changes are file-specific or global if "__all__" in file_changes: # Global changes - try to apply to all files changes_text = file_changes["__all__"] print(f"[Deploy] Applying global search/replace changes") # Try to apply to each file for file_path, original_content in existing_files.items(): modified_content = apply_search_replace_changes(original_content, changes_text) if modified_content != original_content: print(f"[Deploy] Modified {file_path}") success, msg = update_space_file( repo_id=existing_repo_id, file_path=file_path, content=modified_content, token=token, commit_message=commit_message or f"Update {file_path} from anycoder" ) if success: updated_files.append(file_path) else: print(f"[Deploy] Warning: Failed to update {file_path}: {msg}") else: # File-specific changes for filename, changes_text in file_changes.items(): # Find the file in existing files (handle both with/without directory prefix) matching_file = None for file_path in existing_files.keys(): if file_path == filename or file_path.endswith('/' + filename): matching_file = file_path break if matching_file: original_content = existing_files[matching_file] modified_content = apply_search_replace_changes(original_content, changes_text) print(f"[Deploy] Applying changes to {matching_file}") success, msg = update_space_file( repo_id=existing_repo_id, file_path=matching_file, content=modified_content, token=token, commit_message=commit_message or f"Update {matching_file} from anycoder" ) if success: updated_files.append(matching_file) else: print(f"[Deploy] Warning: Failed to update {matching_file}: {msg}") else: print(f"[Deploy] Warning: File {filename} not found in space") if updated_files: space_url = f"https://huggingface.co/spaces/{existing_repo_id}" files_list = ", ".join(updated_files) return True, f"✅ Updated {len(updated_files)} file(s): {files_list}! View at: {space_url}", space_url else: return False, "No files were updated", None except Exception as e: print(f"[Deploy] Error applying search/replace changes: {e}") import traceback traceback.print_exc() # Fall through to normal deployment else: print(f"[Deploy] No SEARCH/REPLACE blocks detected, proceeding with full file update") # Fall through to normal React deployment below # For Gradio space updates (import/redesign), update .py files and upload all new files if is_update and language == "gradio": print(f"[Deploy] Gradio space update - updating .py files and uploading any new files")148 unmodified lines149150151152152153154155171 unmodified lines327328329330330331148 unmodified lines base_url="https://api.stepfun.com/v1" ) elif model_id == "codestral-2508" or model_id == "mistral-medium-2508": elif model_id == "codestral-2508" or model_id == "mistral-medium-2508" or model_id == "devstral-medium-2512": # Use Mistral client for Mistral models return Mistral(api_key=os.getenv("MISTRAL_API_KEY")) 171 unmodified lines
def is_mistral_model(model_id: str) -> bool: """Check if model uses Mistral SDK""" return model_id in ["codestral-2508", "mistral-medium-2508"] return model_id in ["codestral-2508", "mistral-medium-2508", "devstral-medium-2512"]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214"""Search/Replace utilities for applying targeted code changes.Extracted from anycoder_app/parsers.py for use in backend."""
# Search/Replace block markersSEARCH_START = "\u003c\u003c\u003c\u003c\u003c\u003c\u003c SEARCH"DIVIDER = "======="REPLACE_END = "\u003e\u003e\u003e\u003e\u003e\u003e\u003e REPLACE"
def apply_search_replace_changes(original_content: str, changes_text: str) -> str: """Apply search/replace changes to content (HTML, Python, JS, CSS, etc.) Args: original_content: The original file content to modify changes_text: Text containing SEARCH/REPLACE blocks Returns: Modified content with all search/replace blocks applied """ if not changes_text.strip(): return original_content # If the model didn't use the block markers, try a CSS-rule fallback where # provided blocks like `.selector { ... }` replace matching CSS rules. if (SEARCH_START not in changes_text) and (DIVIDER not in changes_text) and (REPLACE_END not in changes_text): try: import re # Local import to avoid global side effects updated_content = original_content replaced_any_rule = False # Find CSS-like rule blocks in the changes_text # This is a conservative matcher that looks for `selector { ... }` css_blocks = re.findall(r"([^{]+)\{([\s\S]*?)\}", changes_text, flags=re.MULTILINE) for selector_raw, body_raw in css_blocks: selector = selector_raw.strip() body = body_raw.strip() if not selector: continue # Build a regex to find the existing rule for this selector # Capture opening `{` and closing `}` to preserve them; replace inner body. pattern = re.compile(rf"({re.escape(selector)}\s*\{{)([\s\S]*?)(\}})") def _replace_rule(match): nonlocal replaced_any_rule replaced_any_rule = True prefix, existing_body, suffix = match.groups() # Preserve indentation of the existing first body line if present first_line_indent = "" for line in existing_body.splitlines(): stripped = line.lstrip(" \t") if stripped: first_line_indent = line[: len(line) - len(stripped)] break # Re-indent provided body with the detected indent if body: new_body_lines = [first_line_indent + line if line.strip() else line for line in body.splitlines()] new_body_text = "\n" + "\n".join(new_body_lines) + "\n" else: new_body_text = existing_body # If empty body provided, keep existing return f"{prefix}{new_body_text}{suffix}" updated_content, num_subs = pattern.subn(_replace_rule, updated_content, count=1) if replaced_any_rule: return updated_content except Exception: # Fallback silently to the standard block-based application pass
# Split the changes text into individual search/replace blocks blocks = [] current_block = "" lines = changes_text.split('\n') for line in lines: if line.strip() == SEARCH_START: if current_block.strip(): blocks.append(current_block.strip()) current_block = line + '\n' elif line.strip() == REPLACE_END: current_block += line + '\n' blocks.append(current_block.strip()) current_block = "" else: current_block += line + '\n' if current_block.strip(): blocks.append(current_block.strip()) modified_content = original_content for block in blocks: if not block.strip(): continue # Parse the search/replace block lines = block.split('\n') search_lines = [] replace_lines = [] in_search = False in_replace = False for line in lines: if line.strip() == SEARCH_START: in_search = True in_replace = False elif line.strip() == DIVIDER: in_search = False in_replace = True elif line.strip() == REPLACE_END: in_replace = False elif in_search: search_lines.append(line) elif in_replace: replace_lines.append(line) # Apply the search/replace if search_lines: search_text = '\n'.join(search_lines).strip() replace_text = '\n'.join(replace_lines).strip() if search_text in modified_content: modified_content = modified_content.replace(search_text, replace_text) else: # If exact block match fails, attempt a CSS-rule fallback using the replace_text try: import re updated_content = modified_content replaced_any_rule = False css_blocks = re.findall(r"([^{]+)\{([\s\S]*?)\}", replace_text, flags=re.MULTILINE) for selector_raw, body_raw in css_blocks: selector = selector_raw.strip() body = body_raw.strip() if not selector: continue pattern = re.compile(rf"({re.escape(selector)}\s*\{{)([\s\S]*?)(\}})") def _replace_rule(match): nonlocal replaced_any_rule replaced_any_rule = True prefix, existing_body, suffix = match.groups() first_line_indent = "" for line in existing_body.splitlines(): stripped = line.lstrip(" \t") if stripped: first_line_indent = line[: len(line) - len(stripped)] break if body: new_body_lines = [first_line_indent + line if line.strip() else line for line in body.splitlines()] new_body_text = "\n" + "\n".join(new_body_lines) + "\n" else: new_body_text = existing_body return f"{prefix}{new_body_text}{suffix}" updated_content, num_subs = pattern.subn(_replace_rule, updated_content, count=1) if replaced_any_rule: modified_content = updated_content else: print(f"[Search/Replace] Warning: Search text not found in content: {search_text[:100]}...") except Exception: print(f"[Search/Replace] Warning: Search text not found in content: {search_text[:100]}...") return modified_content
def has_search_replace_blocks(text: str) -> bool: """Check if text contains SEARCH/REPLACE block markers. Args: text: Text to check Returns: True if text contains search/replace markers, False otherwise """ return (SEARCH_START in text) and (DIVIDER in text) and (REPLACE_END in text)
def parse_file_specific_changes(changes_text: str) -> dict: """Parse changes that specify which files to modify. Looks for patterns like: === components/Header.jsx === \u003c\u003c\u003c\u003c\u003c\u003c\u003c SEARCH ... Returns: Dict mapping filename -> search/replace changes for that file """ import re file_changes = {} # Pattern to match file sections: === filename === file_pattern = re.compile(r"^===\s+([^\n=]+?)\s+===\s*$", re.MULTILINE) # Find all file sections matches = list(file_pattern.finditer(changes_text)) if not matches: # No file-specific sections, treat entire text as changes return {"__all__": changes_text} for i, match in enumerate(matches): filename = match.group(1).strip() start_pos = match.end() # Find the end of this file's section (start of next file or end of text) if i + 1 < len(matches): end_pos = matches[i + 1].start() else: end_pos = len(changes_text) file_content = changes_text[start_pos:end_pos].strip() if file_content: file_changes[filename] = file_content return file_changesDockerfile+1
backend_api.py+94-40
backend_deploy.py+117
backend_models.py+2-2
backend_search_replace.pyAdded+214