Spaces:
Paused
Paused
Update app/main.py
Browse files- app/main.py +34 -19
app/main.py
CHANGED
|
@@ -1,44 +1,47 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
from app.predictor import classifier, guide_generator
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Data
|
| 12 |
class FileRequest(BaseModel):
|
| 13 |
fileName: str
|
| 14 |
-
content: str
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
class GuideRequest(BaseModel):
|
| 18 |
repoName: str
|
| 19 |
-
filePaths:
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
def health_check():
|
| 24 |
-
"""
|
| 25 |
-
Simple check to see if the server is alive and which GPU it's using.
|
| 26 |
-
"""
|
| 27 |
return {
|
| 28 |
"status": "online",
|
| 29 |
"model": "microsoft/codebert-base",
|
| 30 |
"device": classifier.device,
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# first FAST_API with endpoint('/classify') called in [visualization.services.ts]
|
| 35 |
-
# @param {*} file
|
| 36 |
-
# @return {*} layerd based classified_info along with file-name
|
| 37 |
@app.post("/classify")
|
| 38 |
async def classify_file(request: FileRequest):
|
|
|
|
| 39 |
try:
|
| 40 |
-
# calling the predict function of our classifier to determine which layer it belongs
|
| 41 |
-
# returns { label, confidence, embedding }
|
| 42 |
result = classifier.predict(request.fileName, request.content)
|
| 43 |
return {
|
| 44 |
"fileName": request.fileName,
|
|
@@ -47,20 +50,32 @@ async def classify_file(request: FileRequest):
|
|
| 47 |
"embedding": result["embedding"]
|
| 48 |
}
|
| 49 |
except Exception as e:
|
|
|
|
| 50 |
raise HTTPException(status_code=500, detail=str(e))
|
| 51 |
|
| 52 |
-
|
| 53 |
@app.post("/generate-guide")
|
| 54 |
async def generate_guide(request: GuideRequest):
|
|
|
|
| 55 |
try:
|
| 56 |
markdown = guide_generator.generate_markdown(request.repoName, request.filePaths)
|
| 57 |
return {"markdown": markdown}
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
import uvicorn
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
# main.py - Final Fixed Version
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from typing import List, Optional
|
| 5 |
+
import logging
|
| 6 |
|
| 7 |
+
from app.services.reviewer_service import AIReviewerService
|
| 8 |
from app.predictor import classifier, guide_generator
|
| 9 |
|
| 10 |
+
# 1. Setup Logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# 2. Initialize FastAPI and Services
|
| 15 |
+
app = FastAPI(title="GitGud AI Service")
|
| 16 |
+
reviewer_service = AIReviewerService()
|
| 17 |
|
| 18 |
+
# 3. Data Models (Order matters: ReviewRequest needs FileRequest)
|
| 19 |
class FileRequest(BaseModel):
|
| 20 |
fileName: str
|
| 21 |
+
content: Optional[str] = None
|
| 22 |
|
| 23 |
+
class ReviewRequest(BaseModel):
|
| 24 |
+
files: List[FileRequest]
|
| 25 |
|
| 26 |
class GuideRequest(BaseModel):
|
| 27 |
repoName: str
|
| 28 |
+
filePaths: List[str]
|
| 29 |
|
| 30 |
+
# 4. Endpoints
|
| 31 |
|
| 32 |
@app.get("/")
|
| 33 |
def health_check():
|
| 34 |
+
"""Checks server status and GPU availability."""
|
|
|
|
|
|
|
| 35 |
return {
|
| 36 |
"status": "online",
|
| 37 |
"model": "microsoft/codebert-base",
|
| 38 |
"device": classifier.device,
|
| 39 |
}
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
@app.post("/classify")
|
| 42 |
async def classify_file(request: FileRequest):
|
| 43 |
+
"""Classifies file into architectural layers."""
|
| 44 |
try:
|
|
|
|
|
|
|
| 45 |
result = classifier.predict(request.fileName, request.content)
|
| 46 |
return {
|
| 47 |
"fileName": request.fileName,
|
|
|
|
| 50 |
"embedding": result["embedding"]
|
| 51 |
}
|
| 52 |
except Exception as e:
|
| 53 |
+
logger.error(f"Classify failed: {e}")
|
| 54 |
raise HTTPException(status_code=500, detail=str(e))
|
| 55 |
|
|
|
|
| 56 |
@app.post("/generate-guide")
|
| 57 |
async def generate_guide(request: GuideRequest):
|
| 58 |
+
"""Generates markdown guides for repositories."""
|
| 59 |
try:
|
| 60 |
markdown = guide_generator.generate_markdown(request.repoName, request.filePaths)
|
| 61 |
return {"markdown": markdown}
|
| 62 |
except Exception as e:
|
| 63 |
+
logger.error(f"Guide generation failed: {e}")
|
| 64 |
raise HTTPException(status_code=500, detail=str(e))
|
| 65 |
|
| 66 |
+
@app.post("/review")
|
| 67 |
+
async def review_code(request: ReviewRequest):
|
| 68 |
+
"""Detects security and logic issues in batches of files."""
|
| 69 |
+
try:
|
| 70 |
+
# Call the batch review logic from your service
|
| 71 |
+
results = reviewer_service.review_batch_code(request.files)
|
| 72 |
+
return {"reviews": results}
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logger.error(f"Review endpoint failed: {e}")
|
| 75 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 76 |
|
| 77 |
+
# 5. Application Entry Point
|
| 78 |
if __name__ == "__main__":
|
| 79 |
import uvicorn
|
| 80 |
+
# Port 7860 is mandatory for Hugging Face Spaces
|
| 81 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|