Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,19 @@ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
from huggingface_hub import login
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 10 |
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 11 |
os.environ['HF_HOME'] = '/tmp/cache'
|
| 12 |
|
| 13 |
-
# Charger le modèle et le tokenizer (en utilisant cerebras BTLM-3B-8K)
|
| 14 |
model_name = "cerebras/btlm-3b-8k-chat"
|
| 15 |
-
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
model_name,
|
|
@@ -21,40 +23,63 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 21 |
trust_remote_code=True
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Créer un pipeline de génération de texte avec le pad_token_id requis pour ce modèle
|
| 25 |
generator = pipeline(
|
| 26 |
"text-generation",
|
| 27 |
model=model,
|
| 28 |
tokenizer=tokenizer,
|
| 29 |
device_map="auto",
|
| 30 |
torch_dtype=torch_dtype,
|
| 31 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 32 |
trust_remote_code=True
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
prompt = ""
|
| 42 |
-
for msg in
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
|
|
|
| 46 |
output = generator(
|
| 47 |
prompt,
|
| 48 |
-
max_new_tokens=
|
| 49 |
-
temperature=
|
| 50 |
top_p=0.9,
|
| 51 |
repetition_penalty=1.1,
|
| 52 |
do_sample=True
|
| 53 |
)
|
| 54 |
-
reply = output[0][
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
from huggingface_hub import login
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
|
| 7 |
+
# Login to Hugging Face using secret token stored in Space secrets
|
| 8 |
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
| 9 |
|
| 10 |
+
# Token authentication for requests
|
| 11 |
+
API_TOKEN = os.getenv("HF_API_TOKEN") # You set this in Space secrets
|
| 12 |
+
|
| 13 |
+
# Set up model loading and pipeline
|
| 14 |
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 15 |
os.environ['HF_HOME'] = '/tmp/cache'
|
| 16 |
|
|
|
|
| 17 |
model_name = "cerebras/btlm-3b-8k-chat"
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_name,
|
|
|
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
generator = pipeline(
|
| 27 |
"text-generation",
|
| 28 |
model=model,
|
| 29 |
tokenizer=tokenizer,
|
| 30 |
device_map="auto",
|
| 31 |
torch_dtype=torch_dtype,
|
| 32 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 33 |
trust_remote_code=True
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Flask app
|
| 37 |
+
app = Flask(__name__)
|
| 38 |
+
|
| 39 |
+
@app.route("/v1/chat/completions", methods=["POST"])
|
| 40 |
+
def chat():
|
| 41 |
+
# Token auth: require Bearer token
|
| 42 |
+
auth_header = request.headers.get("Authorization", "")
|
| 43 |
+
if not auth_header.startswith("Bearer ") or auth_header.split(" ")[1] != API_TOKEN:
|
| 44 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 45 |
+
|
| 46 |
+
data = request.json
|
| 47 |
+
messages = data.get("messages", [])
|
| 48 |
+
max_tokens = data.get("max_tokens", 256)
|
| 49 |
+
temperature = data.get("temperature", 0.7)
|
| 50 |
+
|
| 51 |
+
# Build the prompt from chat history
|
| 52 |
prompt = ""
|
| 53 |
+
for msg in messages:
|
| 54 |
+
role = msg.get("role", "user").capitalize()
|
| 55 |
+
content = msg.get("content", "")
|
| 56 |
+
prompt += f"{role}: {content}\n"
|
| 57 |
+
prompt += "Assistant:"
|
| 58 |
|
| 59 |
+
# Generate response
|
| 60 |
output = generator(
|
| 61 |
prompt,
|
| 62 |
+
max_new_tokens=max_tokens,
|
| 63 |
+
temperature=temperature,
|
| 64 |
top_p=0.9,
|
| 65 |
repetition_penalty=1.1,
|
| 66 |
do_sample=True
|
| 67 |
)
|
| 68 |
+
reply = output[0]["generated_text"].replace(prompt, "").strip()
|
| 69 |
|
| 70 |
+
# Return response in OpenAI-style format
|
| 71 |
+
return jsonify({
|
| 72 |
+
"choices": [
|
| 73 |
+
{
|
| 74 |
+
"message": {
|
| 75 |
+
"role": "assistant",
|
| 76 |
+
"content": reply
|
| 77 |
+
},
|
| 78 |
+
"finish_reason": "stop",
|
| 79 |
+
"index": 0
|
| 80 |
+
}
|
| 81 |
+
]
|
| 82 |
+
})
|
| 83 |
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
app.run(host="0.0.0.0", port=8081)
|