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")))
|
||||
58
frontend/ui.py
Normal file
58
frontend/ui.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
import streamlit as st
|
||||
|
||||
st.set_page_config(page_title="DevOps AI Assistant", page_icon="🚀")
|
||||
|
||||
st.title("🤖 AI CI/CD Pipeline Generator")
|
||||
st.markdown("Прототип сервиса для генерации и валидации конфигураций")
|
||||
|
||||
|
||||
def _env(name: str, default: str) -> str:
|
||||
value = os.getenv(name)
|
||||
return value if value else default
|
||||
|
||||
|
||||
default_backend_url = _env("BACKEND_URL", "http://backend:8000/generate")
|
||||
|
||||
with st.sidebar:
|
||||
st.header("Настройки")
|
||||
api_url = st.text_input("Backend URL", default_backend_url)
|
||||
model = st.selectbox(
|
||||
"LLM Модель",
|
||||
["deepseek-r1:1.5b", "qwen2.5-coder:1.5b", "llama3.1:8b", "Arena Model"],
|
||||
)
|
||||
platform = st.radio("Платформа", ["GitHub Actions", "GitLab CI"])
|
||||
|
||||
user_input = st.text_area(
|
||||
"Опишите пайплайн (например: 'Python app with pytest and docker build')", height=150
|
||||
)
|
||||
|
||||
if st.button("Сгенерировать конфигурацию", type="primary"):
|
||||
if user_input:
|
||||
with st.spinner("LLM думает..."):
|
||||
try:
|
||||
res = requests.post(
|
||||
api_url,
|
||||
json={"description": user_input, "platform": platform, "model": model},
|
||||
timeout=90,
|
||||
)
|
||||
res.raise_for_status()
|
||||
data = res.json()
|
||||
|
||||
if data.get("status") == "ok":
|
||||
st.success("✅ Конфигурация валидна!")
|
||||
st.code(data.get("config", ""), language="yaml")
|
||||
st.download_button(
|
||||
"Скачать .yml",
|
||||
data.get("config", ""),
|
||||
file_name="pipeline.yml",
|
||||
)
|
||||
else:
|
||||
st.error(f"❌ Ошибка валидации: {data.get('validation_error')}")
|
||||
st.code(data.get("config", ""), language="yaml")
|
||||
except Exception as e:
|
||||
st.error(f"Ошибка подключения к бекенду: {e}")
|
||||
else:
|
||||
st.warning("Пожалуйста, введите описание.")
|
||||
Reference in New Issue
Block a user