Spaces:
Paused
Paused
Create app/main.py
Browse files- app/main.py +66 -0
app/main.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# entry point aka building FAST_API here
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
from app.predictor import classifier, guide_generator
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="GitGud AI Service")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Data Model: Matches what NestJS (server-side[refer to visualization.services.ts]) sends
|
| 12 |
+
class FileRequest(BaseModel):
|
| 13 |
+
fileName: str
|
| 14 |
+
content: str | None = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class GuideRequest(BaseModel):
|
| 18 |
+
repoName: str
|
| 19 |
+
filePaths: list[str]
|
| 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,
|
| 45 |
+
"layer": result["label"],
|
| 46 |
+
"confidence": result["confidence"],
|
| 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 |
+
# Runs on localhost:8000
|
| 66 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|