Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
|
|
| 1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 2 |
|
| 3 |
-
# Load
|
| 4 |
-
model_name = "Qwen/Qwen2.5-0.5B-Instruct" #
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
model_name,
|
|
@@ -9,7 +11,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 9 |
device_map="auto"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def chat(prompt):
|
| 14 |
messages = [{"role": "user", "content": prompt}]
|
| 15 |
text = tokenizer.apply_chat_template(
|
|
@@ -24,5 +26,21 @@ def chat(prompt):
|
|
| 24 |
|
| 25 |
return response
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# 1️⃣ Load model and tokenizer
|
| 6 |
+
model_name = "Qwen/Qwen2.5-0.5B-Instruct" # Change to 1.5B, 3B, 7B if needed
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
model_name,
|
|
|
|
| 11 |
device_map="auto"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# 2️⃣ Define chat function
|
| 15 |
def chat(prompt):
|
| 16 |
messages = [{"role": "user", "content": prompt}]
|
| 17 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 26 |
|
| 27 |
return response
|
| 28 |
|
| 29 |
+
# 3️⃣ Create Flask app
|
| 30 |
+
app = Flask(__name__)
|
| 31 |
+
|
| 32 |
+
@app.route("/chat", methods=["GET"])
|
| 33 |
+
def chat_route():
|
| 34 |
+
user_message = request.args.get("message") # Get message from query string
|
| 35 |
+
if not user_message:
|
| 36 |
+
return jsonify({"error": "No message provided"}), 400
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = chat(user_message)
|
| 40 |
+
return jsonify({"response": response})
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return jsonify({"error": str(e)}), 500
|
| 43 |
+
|
| 44 |
+
# 4️⃣ Run Flask on port 7860
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
app.run(host="0.0.0.0", port=7860)
|