| |
| from flask import Flask, jsonify, request |
| import requests |
| import os |
|
|
| |
| app = Flask(__name__) |
|
|
| |
| model_name = "tanusrich/Mental_Health_Chatbot" |
| api_url = f"https://api-inference.huggingface.co/models/{model_name}" |
|
|
| |
| api_token = os.getenv("HF_API_TOKEN") |
|
|
| |
| if api_token is None: |
| raise ValueError("Hugging Face API token is not set in the environment variables.") |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {api_token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| def chat_with_model(input_texts): |
| |
| payload = { |
| "inputs": input_texts, |
| "parameters": { |
| "max_length": 50 |
| } |
| } |
|
|
| |
| response = requests.post(api_url, headers=headers, json=payload) |
|
|
| |
| if response.status_code == 200: |
| |
| return [resp['generated_text'] for resp in response.json()] |
| else: |
| |
| return f"Error: {response.status_code}, {response.text}" |
|
|
| @app.route('/') |
| def home(): |
| return jsonify({"message": "Welcome to the Mental Health Therapy Chatbot!"}) |
|
|
| @app.route('/chat', methods=['POST']) |
| def chat(): |
| data = request.get_json() |
| user_inputs = data.get('inputs') |
|
|
| if user_inputs: |
| |
| responses = chat_with_model(user_inputs) |
| return jsonify({"responses": responses}) |
| else: |
| return jsonify({"error": "No inputs provided."}), 400 |
|
|
| if __name__ == '__main__': |
| app.run(debug=False) |