debug
Browse files- data/models/llama3-1-70b.py +14 -24
data/models/llama3-1-70b.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from huggingface_hub import login
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import spaces
|
|
|
|
| 5 |
import torch
|
| 6 |
import os
|
| 7 |
|
|
@@ -12,19 +12,18 @@ api_key = os.getenv("UCODE_SECRET")
|
|
| 12 |
login(api_key,add_to_git_credential=True)
|
| 13 |
|
| 14 |
model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
)
|
| 23 |
|
| 24 |
@app.route('/chat', methods=['POST'])
|
| 25 |
-
@spaces.GPU()
|
| 26 |
def chat_completion():
|
| 27 |
-
torch.cuda.empty_cache()
|
| 28 |
data = request.json
|
| 29 |
|
| 30 |
user_input = data[0].get('user_input', [])
|
|
@@ -33,28 +32,19 @@ def chat_completion():
|
|
| 33 |
top_p = data[0].get('top_p', 0.95)
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
generate_kwargs = dict(
|
| 40 |
-
inputs,
|
| 41 |
-
top_p=top_p,
|
| 42 |
max_new_tokens=max_tokens,
|
| 43 |
-
do_sample=True,
|
| 44 |
temperature=temperature,
|
| 45 |
-
|
| 46 |
)
|
| 47 |
-
|
| 48 |
-
outputs = model.generate(**generate_kwargs)
|
| 49 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
-
|
| 51 |
-
return jsonify({"status": "success", "output": generated_text})
|
| 52 |
except Exception as e:
|
| 53 |
return jsonify({"status": "error", "message": str(e)})
|
| 54 |
|
| 55 |
|
| 56 |
def main():
|
| 57 |
-
app.run(host='0.0.0.0', port=
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
main()
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from huggingface_hub import login
|
|
|
|
| 3 |
import spaces
|
| 4 |
+
import transformers
|
| 5 |
import torch
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 12 |
login(api_key,add_to_git_credential=True)
|
| 13 |
|
| 14 |
model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
|
| 15 |
+
|
| 16 |
+
pipeline = transformers.pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model=model_id,
|
| 19 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 20 |
+
device="cuda",
|
| 21 |
+
token=True
|
| 22 |
)
|
| 23 |
|
| 24 |
@app.route('/chat', methods=['POST'])
|
| 25 |
+
@spaces.GPU(enable_queue=True)
|
| 26 |
def chat_completion():
|
|
|
|
| 27 |
data = request.json
|
| 28 |
|
| 29 |
user_input = data[0].get('user_input', [])
|
|
|
|
| 32 |
top_p = data[0].get('top_p', 0.95)
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
outputs = pipeline(
|
| 36 |
+
user_input,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
max_new_tokens=max_tokens,
|
|
|
|
| 38 |
temperature=temperature,
|
| 39 |
+
top_p=top_p
|
| 40 |
)
|
| 41 |
+
return jsonify({"status": "success", "output": outputs[0]["generated_text"][-1]})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return jsonify({"status": "error", "message": str(e)})
|
| 44 |
|
| 45 |
|
| 46 |
def main():
|
| 47 |
+
app.run(host='0.0.0.0', port=7051)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
main()
|