Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,16 +5,12 @@ from datasets import load_dataset
|
|
5 |
import markdown2
|
6 |
import signal
|
7 |
|
8 |
-
# Set Hugging Face cache directory
|
9 |
os.environ["HF_HOME"] = "/app/.cache"
|
10 |
|
11 |
-
# Initialize Flask app
|
12 |
application = Flask(__name__, static_folder='static', template_folder='templates')
|
13 |
|
14 |
-
# Get Hugging Face token from environment variables
|
15 |
hf_token = os.getenv("HF_TOKEN")
|
16 |
|
17 |
-
# Load datasets and inference client from Hugging Face Hub
|
18 |
chat_doctor_dataset = load_dataset("avaliev/chat_doctor")
|
19 |
mental_health_dataset = load_dataset("Amod/mental_health_counseling_conversations")
|
20 |
|
@@ -23,7 +19,6 @@ client = InferenceClient(
|
|
23 |
token=hf_token,
|
24 |
)
|
25 |
|
26 |
-
# Function to select context based on the user's input
|
27 |
def select_relevant_context(user_input):
|
28 |
mental_health_keywords = [
|
29 |
"anxious", "depressed", "stress", "mental health", "counseling",
|
@@ -48,30 +43,31 @@ def select_relevant_context(user_input):
|
|
48 |
|
49 |
return context
|
50 |
|
51 |
-
# Function to create the prompt to send to the AI model
|
52 |
def create_prompt(context, user_input):
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Function to render Markdown into HTML
|
56 |
def render_markdown(text):
|
57 |
return markdown2.markdown(text)
|
58 |
|
59 |
-
# Flask route to serve the homepage
|
60 |
@application.route('/')
|
61 |
def index():
|
62 |
return render_template('index.html')
|
63 |
|
64 |
-
# Route to serve static files
|
65 |
@application.route('/static/<path:path>')
|
66 |
def send_static(path):
|
67 |
return send_from_directory('static', path)
|
68 |
|
69 |
-
# Route to handle chat interaction
|
70 |
@application.route('/chat', methods=['POST'])
|
71 |
def chat():
|
72 |
user_input = request.json['message']
|
73 |
|
74 |
context = select_relevant_context(user_input)
|
|
|
75 |
prompt = create_prompt(context, user_input)
|
76 |
|
77 |
response = ""
|
@@ -86,7 +82,6 @@ def chat():
|
|
86 |
|
87 |
return jsonify({"response": formatted_response})
|
88 |
|
89 |
-
# Route to shutdown the server (if necessary)
|
90 |
@application.route('/shutdown', methods=['POST'])
|
91 |
def shutdown():
|
92 |
if request.environ.get('werkzeug.server.shutdown'):
|
@@ -95,7 +90,6 @@ def shutdown():
|
|
95 |
os.kill(os.getpid(), signal.SIGINT)
|
96 |
return jsonify({"message": "Server is shutting down..."})
|
97 |
|
98 |
-
# Function to handle the server shutdown
|
99 |
def shutdown_server():
|
100 |
func = request.environ.get('werkzeug.server.shutdown')
|
101 |
if func is None:
|
@@ -103,6 +97,5 @@ def shutdown_server():
|
|
103 |
else:
|
104 |
func()
|
105 |
|
106 |
-
# Run the app without specifying host and port (Hugging Face Spaces handles this)
|
107 |
if __name__ == '__main__':
|
108 |
-
application.run()
|
|
|
5 |
import markdown2
|
6 |
import signal
|
7 |
|
|
|
8 |
os.environ["HF_HOME"] = "/app/.cache"
|
9 |
|
|
|
10 |
application = Flask(__name__, static_folder='static', template_folder='templates')
|
11 |
|
|
|
12 |
hf_token = os.getenv("HF_TOKEN")
|
13 |
|
|
|
14 |
chat_doctor_dataset = load_dataset("avaliev/chat_doctor")
|
15 |
mental_health_dataset = load_dataset("Amod/mental_health_counseling_conversations")
|
16 |
|
|
|
19 |
token=hf_token,
|
20 |
)
|
21 |
|
|
|
22 |
def select_relevant_context(user_input):
|
23 |
mental_health_keywords = [
|
24 |
"anxious", "depressed", "stress", "mental health", "counseling",
|
|
|
43 |
|
44 |
return context
|
45 |
|
|
|
46 |
def create_prompt(context, user_input):
|
47 |
+
prompt = (
|
48 |
+
f"{context}\n\n"
|
49 |
+
f"User: {user_input}\nAssistant:"
|
50 |
+
)
|
51 |
+
return prompt
|
52 |
|
53 |
# Function to render Markdown into HTML
|
54 |
def render_markdown(text):
|
55 |
return markdown2.markdown(text)
|
56 |
|
|
|
57 |
@application.route('/')
|
58 |
def index():
|
59 |
return render_template('index.html')
|
60 |
|
|
|
61 |
@application.route('/static/<path:path>')
|
62 |
def send_static(path):
|
63 |
return send_from_directory('static', path)
|
64 |
|
|
|
65 |
@application.route('/chat', methods=['POST'])
|
66 |
def chat():
|
67 |
user_input = request.json['message']
|
68 |
|
69 |
context = select_relevant_context(user_input)
|
70 |
+
|
71 |
prompt = create_prompt(context, user_input)
|
72 |
|
73 |
response = ""
|
|
|
82 |
|
83 |
return jsonify({"response": formatted_response})
|
84 |
|
|
|
85 |
@application.route('/shutdown', methods=['POST'])
|
86 |
def shutdown():
|
87 |
if request.environ.get('werkzeug.server.shutdown'):
|
|
|
90 |
os.kill(os.getpid(), signal.SIGINT)
|
91 |
return jsonify({"message": "Server is shutting down..."})
|
92 |
|
|
|
93 |
def shutdown_server():
|
94 |
func = request.environ.get('werkzeug.server.shutdown')
|
95 |
if func is None:
|
|
|
97 |
else:
|
98 |
func()
|
99 |
|
|
|
100 |
if __name__ == '__main__':
|
101 |
+
application.run(host='0.0.0.0', port=7680)
|