GenerAI / app.py
amogaddy's picture
Sostituita ricerca web SearXNG con Chromium/Playwright; aggiunto Dockerfile per Chromium su HF Space
b1a33c2 verified
Raw
History Blame Contribute Delete
16.4 kB
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse, HTMLResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import asyncio
import json
from brain import Brain
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
brain: Brain | None = None
BASE = "https://amogaddy-generai.hf.space"
# ── Shared layout ──────────────────────────────────────────────────────────────
def page(title: str, content: str, active: str = "") -> str:
nav_items = [
("/", "🏠", "Home"),
("/chat", "πŸ’¬", "Chat"),
("/commands", "⚑", "Comandi"),
("/docs", "πŸ“–", "API Docs"),
]
nav_html = "".join(
f'<a href="{href}" class="nav-item{" active" if href == active else ""}">{icon} {label}</a>'
for href, icon, label in nav_items
)
return f"""<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>GenerAI β€” {title}</title>
<style>
*{{box-sizing:border-box;margin:0;padding:0}}
body{{font-family:'Segoe UI',sans-serif;background:#0f1117;color:#e0e0e0;min-height:100vh}}
nav{{background:#13151f;border-bottom:1px solid #2a2d3e;display:flex;align-items:center;padding:0 24px;height:52px;gap:4px}}
nav .logo{{color:#7c9ef8;font-weight:700;font-size:18px;margin-right:20px}}
.nav-item{{color:#aaa;text-decoration:none;padding:6px 14px;border-radius:6px;font-size:14px;transition:.15s}}
.nav-item:hover{{background:#2a2d3e;color:#e0e0e0}}
.nav-item.active{{background:#7c9ef8;color:#000;font-weight:600}}
.content{{padding:32px;max-width:900px;margin:0 auto}}
h1{{color:#7c9ef8;margin-bottom:8px}}
.sub{{color:#888;margin-bottom:24px;font-size:14px}}
.card{{background:#1e2130;border-radius:10px;padding:20px;margin-bottom:16px}}
.card h2{{color:#a0c4ff;font-size:15px;margin-bottom:12px;border-left:3px solid #7c9ef8;padding-left:10px}}
.badge{{display:inline-block;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600}}
.badge.green{{background:#1a3a2a;color:#3cb371;border:1px solid #3cb371}}
.badge.red{{background:#3a1a1a;color:#e05555;border:1px solid #e05555}}
.badge.blue{{background:#1a2a3a;color:#7c9ef8;border:1px solid #7c9ef8}}
.btn{{display:inline-block;padding:8px 18px;border-radius:7px;text-decoration:none;font-size:13px;cursor:pointer;border:none;font-family:inherit}}
.btn-primary{{background:#7c9ef8;color:#000;font-weight:600}}
.btn-secondary{{background:#2a2d3e;color:#e0e0e0;border:1px solid #444}}
.btn:hover{{opacity:.85}}
input,textarea{{background:#13151f;border:1px solid #2a2d3e;color:#e0e0e0;border-radius:6px;padding:10px 12px;font-family:monospace;font-size:13px;width:100%}}
input:focus,textarea:focus{{outline:none;border-color:#7c9ef8}}
.cmd-text{{font-family:monospace;font-size:13px;background:#13151f;padding:10px 12px;border-radius:6px;word-break:break-all;color:#c8f0a0}}
.tab{{font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;border:1px solid #444;background:#2a2d3e;color:#aaa}}
.tab.active{{background:#7c9ef8;color:#000;border-color:#7c9ef8}}
.copy-btn{{padding:5px 14px;background:#2a2d3e;border:1px solid #444;border-radius:5px;color:#aaa;cursor:pointer;font-size:12px;margin-top:8px}}
.copy-btn:hover{{background:#7c9ef8;color:#000}}
.copy-btn.copied{{background:#3cb371;color:#fff}}
table{{width:100%;border-collapse:collapse;font-size:13px}}
th{{color:#888;font-weight:500;padding:8px 12px;text-align:left;border-bottom:1px solid #2a2d3e}}
td{{padding:8px 12px;border-bottom:1px solid #1e2130}}
tr:hover td{{background:#1e2130}}
</style>
</head>
<body>
<nav>
<span class="logo">⚑ GenerAI</span>
{nav_html}
</nav>
<div class="content">
{content}
</div>
</body></html>"""
# ── Home ───────────────────────────────────────────────────────────────────────
@app.get("/", response_class=HTMLResponse)
async def home():
kb = brain.kb_size if brain else 0
status = "Online" if brain else "Avvio..."
badge = "green" if brain else "red"
content = f"""
<h1>GenerAI</h1>
<p class="sub">Assistente AI in italiano con memoria locale e ricerca web.</p>
<div class="card">
<h2>Stato sistema</h2>
<table>
<tr><th>Componente</th><th>Stato</th><th>Info</th></tr>
<tr><td>AI Brain</td><td><span class="badge {badge}">{status}</span></td><td>KB + Web search</td></tr>
<tr><td>Knowledge Base</td><td><span class="badge green">Online</span></td><td>{kb} documenti in memoria</td></tr>
<tr><td>Ricerca Web</td><td><span class="badge blue">Chromium</span></td><td>Browser headless (Playwright)</td></tr>
</table>
</div>
<div class="card">
<h2>Accesso rapido</h2>
<div style="display:flex;gap:10px;flex-wrap:wrap">
<a href="/chat" class="btn btn-primary">πŸ’¬ Chat</a>
<a href="/commands" class="btn btn-secondary">⚑ Comandi terminale</a>
<a href="/docs" class="btn btn-secondary">πŸ“– API Docs</a>
</div>
</div>
"""
return HTMLResponse(page("Home", content, "/"))
# ── Chat ───────────────────────────────────────────────────────────────────────
@app.get("/chat", response_class=HTMLResponse)
def chat_page():
content = f"""
<h1>πŸ’¬ Chat</h1>
<p class="sub">Fai una domanda alla AI β€” vedrai i passaggi in tempo reale.</p>
<div class="card">
<textarea id="inp" rows="3" placeholder="Scrivi una domanda..."></textarea>
<div style="display:flex;gap:10px;margin-top:10px">
<button class="btn btn-primary" onclick="ask()">Invia</button>
<button class="btn btn-secondary" onclick="clear_()">Pulisci</button>
</div>
</div>
<div class="card" id="out-card" style="display:none">
<h2>Risposta</h2>
<div id="steps" style="font-size:13px;color:#888;margin-bottom:12px"></div>
<div id="result" style="white-space:pre-wrap;line-height:1.6"></div>
<div id="status-badge" style="margin-top:10px"></div>
</div>
<script>
function ask() {{
const q = document.getElementById('inp').value.trim();
if (!q) return;
document.getElementById('out-card').style.display = 'block';
document.getElementById('steps').innerHTML = '';
document.getElementById('result').innerHTML = '<span style="color:#888">...</span>';
document.getElementById('status-badge').innerHTML = '';
fetch('{BASE}/ask/stream', {{
method: 'POST',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({{prompt: q}})
}}).then(r => {{
const reader = r.body.getReader();
const dec = new TextDecoder();
function read() {{
reader.read().then(({{{{'done': done, 'value': value}}}}) => {{
if (done) return;
dec.decode(value).split('\\n').forEach(line => {{
if (!line.startsWith('data:')) return;
const d = JSON.parse(line.slice(5));
if (d.type === 'status') {{
document.getElementById('steps').innerHTML += d.message + '<br>';
}} else if (d.type === 'result') {{
document.getElementById('result').innerText = d.message;
const colors = {{local:'#3cb371', searched:'#7c9ef8', llm:'#f0a000', unknown:'#e05555', error:'#e05555'}};
const c = colors[d.status] || '#aaa';
document.getElementById('status-badge').innerHTML =
`<span class="badge" style="background:#1a1a2a;color:${{c}};border:1px solid ${{c}}">${{d.status}}</span>`;
}}
}});
read();
}});
}}
read();
}});
}}
function clear_() {{
document.getElementById('inp').value = '';
document.getElementById('out-card').style.display = 'none';
}}
document.getElementById('inp').addEventListener('keydown', e => {{
if (e.key === 'Enter' && e.ctrlKey) ask();
}});
</script>
"""
return HTMLResponse(page("Chat", content, "/chat"))
# ── Comandi ────────────────────────────────────────────────────────────────────
@app.get("/commands", response_class=HTMLResponse)
def commands_page():
def cmd_block(bid, label, ps, cmd):
return f"""<div class="card" id="{bid}">
<div style="font-size:12px;color:#888;margin-bottom:8px">{label}</div>
<div style="display:flex;gap:6px;margin-bottom:8px">
<span class="tab active" id="{bid}-tab-ps" onclick="setTab('{bid}','ps')">PowerShell</span>
<span class="tab" id="{bid}-tab-cmd" onclick="setTab('{bid}','cmd')">CMD</span>
</div>
<div id="{bid}-ps" class="cmd-text">{ps}</div>
<div id="{bid}-cmd" class="cmd-text" style="display:none">{cmd}</div>
<div style="display:flex;gap:8px">
<button class="copy-btn" id="{bid}-btn-ps" onclick="copy('{bid}','ps')">Copia</button>
<button class="copy-btn" id="{bid}-btn-cmd" onclick="copy('{bid}','cmd')" style="display:none">Copia</button>
</div>
</div>"""
blocks = {
"Stato AI": [
cmd_block("b0","Controlla che la AI sia online",
f'Invoke-RestMethod "{BASE}/"',
f'curl "{BASE}/"'),
],
"Chat": [
cmd_block("b1","Domanda (risposta completa)",
f'Invoke-RestMethod -Uri "{BASE}/ask" -Method POST -ContentType "application/json" -Body \'&lbrace;"prompt":"chi e napoleone?"&rbrace;\' | ConvertTo-Json',
f'curl -s -X POST "{BASE}/ask" -H "Content-Type: application/json" -d "{{\\"prompt\\":\\"chi e napoleone?\\"}}"'),
cmd_block("b2","Domanda in streaming (tempo reale)",
f'curl -N -X POST "{BASE}/ask/stream" -H "Content-Type: application/json" -d "{{\\"prompt\\":\\"chi e napoleone?\\"}}"',
f'curl -N -X POST "{BASE}/ask/stream" -H "Content-Type: application/json" -d "{{\\"prompt\\":\\"chi e napoleone?\\"}}"'),
],
"Feedback": [
cmd_block("b6","Risposta utile",
f'Invoke-RestMethod -Uri "{BASE}/feedback?positive=true" -Method POST',
f'curl -X POST "{BASE}/feedback?positive=true"'),
cmd_block("b7","Risposta non utile",
f'Invoke-RestMethod -Uri "{BASE}/feedback?positive=false" -Method POST',
f'curl -X POST "{BASE}/feedback?positive=false"'),
],
}
sections_html = ""
for section, items in blocks.items():
icon = {"Stato AI":"🟒","Chat":"πŸ’¬","Feedback":"πŸ‘"}.get(section,"")
sections_html += f'<div style="margin-bottom:28px"><h2 style="color:#a0c4ff;font-size:15px;margin-bottom:12px;border-left:3px solid #7c9ef8;padding-left:10px">{icon} {section}</h2>{"".join(items)}</div>'
content = f"""
<h1>⚑ Comandi terminale</h1>
<p class="sub">Scegli PowerShell o CMD, copia il comando. Oppure scarica lo script completo.</p>
<div style="display:flex;gap:10px;margin-bottom:28px;flex-wrap:wrap">
<a href="/scripts/powershell" download="generai.ps1" class="btn btn-primary">⬇ generai.ps1</a>
<a href="/scripts/batch" download="generai.bat" class="btn btn-secondary">⬇ generai.bat</a>
</div>
{sections_html}
<script>
function setTab(id, tab) {{
['ps','cmd'].forEach(t => {{
document.getElementById(id+'-'+t).style.display = t===tab?'block':'none';
document.getElementById(id+'-btn-'+t).style.display = t===tab?'inline-block':'none';
document.getElementById(id+'-tab-'+t).classList.toggle('active', t===tab);
}});
}}
function copy(id, tab) {{
navigator.clipboard.writeText(document.getElementById(id+'-'+tab).innerText);
const btn = document.getElementById(id+'-btn-'+tab);
btn.innerText='Copiato!'; btn.classList.add('copied');
setTimeout(()=>{{btn.innerText='Copia';btn.classList.remove('copied')}}, 1500);
}}
</script>
"""
return HTMLResponse(page("Comandi", content, "/commands"))
# ── Script downloads ───────────────────────────────────────────────────────────
@app.get("/scripts/powershell")
def script_ps():
script = f"""# GenerAI β€” Script PowerShell | .\generai.ps1
$BASE = "{BASE}"
function Ask($d) {{
$r = Invoke-RestMethod -Uri "$BASE/ask" -Method POST -ContentType "application/json" -Body (@{{prompt=$d}}|ConvertTo-Json)
Write-Host "`n[$($r.status)]" -ForegroundColor Cyan; Write-Host $r.result
}}
function AskStream($d) {{
curl -s -N -X POST "$BASE/ask/stream" -H "Content-Type: application/json" -d (@{{prompt=$d}}|ConvertTo-Json -Compress)
}}
function Stato {{ Invoke-RestMethod "$BASE/" | ConvertTo-Json }}
function Feedback($p) {{
Invoke-RestMethod -Uri "$BASE/feedback?positive=$p" -Method POST | Out-Null
Write-Host "Feedback inviato!" -ForegroundColor Green
}}
while ($true) {{
Write-Host "`n===== GENERAI =====" -ForegroundColor Magenta
Write-Host "1. Domanda 2. Streaming 3. Stato 4/5. Feedback 0. Esci"
switch (Read-Host "Scelta") {{
"1" {{ Ask (Read-Host "Domanda") }}
"2" {{ AskStream (Read-Host "Domanda") }}
"3" {{ Stato }}
"4" {{ Feedback "true" }}
"5" {{ Feedback "false" }}
"0" {{ exit }}
default {{ Write-Host "Scelta non valida" -ForegroundColor Red }}
}}
}}
"""
return Response(content=script, media_type="text/plain",
headers={"Content-Disposition": "attachment; filename=generai.ps1"})
@app.get("/scripts/batch")
def script_bat():
script = f"""@echo off
chcp 65001 >nul
set BASE={BASE}
:menu
echo.
echo ===== GENERAI =====
echo 1.Domanda 2.Streaming 3.Stato 0.Esci
set /p s=Scelta:
if "%s%"=="1" (set /p d=Domanda: & curl -s -X POST "%BASE%/ask" -H "Content-Type: application/json" -d "{{\\"prompt\\":\\"%d%\\"}}" & echo.) & goto menu
if "%s%"=="2" (set /p d=Domanda: & curl -N -X POST "%BASE%/ask/stream" -H "Content-Type: application/json" -d "{{\\"prompt\\":\\"%d%\\"}}" & echo.) & goto menu
if "%s%"=="3" (curl -s "%BASE%/" & echo.) & goto menu
if "%s%"=="0" exit /b
goto menu
"""
return Response(content=script, media_type="text/plain",
headers={"Content-Disposition": "attachment; filename=generai.bat"})
# ── API endpoints ──────────────────────────────────────────────────────────────
class AskRequest(BaseModel):
prompt: str
@app.post("/ask")
async def ask(req: AskRequest):
if not brain:
raise HTTPException(status_code=503, detail="Brain non ancora pronto")
answer, status = await brain.ask(req.prompt)
return {"result": answer, "status": status}
@app.post("/ask/stream")
async def ask_stream(req: AskRequest):
if not brain:
raise HTTPException(status_code=503, detail="Brain non ancora pronto")
async def generate():
queue: asyncio.Queue = asyncio.Queue()
async def on_status(msg: str):
await queue.put(("status", msg))
async def run():
answer, status = await brain.ask(req.prompt, on_status=on_status)
await queue.put(("result", answer, status))
task = asyncio.create_task(run())
while True:
item = await queue.get()
if item[0] == "status":
yield f"data: {json.dumps({'type':'status','message':item[1]})}\n\n"
elif item[0] == "result":
yield f"data: {json.dumps({'type':'result','message':item[1],'status':item[2]})}\n\n"
break
await task
return StreamingResponse(generate(), media_type="text/event-stream")
@app.post("/feedback")
def feedback(positive: bool):
if brain:
brain.give_feedback(positive)
return {"ok": True}
@app.on_event("startup")
async def startup():
global brain
brain = Brain()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)