File size: 16,364 Bytes
01c9829
 
 
 
57fadb3
01c9829
 
 
57fadb3
 
01c9829
 
 
 
 
 
 
57fadb3
01c9829
57fadb3
01c9829
57fadb3
01c9829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1a33c2
01c9829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57fadb3
 
01c9829
 
b1a33c2
01c9829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1a33c2
01c9829
 
 
 
b1a33c2
 
01c9829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1a33c2
01c9829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57fadb3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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)