Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import torch
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
MODEL_NAME = "tanusrich/Mental_Health_Chatbot"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
|
@@ -22,22 +24,28 @@ except Exception as e:
|
|
| 22 |
|
| 23 |
@app.route("/chat", methods=["POST"])
|
| 24 |
def chat():
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
return jsonify({"error": "Message is required"}), 400
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
outputs = model.generate(
|
| 35 |
-
input_ids=inputs["input_ids"],
|
| 36 |
-
attention_mask=inputs["attention_mask"],
|
| 37 |
-
max_length=150
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS # To handle CORS if needed
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
CORS(app) # Enable CORS for frontend communication
|
| 8 |
+
|
| 9 |
MODEL_NAME = "tanusrich/Mental_Health_Chatbot"
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
|
|
|
| 24 |
|
| 25 |
@app.route("/chat", methods=["POST"])
|
| 26 |
def chat():
|
| 27 |
+
try:
|
| 28 |
+
data = request.json
|
| 29 |
+
user_input = data.get("message", "").strip()
|
| 30 |
+
|
| 31 |
+
if not user_input:
|
| 32 |
+
return jsonify({"error": "Message is required"}), 400
|
| 33 |
|
| 34 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
|
|
|
| 35 |
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
input_ids=inputs["input_ids"],
|
| 39 |
+
attention_mask=inputs["attention_mask"],
|
| 40 |
+
max_length=150
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
return jsonify({"response": response_text})
|
| 46 |
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
| 49 |
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|