Spaces:
Sleeping
Sleeping
Commit
·
bf858f1
0
Parent(s):
Initial commit without background.jpg
Browse files- .gitattributes +39 -0
- .gitignore +0 -0
- Dockerfile +20 -0
- README.md +11 -0
- Requirements.txt +7 -0
- application.py +99 -0
- gitignore +1 -0
- static/script.js +49 -0
- static/style.css +154 -0
- templates/index.html +28 -0
.gitattributes
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
<<<<<<< HEAD
|
37 |
+
static/background.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
=======
|
39 |
+
>>>>>>> d81314b530b40e15259c3fcc60df37c5f08161cb
|
.gitignore
ADDED
Binary file (48 Bytes). View file
|
|
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.11-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /app
|
8 |
+
COPY . /app
|
9 |
+
|
10 |
+
# Install any needed packages specified in requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r Requirements.txt
|
12 |
+
|
13 |
+
# Make port 8080 available to the world outside this container
|
14 |
+
EXPOSE 8080
|
15 |
+
|
16 |
+
# Define environment variable for GCP
|
17 |
+
ENV PORT=8080
|
18 |
+
|
19 |
+
# Run the application using Gunicorn
|
20 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "application:application"]
|
README.md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: HarmonyBot
|
3 |
+
emoji: 👀
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: pink
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
license: apache-2.0
|
9 |
+
---
|
10 |
+
|
11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
Requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
gunicorn
|
3 |
+
huggingface_hub
|
4 |
+
datasets
|
5 |
+
markdown2
|
6 |
+
requests
|
7 |
+
transformers
|
application.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, jsonify, render_template, send_from_directory
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from datasets import load_dataset
|
5 |
+
import markdown2
|
6 |
+
import signal
|
7 |
+
|
8 |
+
application = Flask(__name__, static_folder='static', template_folder='templates')
|
9 |
+
|
10 |
+
hf_token = os.getenv("HF_TOKEN")
|
11 |
+
|
12 |
+
chat_doctor_dataset = load_dataset("avaliev/chat_doctor")
|
13 |
+
mental_health_dataset = load_dataset("Amod/mental_health_counseling_conversations")
|
14 |
+
|
15 |
+
client = InferenceClient(
|
16 |
+
"meta-llama/Meta-Llama-3-8B-Instruct",
|
17 |
+
token=hf_token,
|
18 |
+
)
|
19 |
+
|
20 |
+
def select_relevant_context(user_input):
|
21 |
+
mental_health_keywords = [
|
22 |
+
"anxious", "depressed", "stress", "mental health", "counseling",
|
23 |
+
"therapy", "feelings", "worthless", "suicidal", "panic", "anxiety"
|
24 |
+
]
|
25 |
+
medical_keywords = [
|
26 |
+
"symptoms", "diagnosis", "treatment", "doctor", "prescription", "medication",
|
27 |
+
"pain", "illness", "disease", "infection", "surgery"
|
28 |
+
]
|
29 |
+
|
30 |
+
# Check if the input contains any mental health-related keywords
|
31 |
+
if any(keyword in user_input.lower() for keyword in mental_health_keywords):
|
32 |
+
example = mental_health_dataset['train'][0]
|
33 |
+
context = f"Counselor: {example['Response']}\nUser: {example['Context']}"
|
34 |
+
# Check if the input contains any medical-related keywords
|
35 |
+
elif any(keyword in user_input.lower() for keyword in medical_keywords):
|
36 |
+
example = chat_doctor_dataset['train'][0]
|
37 |
+
context = f"Doctor: {example['input']}\nPatient: {example['output']}"
|
38 |
+
else:
|
39 |
+
# If no specific keywords are found, provide a general response
|
40 |
+
context = "You are a general assistant. Respond to the user's query in a helpful manner."
|
41 |
+
|
42 |
+
return context
|
43 |
+
|
44 |
+
def create_prompt(context, user_input):
|
45 |
+
prompt = (
|
46 |
+
f"{context}\n\n"
|
47 |
+
f"User: {user_input}\nAssistant:"
|
48 |
+
)
|
49 |
+
return prompt
|
50 |
+
|
51 |
+
# Function to render Markdown into HTML
|
52 |
+
def render_markdown(text):
|
53 |
+
return markdown2.markdown(text)
|
54 |
+
|
55 |
+
@application.route('/')
|
56 |
+
def index():
|
57 |
+
return render_template('index.html')
|
58 |
+
|
59 |
+
@application.route('/static/<path:path>')
|
60 |
+
def send_static(path):
|
61 |
+
return send_from_directory('static', path)
|
62 |
+
|
63 |
+
@application.route('/chat', methods=['POST'])
|
64 |
+
def chat():
|
65 |
+
user_input = request.json['message']
|
66 |
+
|
67 |
+
context = select_relevant_context(user_input)
|
68 |
+
|
69 |
+
prompt = create_prompt(context, user_input)
|
70 |
+
|
71 |
+
response = ""
|
72 |
+
for message in client.chat_completion(
|
73 |
+
messages=[{"role": "user", "content": prompt}],
|
74 |
+
max_tokens=500,
|
75 |
+
stream=True,
|
76 |
+
):
|
77 |
+
response += message.choices[0].delta.content
|
78 |
+
|
79 |
+
formatted_response = render_markdown(response)
|
80 |
+
|
81 |
+
return jsonify({"response": formatted_response})
|
82 |
+
|
83 |
+
@application.route('/shutdown', methods=['POST'])
|
84 |
+
def shutdown():
|
85 |
+
if request.environ.get('werkzeug.server.shutdown'):
|
86 |
+
shutdown_server()
|
87 |
+
else:
|
88 |
+
os.kill(os.getpid(), signal.SIGINT)
|
89 |
+
return jsonify({"message": "Server is shutting down..."})
|
90 |
+
|
91 |
+
def shutdown_server():
|
92 |
+
func = request.environ.get('werkzeug.server.shutdown')
|
93 |
+
if func is None:
|
94 |
+
os.kill(os.getpid(), signal.SIGINT) # Kill the process if Werkzeug is not available
|
95 |
+
else:
|
96 |
+
func()
|
97 |
+
|
98 |
+
if __name__ == '__main__':
|
99 |
+
application.run(debug=False)
|
gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
static/background.jpg
|
static/script.js
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
async function sendMessage() {
|
2 |
+
const userInput = document.getElementById('user-input').value;
|
3 |
+
if (!userInput) return;
|
4 |
+
|
5 |
+
const chatBox = document.getElementById('chat-box');
|
6 |
+
|
7 |
+
// Display user's message
|
8 |
+
const userMessage = document.createElement('div');
|
9 |
+
userMessage.classList.add('message', 'user');
|
10 |
+
userMessage.textContent = userInput;
|
11 |
+
chatBox.appendChild(userMessage);
|
12 |
+
|
13 |
+
// Clear the input field
|
14 |
+
document.getElementById('user-input').value = '';
|
15 |
+
|
16 |
+
// Send the message to the server
|
17 |
+
const response = await fetch('/chat', {
|
18 |
+
method: 'POST',
|
19 |
+
headers: { 'Content-Type': 'application/json' },
|
20 |
+
body: JSON.stringify({ message: userInput })
|
21 |
+
});
|
22 |
+
|
23 |
+
const data = await response.json();
|
24 |
+
|
25 |
+
// Create a new div for the bot's response
|
26 |
+
const botMessage = document.createElement('div');
|
27 |
+
botMessage.classList.add('message', 'bot');
|
28 |
+
botMessage.innerHTML = data.response; // Use innerHTML to render Markdown
|
29 |
+
|
30 |
+
chatBox.appendChild(botMessage);
|
31 |
+
|
32 |
+
// Scroll to the bottom of the chat box
|
33 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
34 |
+
}
|
35 |
+
|
36 |
+
function handleKeyPress(event) {
|
37 |
+
if (event.key === 'Enter') {
|
38 |
+
sendMessage();
|
39 |
+
}
|
40 |
+
}
|
41 |
+
|
42 |
+
async function exitChat() {
|
43 |
+
await fetch('/shutdown', { method: 'POST' });
|
44 |
+
}
|
45 |
+
|
46 |
+
function newChat() {
|
47 |
+
const chatBox = document.getElementById('chat-box');
|
48 |
+
chatBox.innerHTML = '<div class="message bot">Hi, Welcome to the Medical Bot. What is your query?</div>';
|
49 |
+
}
|
static/style.css
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: 'Helvetica Neue', Arial, sans-serif;
|
3 |
+
margin: 0;
|
4 |
+
padding: 0;
|
5 |
+
background: url('/static/background.jpg') no-repeat center center fixed;
|
6 |
+
background-size: cover;
|
7 |
+
display: flex;
|
8 |
+
justify-content: center;
|
9 |
+
align-items: center;
|
10 |
+
height: 100vh;
|
11 |
+
}
|
12 |
+
|
13 |
+
.chat-container {
|
14 |
+
width: 100%;
|
15 |
+
max-width: 900px;
|
16 |
+
height: 90vh;
|
17 |
+
display: flex;
|
18 |
+
background-color: rgb(236, 59, 177);
|
19 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
20 |
+
border-radius: 10px;
|
21 |
+
overflow: hidden;
|
22 |
+
}
|
23 |
+
|
24 |
+
.sidebar {
|
25 |
+
width: 120px;
|
26 |
+
background-color: hsl(280, 48%, 28%);
|
27 |
+
display: flex;
|
28 |
+
flex-direction: column;
|
29 |
+
align-items: center;
|
30 |
+
padding: 20px 10px;
|
31 |
+
}
|
32 |
+
|
33 |
+
.sidebar button {
|
34 |
+
background-color: #071c32;
|
35 |
+
color: rgb(255, 255, 255);
|
36 |
+
border: none;
|
37 |
+
padding: 15px 20px;
|
38 |
+
border-radius: 20px;
|
39 |
+
margin: 20px 0;
|
40 |
+
cursor: pointer;
|
41 |
+
font-size: 16px;
|
42 |
+
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
|
43 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
44 |
+
width: 100px; /* Ensure buttons are the same width */
|
45 |
+
text-align: center;
|
46 |
+
}
|
47 |
+
|
48 |
+
.sidebar button:hover {
|
49 |
+
transform: scale(1.05);
|
50 |
+
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);
|
51 |
+
}
|
52 |
+
|
53 |
+
.main-content {
|
54 |
+
flex: 1;
|
55 |
+
display: flex;
|
56 |
+
flex-direction: column;
|
57 |
+
background-color: rgba(125, 130, 131, 0.455);
|
58 |
+
}
|
59 |
+
|
60 |
+
.chat-box {
|
61 |
+
flex: 1;
|
62 |
+
padding: 20px;
|
63 |
+
overflow-y: auto;
|
64 |
+
border-bottom: 1px solid #e91199;
|
65 |
+
}
|
66 |
+
|
67 |
+
.message {
|
68 |
+
margin: 10px 0;
|
69 |
+
padding: 10px 15px;
|
70 |
+
border-radius: 20px;
|
71 |
+
max-width: 75%;
|
72 |
+
word-wrap: break-word;
|
73 |
+
box-shadow: 0 0 5px rgb(203, 60, 146);
|
74 |
+
}
|
75 |
+
|
76 |
+
/* Markdown specific styling */
|
77 |
+
.message.bot p {
|
78 |
+
margin: 0 0 10px;
|
79 |
+
}
|
80 |
+
|
81 |
+
.message.bot ul {
|
82 |
+
margin: 10px 0;
|
83 |
+
padding-left: 20px;
|
84 |
+
}
|
85 |
+
|
86 |
+
.message.bot ol {
|
87 |
+
margin: 10px 0;
|
88 |
+
padding-left: 20px;
|
89 |
+
}
|
90 |
+
|
91 |
+
.message.bot li {
|
92 |
+
margin-bottom: 5px;
|
93 |
+
}
|
94 |
+
|
95 |
+
.message.bot code {
|
96 |
+
background-color: rgba(27, 31, 35, 0.05);
|
97 |
+
padding: 2px 4px;
|
98 |
+
border-radius: 3px;
|
99 |
+
font-family: 'Courier New', Courier, monospace;
|
100 |
+
}
|
101 |
+
|
102 |
+
.message.bot pre {
|
103 |
+
background-color: rgba(27, 31, 35, 0.1);
|
104 |
+
padding: 10px;
|
105 |
+
border-radius: 5px;
|
106 |
+
overflow-x: auto;
|
107 |
+
font-family: 'Courier New', Courier, monospace;
|
108 |
+
}
|
109 |
+
|
110 |
+
/* User messages */
|
111 |
+
.message.user {
|
112 |
+
background-color: hwb(285 20% 63% / 0.815);
|
113 |
+
color: rgba(214, 199, 199, 0.879);
|
114 |
+
align-self: flex-end;
|
115 |
+
}
|
116 |
+
|
117 |
+
.message.bot {
|
118 |
+
background-color: hwb(297 45% 14% / 0.815) ;
|
119 |
+
color: #151415;
|
120 |
+
align-self: flex-start;
|
121 |
+
}
|
122 |
+
|
123 |
+
.input-container {
|
124 |
+
display: flex;
|
125 |
+
padding: 15px;
|
126 |
+
border-top: 1px solid #ddd;
|
127 |
+
background-color: rgba(250, 250, 250, 0.9);
|
128 |
+
}
|
129 |
+
|
130 |
+
input[type="text"] {
|
131 |
+
flex: 1;
|
132 |
+
padding: 10px;
|
133 |
+
border: 1px solid #ddd;
|
134 |
+
border-radius: 20px;
|
135 |
+
margin-right: 10px;
|
136 |
+
font-size: 16px;
|
137 |
+
}
|
138 |
+
|
139 |
+
button.send-button {
|
140 |
+
padding: 10px 20px;
|
141 |
+
background-color: hsl(280, 48%, 28%);
|
142 |
+
color: white;
|
143 |
+
border: none;
|
144 |
+
border-radius: 20px;
|
145 |
+
cursor: pointer;
|
146 |
+
font-size: 16px;
|
147 |
+
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
|
148 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
149 |
+
}
|
150 |
+
|
151 |
+
button.send-button:hover {
|
152 |
+
transform: scale(1.05);
|
153 |
+
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);
|
154 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Medical Bot</title>
|
7 |
+
<link rel="stylesheet" href="static/style.css">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<div class="chat-container">
|
11 |
+
<div class="sidebar">
|
12 |
+
<button class="new-chat-button" onclick="newChat()">New</button>
|
13 |
+
<button class="exit-button" onclick="exitChat()">Exit</button>
|
14 |
+
</div>
|
15 |
+
<div class="main-content">
|
16 |
+
<div id="chat-box" class="chat-box">
|
17 |
+
<div class="message bot">Hi, Welcome to the Medical Bot. What is your query?</div>
|
18 |
+
</div>
|
19 |
+
<div class="input-container">
|
20 |
+
<input type="text" id="user-input" placeholder="Type your message here..." onkeypress="handleKeyPress(event)" />
|
21 |
+
<button class="send-button" onclick="sendMessage()">Send</button>
|
22 |
+
</div>
|
23 |
+
</div>
|
24 |
+
</div>
|
25 |
+
|
26 |
+
<script src="static/script.js"></script>
|
27 |
+
</body>
|
28 |
+
</html>
|