Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.responses import HTMLResponse | |
| from pydantic import BaseModel | |
| import time | |
| import random | |
| # Emergency version - no AI, just mock responses | |
| app = FastAPI( | |
| title="AI Code Review Service", | |
| description="Emergency version - returns mock reviews quickly", | |
| version="1.0.0", | |
| ) | |
| class DiffRequest(BaseModel): | |
| diff: str | |
| class ReviewComment(BaseModel): | |
| file_path: str | |
| line_number: int | |
| comment_text: str | |
| class ReviewResponse(BaseModel): | |
| comments: list[ReviewComment] | |
| # Mock reviews database | |
| MOCK_REVIEWS = [ | |
| "Consider adding error handling for edge cases.", | |
| "This function could benefit from better variable names.", | |
| "Add docstrings to improve code documentation.", | |
| "Consider breaking this into smaller functions.", | |
| "Add input validation to prevent potential issues.", | |
| "This logic could be simplified for better readability.", | |
| "Consider adding unit tests for this functionality.", | |
| "Potential performance improvement: use list comprehension.", | |
| "Add logging for better debugging capabilities.", | |
| "Consider using constants instead of magic numbers." | |
| ] | |
| # Add a simple HTML response for browser viewing | |
| def root_html(): | |
| """Return HTML for browser viewing.""" | |
| return """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>AI Code Review Service</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; margin: 40px; } | |
| .status { color: green; font-weight: bold; } | |
| .endpoint { background: #f4f4f4; padding: 10px; margin: 10px 0; border-radius: 5px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>AI Code Review Service</h1> | |
| <p class="status">✅ Service is running in emergency mode</p> | |
| <h2>Available Endpoints:</h2> | |
| <div class="endpoint"><strong>GET /health</strong> - Health check</div> | |
| <div class="endpoint"><strong>POST /review</strong> - Submit code diff for review</div> | |
| <div class="endpoint"><strong>GET /docs</strong> - Interactive API documentation</div> | |
| <div class="endpoint"><strong>GET /test</strong> - Simple test endpoint</div> | |
| <h2>Quick Test:</h2> | |
| <p><a href="/health">Test Health Endpoint</a></p> | |
| <p><a href="/docs">View API Documentation</a></p> | |
| <h2>Status:</h2> | |
| <ul> | |
| <li>Mode: Emergency (Mock responses)</li> | |
| <li>AI Model: Disabled</li> | |
| <li>Response Time: ~100ms</li> | |
| </ul> | |
| </body> | |
| </html> | |
| """ | |
| def root_json(): | |
| """JSON endpoint to verify the service is running.""" | |
| return { | |
| "message": "AI Code Review Service is running!", | |
| "status": "healthy", | |
| "version": "emergency-1.0.0", | |
| "endpoints": ["/health", "/review", "/docs", "/test"], | |
| "note": "This is a FastAPI service. Visit /docs for the interactive API documentation." | |
| } | |
| def health_check(): | |
| """Health check endpoint.""" | |
| return { | |
| "status": "healthy", | |
| "service": "AI Code Review Service (Emergency Mode)", | |
| "model_loaded": False, | |
| "mode": "mock_responses", | |
| "uptime": "ready", | |
| "memory_usage": "minimal" | |
| } | |
| def review_diff(request: DiffRequest): | |
| """ | |
| Returns mock review comments instantly. | |
| This ensures the service works while we debug the AI model. | |
| """ | |
| print(f"[INFO] Received diff for review (length: {len(request.diff)} chars)") | |
| # Simulate a tiny bit of processing time | |
| time.sleep(0.1) | |
| # Generate 1-3 random mock comments | |
| num_comments = random.randint(1, 3) | |
| comments = [] | |
| for _ in range(num_comments): | |
| comment = { | |
| "file_path": "code_file.py", | |
| "line_number": random.randint(1, 20), | |
| "comment_text": random.choice(MOCK_REVIEWS) | |
| } | |
| comments.append(comment) | |
| print(f"[INFO] Returning {len(comments)} mock review comments") | |
| return ReviewResponse(comments=comments) | |
| # Add a simple test endpoint | |
| def test_endpoint(): | |
| """Simple test endpoint to verify the service.""" | |
| return { | |
| "message": "Test successful!", | |
| "timestamp": time.time(), | |
| "service": "running" | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| print("[INFO] Starting emergency FastAPI service...") | |
| uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info") | |