Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, flash, jsonify
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from huggingface_hub import login, whoami, HfApi
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os, json
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
app.secret_key = os.urandom(24)
|
| 10 |
+
|
| 11 |
+
# Global for running server mode
|
| 12 |
+
ee_model = None
|
| 13 |
+
ee_tokenizer = None
|
| 14 |
+
ee_config = None
|
| 15 |
+
|
| 16 |
+
@app.route("/", methods=["GET", "POST"])
|
| 17 |
+
def index():
|
| 18 |
+
global ee_model, ee_tokenizer, ee_config
|
| 19 |
+
if request.method == "POST":
|
| 20 |
+
action = request.form.get("action")
|
| 21 |
+
|
| 22 |
+
if action == "start_server":
|
| 23 |
+
ee_model_name = request.form["ee_model_name"].strip()
|
| 24 |
+
hf_token = request.form["hf_token"].strip()
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
login(token=hf_token)
|
| 28 |
+
global ee_model, ee_tokenizer, ee_config
|
| 29 |
+
|
| 30 |
+
ee_model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
ee_model_name,
|
| 32 |
+
torch_dtype=torch.float16,
|
| 33 |
+
#load_in_4bit=True,
|
| 34 |
+
device_map="auto",
|
| 35 |
+
trust_remote_code=True
|
| 36 |
+
)
|
| 37 |
+
ee_tokenizer = AutoTokenizer.from_pretrained(ee_model_name, trust_remote_code=True)
|
| 38 |
+
|
| 39 |
+
# Load config
|
| 40 |
+
from huggingface_hub import hf_hub_download
|
| 41 |
+
config_path = hf_hub_download(ee_model_name, "ee_config.json")
|
| 42 |
+
with open(config_path) as f:
|
| 43 |
+
ee_config = json.load(f)
|
| 44 |
+
|
| 45 |
+
flash(f"✅ Server ready! Model loaded: {ee_model_name}", "success")
|
| 46 |
+
flash("Now use the Client Space and point it to this Space's URL", "info")
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
flash(f"Error: {str(e)}", "danger")
|
| 50 |
+
|
| 51 |
+
return render_template("index.html")
|
| 52 |
+
|
| 53 |
+
# === INFERENCE ENDPOINT (always available when model is loaded) ===
|
| 54 |
+
@app.route("/generate", methods=["POST"])
|
| 55 |
+
def generate():
|
| 56 |
+
if ee_model is None:
|
| 57 |
+
return jsonify({"error": "Server not started yet"}), 400
|
| 58 |
+
|
| 59 |
+
data = request.json
|
| 60 |
+
encrypted_embeds = torch.tensor(data["encrypted_embeds"]).to(ee_model.device) # (1, seq, hidden)
|
| 61 |
+
attention_mask = torch.tensor(data.get("attention_mask", [[1]*encrypted_embeds.shape[1]])).to(ee_model.device)
|
| 62 |
+
max_new = int(data.get("max_new_tokens", 256))
|
| 63 |
+
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
output_ids = ee_model.generate(
|
| 66 |
+
inputs_embeds=encrypted_embeds,
|
| 67 |
+
attention_mask=attention_mask,
|
| 68 |
+
max_new_tokens=max_new,
|
| 69 |
+
do_sample=True,
|
| 70 |
+
temperature=0.7,
|
| 71 |
+
top_p=0.9,
|
| 72 |
+
pad_token_id=ee_tokenizer.eos_token_id
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
return jsonify({"generated_ids": output_ids[0].tolist()})
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
app.run(host="0.0.0.0", port=7860)
|