Spaces:
Sleeping
Sleeping
Sgridda
commited on
Commit
·
2d836ae
1
Parent(s):
d1e0f9b
adde emergency file
Browse files- main_emergency.py +141 -0
main_emergency.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import time
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Emergency version - no AI, just mock responses
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
title="AI Code Review Service",
|
| 10 |
+
description="Emergency version - returns mock reviews quickly",
|
| 11 |
+
version="1.0.0",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
class DiffRequest(BaseModel):
|
| 15 |
+
diff: str
|
| 16 |
+
|
| 17 |
+
class ReviewComment(BaseModel):
|
| 18 |
+
file_path: str
|
| 19 |
+
line_number: int
|
| 20 |
+
comment_text: str
|
| 21 |
+
|
| 22 |
+
class ReviewResponse(BaseModel):
|
| 23 |
+
comments: list[ReviewComment]
|
| 24 |
+
|
| 25 |
+
# Mock reviews database
|
| 26 |
+
MOCK_REVIEWS = [
|
| 27 |
+
"Consider adding error handling for edge cases.",
|
| 28 |
+
"This function could benefit from better variable names.",
|
| 29 |
+
"Add docstrings to improve code documentation.",
|
| 30 |
+
"Consider breaking this into smaller functions.",
|
| 31 |
+
"Add input validation to prevent potential issues.",
|
| 32 |
+
"This logic could be simplified for better readability.",
|
| 33 |
+
"Consider adding unit tests for this functionality.",
|
| 34 |
+
"Potential performance improvement: use list comprehension.",
|
| 35 |
+
"Add logging for better debugging capabilities.",
|
| 36 |
+
"Consider using constants instead of magic numbers."
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Add a simple HTML response for browser viewing
|
| 40 |
+
@app.get("/", response_class=HTMLResponse)
|
| 41 |
+
def root_html():
|
| 42 |
+
"""Return HTML for browser viewing."""
|
| 43 |
+
return """
|
| 44 |
+
<!DOCTYPE html>
|
| 45 |
+
<html>
|
| 46 |
+
<head>
|
| 47 |
+
<title>AI Code Review Service</title>
|
| 48 |
+
<style>
|
| 49 |
+
body { font-family: Arial, sans-serif; margin: 40px; }
|
| 50 |
+
.status { color: green; font-weight: bold; }
|
| 51 |
+
.endpoint { background: #f4f4f4; padding: 10px; margin: 10px 0; border-radius: 5px; }
|
| 52 |
+
</style>
|
| 53 |
+
</head>
|
| 54 |
+
<body>
|
| 55 |
+
<h1>AI Code Review Service</h1>
|
| 56 |
+
<p class="status">✅ Service is running in emergency mode</p>
|
| 57 |
+
|
| 58 |
+
<h2>Available Endpoints:</h2>
|
| 59 |
+
<div class="endpoint"><strong>GET /health</strong> - Health check</div>
|
| 60 |
+
<div class="endpoint"><strong>POST /review</strong> - Submit code diff for review</div>
|
| 61 |
+
<div class="endpoint"><strong>GET /docs</strong> - Interactive API documentation</div>
|
| 62 |
+
<div class="endpoint"><strong>GET /test</strong> - Simple test endpoint</div>
|
| 63 |
+
|
| 64 |
+
<h2>Quick Test:</h2>
|
| 65 |
+
<p><a href="/health">Test Health Endpoint</a></p>
|
| 66 |
+
<p><a href="/docs">View API Documentation</a></p>
|
| 67 |
+
|
| 68 |
+
<h2>Status:</h2>
|
| 69 |
+
<ul>
|
| 70 |
+
<li>Mode: Emergency (Mock responses)</li>
|
| 71 |
+
<li>AI Model: Disabled</li>
|
| 72 |
+
<li>Response Time: ~100ms</li>
|
| 73 |
+
</ul>
|
| 74 |
+
</body>
|
| 75 |
+
</html>
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
@app.get("/info")
|
| 79 |
+
def root_json():
|
| 80 |
+
"""JSON endpoint to verify the service is running."""
|
| 81 |
+
return {
|
| 82 |
+
"message": "AI Code Review Service is running!",
|
| 83 |
+
"status": "healthy",
|
| 84 |
+
"version": "emergency-1.0.0",
|
| 85 |
+
"endpoints": ["/health", "/review", "/docs", "/test"],
|
| 86 |
+
"note": "This is a FastAPI service. Visit /docs for the interactive API documentation."
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
@app.get("/health")
|
| 90 |
+
def health_check():
|
| 91 |
+
"""Health check endpoint."""
|
| 92 |
+
return {
|
| 93 |
+
"status": "healthy",
|
| 94 |
+
"service": "AI Code Review Service (Emergency Mode)",
|
| 95 |
+
"model_loaded": False,
|
| 96 |
+
"mode": "mock_responses",
|
| 97 |
+
"uptime": "ready",
|
| 98 |
+
"memory_usage": "minimal"
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
@app.post("/review", response_model=ReviewResponse)
|
| 102 |
+
def review_diff(request: DiffRequest):
|
| 103 |
+
"""
|
| 104 |
+
Returns mock review comments instantly.
|
| 105 |
+
This ensures the service works while we debug the AI model.
|
| 106 |
+
"""
|
| 107 |
+
print(f"[INFO] Received diff for review (length: {len(request.diff)} chars)")
|
| 108 |
+
|
| 109 |
+
# Simulate a tiny bit of processing time
|
| 110 |
+
time.sleep(0.1)
|
| 111 |
+
|
| 112 |
+
# Generate 1-3 random mock comments
|
| 113 |
+
num_comments = random.randint(1, 3)
|
| 114 |
+
comments = []
|
| 115 |
+
|
| 116 |
+
for _ in range(num_comments):
|
| 117 |
+
comment = {
|
| 118 |
+
"file_path": "code_file.py",
|
| 119 |
+
"line_number": random.randint(1, 20),
|
| 120 |
+
"comment_text": random.choice(MOCK_REVIEWS)
|
| 121 |
+
}
|
| 122 |
+
comments.append(comment)
|
| 123 |
+
|
| 124 |
+
print(f"[INFO] Returning {len(comments)} mock review comments")
|
| 125 |
+
|
| 126 |
+
return ReviewResponse(comments=comments)
|
| 127 |
+
|
| 128 |
+
# Add a simple test endpoint
|
| 129 |
+
@app.get("/test")
|
| 130 |
+
def test_endpoint():
|
| 131 |
+
"""Simple test endpoint to verify the service."""
|
| 132 |
+
return {
|
| 133 |
+
"message": "Test successful!",
|
| 134 |
+
"timestamp": time.time(),
|
| 135 |
+
"service": "running"
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
import uvicorn
|
| 140 |
+
print("[INFO] Starting emergency FastAPI service...")
|
| 141 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|