Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,48 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
# Load tokenizer
|
| 13 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
-
|
| 15 |
-
# Load model with
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.route("/chat", methods=["POST"])
|
| 25 |
def chat():
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
if not user_input:
|
| 29 |
return jsonify({"error": "Message is required"}), 400
|
| 30 |
|
|
|
|
| 31 |
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
| 32 |
-
|
| 33 |
-
with torch.no_grad(): # Disable gradient calculation to save memory
|
| 34 |
-
output = model.generate(**inputs, max_length=100)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
app.run(
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Model name from Hugging Face
|
| 8 |
+
MODEL_NAME = "tanusrich/Mental_Health_Chatbot"
|
| 9 |
|
| 10 |
+
# Detect if GPU is available
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
# Load tokenizer
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
|
| 16 |
+
# Load model with optimized settings
|
| 17 |
+
try:
|
| 18 |
+
model = AutoModel.from_pretrained(
|
| 19 |
+
MODEL_NAME,
|
| 20 |
+
device_map="auto", # Automatically selects best available device
|
| 21 |
+
low_cpu_mem_usage=True # Optimized for lower memory consumption
|
| 22 |
+
).to(device)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error loading model: {e}")
|
| 25 |
+
exit(1)
|
| 26 |
|
| 27 |
@app.route("/chat", methods=["POST"])
|
| 28 |
def chat():
|
| 29 |
+
data = request.json
|
| 30 |
+
user_input = data.get("message", "")
|
| 31 |
|
| 32 |
if not user_input:
|
| 33 |
return jsonify({"error": "Message is required"}), 400
|
| 34 |
|
| 35 |
+
# Tokenize input
|
| 36 |
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Generate response
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = model.generate(**inputs, max_length=150)
|
| 41 |
+
|
| 42 |
+
# Decode response
|
| 43 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
return jsonify({"response": response_text})
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
app.run(host="0.0.0.0", port=5000)
|