Refactor: improve run.sh startup logic and ensure proper permissions
This commit is contained in:
70
backend/main.py
Normal file
70
backend/main.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI(title="AI CI/CD Assistant")
|
||||
|
||||
|
||||
def _env(name: str, default: str) -> str:
|
||||
value = os.getenv(name)
|
||||
return value if value else default
|
||||
|
||||
|
||||
# Example for local Ollama: http://localhost:11434/api/generate
|
||||
OLLAMA_API_URL = _env("OLLAMA_API_URL", "http://host.docker.internal:11434/api/generate")
|
||||
|
||||
|
||||
class GenerationRequest(BaseModel):
|
||||
description: str
|
||||
platform: str = "GitHub Actions" # or GitLab CI
|
||||
model: str = "codellama"
|
||||
|
||||
|
||||
def validate_config(config_text: str):
|
||||
try:
|
||||
yaml.safe_load(config_text)
|
||||
return True, "Success"
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.post("/generate")
|
||||
async def generate_pipeline(req: GenerationRequest):
|
||||
prompt = (
|
||||
f"Write only a valid {req.platform} YAML configuration for: {req.description}. "
|
||||
"No explanations, just code."
|
||||
)
|
||||
|
||||
payload = {"model": req.model, "prompt": prompt, "stream": False}
|
||||
|
||||
try:
|
||||
response = requests.post(OLLAMA_API_URL, json=payload, timeout=60)
|
||||
response.raise_for_status()
|
||||
raw_text = response.json().get("response", "")
|
||||
|
||||
clean_code = raw_text.replace("```yaml", "").replace("```", "").strip()
|
||||
is_valid, error_msg = validate_config(clean_code)
|
||||
|
||||
return {
|
||||
"status": "ok" if is_valid else "error",
|
||||
"config": clean_code,
|
||||
"validation_error": error_msg if not is_valid else None,
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Ollama error: {str(e)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=int(_env("PORT", "8000")))
|
||||
Reference in New Issue
Block a user