Delete app.py
Browse files
app.py
DELETED
|
@@ -1,192 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
from fastapi import FastAPI, Request
|
| 4 |
-
from fastapi.responses import JSONResponse, HTMLResponse
|
| 5 |
-
from fastapi.templating import Jinja2Templates
|
| 6 |
-
|
| 7 |
-
# =========================
|
| 8 |
-
# APP INIT
|
| 9 |
-
# =========================
|
| 10 |
-
|
| 11 |
-
app = FastAPI(title="PyRunner Agent OS", version="3.0")
|
| 12 |
-
|
| 13 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
-
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# =========================
|
| 18 |
-
# ENV
|
| 19 |
-
# =========================
|
| 20 |
-
|
| 21 |
-
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
|
| 22 |
-
|
| 23 |
-
MODELS = {
|
| 24 |
-
"orchestrator": os.getenv("OLLAMA_MODEL", "qwen2.5:1.5b"),
|
| 25 |
-
"coder": os.getenv("OLLAMA_CODE_MODEL", "qwen2.5-coder:1.5b"),
|
| 26 |
-
"fast": os.getenv("OLLAMA_FAST_MODEL", "deepseek-coder:1.3b"),
|
| 27 |
-
"fallback": os.getenv("OLLAMA_BACKUP_MODEL", "llama3.2:1b")
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# =========================
|
| 32 |
-
# OLLAMA CORE CALL
|
| 33 |
-
# =========================
|
| 34 |
-
|
| 35 |
-
def call_ollama(prompt, model):
|
| 36 |
-
try:
|
| 37 |
-
r = requests.post(
|
| 38 |
-
f"{OLLAMA_URL}/api/generate",
|
| 39 |
-
json={
|
| 40 |
-
"model": model,
|
| 41 |
-
"prompt": prompt,
|
| 42 |
-
"stream": False
|
| 43 |
-
},
|
| 44 |
-
timeout=60
|
| 45 |
-
)
|
| 46 |
-
return r.json()
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return {"error": str(e)}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# =========================
|
| 52 |
-
# AGENT DEFINITIONS
|
| 53 |
-
# =========================
|
| 54 |
-
|
| 55 |
-
def orchestrator_agent(user_input: str):
|
| 56 |
-
"""
|
| 57 |
-
Classifies task type
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
prompt = f"""
|
| 61 |
-
You are an orchestrator AI.
|
| 62 |
-
|
| 63 |
-
Classify this request into one category ONLY:
|
| 64 |
-
|
| 65 |
-
- code
|
| 66 |
-
- vision
|
| 67 |
-
- fashion
|
| 68 |
-
- hairstyle
|
| 69 |
-
- general
|
| 70 |
-
|
| 71 |
-
User request:
|
| 72 |
-
{user_input}
|
| 73 |
-
|
| 74 |
-
Return ONLY the category name.
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
-
res = call_ollama(prompt, MODELS["orchestrator"])
|
| 78 |
-
|
| 79 |
-
if "response" in res:
|
| 80 |
-
return res["response"].strip().lower()
|
| 81 |
-
|
| 82 |
-
return "general"
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def coder_agent(prompt):
|
| 86 |
-
return call_ollama(prompt, MODELS["coder"])
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
def fashion_agent(prompt):
|
| 90 |
-
system_prompt = f"""
|
| 91 |
-
You are a fashion expert AI.
|
| 92 |
-
Analyze outfit, styling, colors, trends.
|
| 93 |
-
|
| 94 |
-
User:
|
| 95 |
-
{prompt}
|
| 96 |
-
"""
|
| 97 |
-
return call_ollama(system_prompt, MODELS["fast"])
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def hairstyle_agent(prompt):
|
| 101 |
-
system_prompt = f"""
|
| 102 |
-
You are a barber & hairstyle expert.
|
| 103 |
-
Describe hairstyle changes, fades, textures, transformations.
|
| 104 |
-
|
| 105 |
-
User:
|
| 106 |
-
{prompt}
|
| 107 |
-
"""
|
| 108 |
-
return call_ollama(system_prompt, MODELS["fast"])
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def fallback_agent(prompt):
|
| 112 |
-
return call_ollama(prompt, MODELS["fallback"])
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# =========================
|
| 116 |
-
# ROUTER
|
| 117 |
-
# =========================
|
| 118 |
-
|
| 119 |
-
def route_agent(category: str, prompt: str):
|
| 120 |
-
|
| 121 |
-
if category == "code":
|
| 122 |
-
return coder_agent(prompt)
|
| 123 |
-
|
| 124 |
-
if category == "fashion":
|
| 125 |
-
return fashion_agent(prompt)
|
| 126 |
-
|
| 127 |
-
if category == "hairstyle":
|
| 128 |
-
return hairstyle_agent(prompt)
|
| 129 |
-
|
| 130 |
-
return fallback_agent(prompt)
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# =========================
|
| 134 |
-
# UI
|
| 135 |
-
# =========================
|
| 136 |
-
|
| 137 |
-
@app.get("/", response_class=HTMLResponse)
|
| 138 |
-
async def home(request: Request):
|
| 139 |
-
return templates.TemplateResponse("index.html", {"request": request})
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
# =========================
|
| 143 |
-
# HEALTH
|
| 144 |
-
# =========================
|
| 145 |
-
|
| 146 |
-
@app.get("/health")
|
| 147 |
-
def health():
|
| 148 |
-
return {
|
| 149 |
-
"status": "ok",
|
| 150 |
-
"agents": list(MODELS.keys())
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
# =========================
|
| 155 |
-
# MAIN AGENT ENDPOINT
|
| 156 |
-
# =========================
|
| 157 |
-
|
| 158 |
-
@app.post("/api/agent")
|
| 159 |
-
async def agent(request: Request):
|
| 160 |
-
|
| 161 |
-
body = await request.json()
|
| 162 |
-
user_input = body.get("prompt", "")
|
| 163 |
-
|
| 164 |
-
# 1. Decide agent
|
| 165 |
-
category = orchestrator_agent(user_input)
|
| 166 |
-
|
| 167 |
-
# 2. Route to agent
|
| 168 |
-
result = route_agent(category, user_input)
|
| 169 |
-
|
| 170 |
-
# 3. Fallback safety
|
| 171 |
-
if "error" in result:
|
| 172 |
-
result = fallback_agent(user_input)
|
| 173 |
-
|
| 174 |
-
return JSONResponse({
|
| 175 |
-
"category": category,
|
| 176 |
-
"result": result
|
| 177 |
-
})
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
# =========================
|
| 181 |
-
# IMAGE PIPELINE HOOK (future)
|
| 182 |
-
# =========================
|
| 183 |
-
|
| 184 |
-
@app.post("/api/image")
|
| 185 |
-
async def image_pipeline(request: Request):
|
| 186 |
-
body = await request.json()
|
| 187 |
-
|
| 188 |
-
return {
|
| 189 |
-
"status": "queued",
|
| 190 |
-
"pipeline": "vision_stub",
|
| 191 |
-
"note": "Attach diffusion model later"
|
| 192 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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