Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,75 @@
|
|
| 1 |
-
# Website: https://madgamesfmai.gamer.gd
|
| 2 |
-
import os
|
| 3 |
import torch
|
| 4 |
-
import json
|
| 5 |
-
import uuid
|
| 6 |
-
import base64
|
| 7 |
-
import io
|
| 8 |
-
from PIL import Image
|
| 9 |
-
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 10 |
-
from fastapi.responses import HTMLResponse, JSONResponse
|
| 11 |
-
from fastapi.staticfiles import StaticFiles
|
| 12 |
-
from fastapi.templating import Jinja2Templates
|
| 13 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 14 |
from model import MiniTransformer, BPETokenizer
|
| 15 |
-
from
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
app = FastAPI()
|
| 18 |
-
templates = Jinja2Templates(directory="templates")
|
| 19 |
-
|
| 20 |
-
print("Lade Vision-Modell (Augen)...")
|
| 21 |
-
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 22 |
-
vision_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to("cpu")
|
| 23 |
-
|
| 24 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
REPO_ID = "Kleinpuki2/madgamesai"
|
| 26 |
FILENAME = "madgames_gpt2_stable.pth"
|
|
|
|
|
|
|
| 27 |
tokenizer = BPETokenizer()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
print("Lade
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
except Exception as e:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
default_keys = {"MG-ADMIN-1337": {"created": "permanent", "uses": 0}}
|
| 44 |
-
if not os.path.exists(KEYS_FILE): return default_keys
|
| 45 |
-
with open(KEYS_FILE, "r") as f:
|
| 46 |
-
try:
|
| 47 |
-
keys = json.load(f)
|
| 48 |
-
keys.update(default_keys)
|
| 49 |
-
return keys
|
| 50 |
-
except: return default_keys
|
| 51 |
-
|
| 52 |
-
def save_keys(keys):
|
| 53 |
-
with open(KEYS_FILE, "w") as f: json.dump(keys, f)
|
| 54 |
-
|
| 55 |
-
def verify_api_key(request: Request):
|
| 56 |
-
api_key = request.headers.get("X-API-Key")
|
| 57 |
-
keys = load_keys()
|
| 58 |
-
if api_key not in keys:
|
| 59 |
-
raise HTTPException(status_code=403, detail="Ungültiger API-Key")
|
| 60 |
-
return api_key
|
| 61 |
-
|
| 62 |
-
from pydantic import BaseModel
|
| 63 |
-
from typing import Optional
|
| 64 |
-
|
| 65 |
-
class PredictRequest(BaseModel):
|
| 66 |
-
prompt: str
|
| 67 |
-
image: Optional[str] = None
|
| 68 |
-
|
| 69 |
-
@app.get("/", response_class=HTMLResponse)
|
| 70 |
-
async def dashboard(request: Request):
|
| 71 |
-
return templates.TemplateResponse(request=request, name="index.html")
|
| 72 |
-
|
| 73 |
-
@app.post("/generate_key")
|
| 74 |
-
async def create_key():
|
| 75 |
-
new_key = f"MG-{uuid.uuid4().hex[:12].upper()}"
|
| 76 |
-
keys = load_keys()
|
| 77 |
-
keys[new_key] = {"created": "manual", "uses": 0}
|
| 78 |
-
save_keys(keys)
|
| 79 |
-
return {"key": new_key}
|
| 80 |
-
|
| 81 |
@app.post("/predict")
|
| 82 |
-
async def predict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
inputs = processor(img, return_tensors="pt").to("cpu")
|
| 92 |
-
out_vision = vision_model.generate(**inputs)
|
| 93 |
-
description = processor.decode(out_vision[0], skip_special_tokens=True)
|
| 94 |
-
prompt = f"[Nutzer sendet Bild: {description}] " + prompt
|
| 95 |
-
print(f"Vision-Ergebnis: {description}")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
print(f"Vision-Fehler: {e}")
|
| 98 |
-
|
| 99 |
-
if not model:
|
| 100 |
-
return {"response": "Fehler: Modell wurde nicht korrekt geladen."}
|
| 101 |
-
|
| 102 |
-
tokens = tokenizer.encode(prompt)
|
| 103 |
-
input_ids = torch.tensor([tokens]).to(DEVICE)
|
| 104 |
-
|
| 105 |
ctx_len = model.ctx_len if hasattr(model, 'ctx_len') else 1024
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
out = model.generate(input_ids, max_new_tokens=50, temperature=0.2, top_k=10, repetition_penalty=1.0)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
keys = load_keys()
|
| 122 |
-
if api_key in keys:
|
| 123 |
-
keys[api_key]["uses"] += 1
|
| 124 |
-
save_keys(keys)
|
| 125 |
-
except: pass
|
| 126 |
-
|
| 127 |
-
return {"response": response_text}
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
-
print(f"
|
| 131 |
-
return
|
| 132 |
-
|
| 133 |
if __name__ == "__main__":
|
| 134 |
-
import uvicorn
|
| 135 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from model import MiniTransformer, BPETokenizer
|
| 3 |
+
from fastapi import FastAPI, Request, Header, HTTPException
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import uvicorn
|
| 6 |
+
import os
|
| 7 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
REPO_ID = "Kleinpuki2/madgamesai"
|
| 9 |
FILENAME = "madgames_gpt2_stable.pth"
|
| 10 |
+
API_KEY = "MG-ADMIN-1337"
|
| 11 |
+
model = None
|
| 12 |
tokenizer = BPETokenizer()
|
| 13 |
+
def load_model():
|
| 14 |
+
global model
|
| 15 |
+
try:
|
| 16 |
+
print(f"Lade Checkpoint von {REPO_ID}...")
|
| 17 |
+
path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 18 |
+
model = MiniTransformer.load(path, device='cpu')
|
| 19 |
+
print("Modell erfolgreich geladen und initialisiert!")
|
| 20 |
+
return True
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Fehler beim Laden: {e}")
|
| 23 |
+
return False
|
| 24 |
+
is_loaded = load_model()
|
| 25 |
+
@app.get("/")
|
| 26 |
+
def root():
|
| 27 |
+
return {"status": "online", "loaded": is_loaded, "repo": REPO_ID}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@app.post("/predict")
|
| 29 |
+
async def predict(request: Request, x_api_key: str = Header(None)):
|
| 30 |
+
if x_api_key != API_KEY:
|
| 31 |
+
raise HTTPException(status_code=401)
|
| 32 |
+
|
| 33 |
+
if not is_loaded or model is None:
|
| 34 |
+
return {"response": "Fehler: Modell konnte nicht korrekt initialisiert werden."}
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
+
data = await request.json()
|
| 38 |
+
prompt = data.get("prompt", "")
|
| 39 |
+
if not prompt: return {"response": ""}
|
| 40 |
|
| 41 |
+
print(f"Anfrage empfangen: {prompt[:50]}...")
|
| 42 |
+
|
| 43 |
+
# WICHTIG: Das exakte Format aus dem Training nachbauen!
|
| 44 |
+
formatted_prompt = f"User: {prompt}\nKI: "
|
| 45 |
+
tokens = tokenizer.encode(formatted_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
ctx_len = model.ctx_len if hasattr(model, 'ctx_len') else 1024
|
| 47 |
+
tokens = tokens[-ctx_len:]
|
| 48 |
|
| 49 |
+
idx = torch.tensor([tokens], dtype=torch.long)
|
|
|
|
| 50 |
|
| 51 |
+
# Perfekte Settings für Code & Chat
|
| 52 |
+
out = model.generate(idx, max_new_tokens=250, temperature=0.2, top_k=10, repetition_penalty=1.0)
|
| 53 |
|
| 54 |
+
# Nur den neu generierten Teil extrahieren
|
| 55 |
+
generated_tokens = out[0, len(tokens):].tolist()
|
| 56 |
+
response = tokenizer.decode(generated_tokens)
|
| 57 |
+
|
| 58 |
+
# <|endoftext|> entfernen, falls es im Text auftaucht
|
| 59 |
+
response = response.replace("<|endoftext|>", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Prüfen, ob die KI anfängt, den nächsten "User:" Text zu generieren
|
| 62 |
+
if "User:" in response:
|
| 63 |
+
response = response.split("User:")[0]
|
| 64 |
+
|
| 65 |
+
final_text = response.strip()
|
| 66 |
+
if not final_text:
|
| 67 |
+
final_text = "Die KI hat noch keine klare Antwort gefunden. Trainiere sie noch ein wenig weiter (Ziel: Loss unter 1.0) oder versuche einen anderen Prompt!"
|
| 68 |
+
|
| 69 |
+
print(f"Antwort generiert: {final_text[:50]}...")
|
| 70 |
+
return {"response": final_text}
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"Fehler bei Vorhersage: {e}")
|
| 73 |
+
return {"response": f"Runtime Fehler: {str(e)}"}
|
|
|
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 75 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|