Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
-
from transformers import
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
MODEL_ID = "Qwen/Qwen2.5-Coder-1.0B-Instruct"
|
| 9 |
|
| 10 |
-
print("Loading tokenizer...")
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
model
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
token=hf_token
|
| 18 |
)
|
| 19 |
-
print("
|
| 20 |
|
| 21 |
app = Flask(__name__)
|
| 22 |
|
|
@@ -25,17 +26,23 @@ def generate():
|
|
| 25 |
data = request.json
|
| 26 |
prompt = data.get("prompt", "")
|
| 27 |
max_tokens = int(data.get("max_tokens", 256))
|
|
|
|
| 28 |
if not prompt:
|
| 29 |
return jsonify({"error": "Prompt required"}), 400
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
@app.route("/", methods=["GET"])
|
| 37 |
def health():
|
| 38 |
return jsonify({"status": "ok", "model": MODEL_ID})
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
+
from transformers import pipeline
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Use the secret you set in Hugging Face Space settings
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
MODEL_ID = "Qwen/Qwen2.5-Coder-1.0B-Instruct"
|
| 10 |
|
| 11 |
+
print("Loading pipeline (model + tokenizer)...")
|
| 12 |
+
# The pipeline automatically downloads everything needed
|
| 13 |
+
generator = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model=MODEL_ID,
|
| 16 |
+
device_map="cpu", # Force CPU for free-tier Spaces
|
| 17 |
+
torch_dtype="auto",
|
| 18 |
+
token=hf_token
|
| 19 |
)
|
| 20 |
+
print("Pipeline loaded successfully")
|
| 21 |
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
|
|
|
| 26 |
data = request.json
|
| 27 |
prompt = data.get("prompt", "")
|
| 28 |
max_tokens = int(data.get("max_tokens", 256))
|
| 29 |
+
|
| 30 |
if not prompt:
|
| 31 |
return jsonify({"error": "Prompt required"}), 400
|
| 32 |
|
| 33 |
+
# Pipeline handles tokenization, generation, and decoding automatically
|
| 34 |
+
result = generator(
|
| 35 |
+
prompt,
|
| 36 |
+
max_new_tokens=max_tokens,
|
| 37 |
+
truncation=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return jsonify({"response": result[0]['generated_text']})
|
| 41 |
|
| 42 |
@app.route("/", methods=["GET"])
|
| 43 |
def health():
|
| 44 |
return jsonify({"status": "ok", "model": MODEL_ID})
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
# Hugging Face Spaces require port 7860
|
| 48 |
+
app.run(host="0.0.0.0", port=7860)
|