Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
-
from langchain_community.document_loaders import Docx2txtLoader
|
| 5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
-
from langchain_community.vectorstores import FAISS
|
| 7 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
|
| 9 |
# ===============================
|
| 10 |
-
# 1.
|
| 11 |
-
# ===============================
|
| 12 |
-
def load_documents():
|
| 13 |
-
files = [f for f in os.listdir() if f.endswith(".docx")]
|
| 14 |
-
docs = []
|
| 15 |
-
for file in files:
|
| 16 |
-
loader = Docx2txtLoader(file)
|
| 17 |
-
docs.extend(loader.load())
|
| 18 |
-
return docs
|
| 19 |
-
|
| 20 |
-
texts = load_documents()
|
| 21 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=200)
|
| 22 |
-
chunks = splitter.split_documents(texts)
|
| 23 |
-
|
| 24 |
-
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
-
vectorstore = FAISS.from_documents(chunks, embeddings)
|
| 26 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 27 |
-
|
| 28 |
-
# ===============================
|
| 29 |
-
# 2. Groq client
|
| 30 |
# ===============================
|
| 31 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 32 |
|
|
@@ -44,7 +21,7 @@ def groq_chat(query, history):
|
|
| 44 |
return response.choices[0].message.content.strip()
|
| 45 |
|
| 46 |
# ===============================
|
| 47 |
-
#
|
| 48 |
# ===============================
|
| 49 |
user_sessions = {}
|
| 50 |
|
|
@@ -55,7 +32,6 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 55 |
session = user_sessions.get(user_id, {"stage": 0, "name": None})
|
| 56 |
response = ""
|
| 57 |
|
| 58 |
-
# Stage 0 β first time: greet & ask name
|
| 59 |
if session["stage"] == 0:
|
| 60 |
response = (
|
| 61 |
"π Hey friend! Iβm really happy youβre here π€ "
|
|
@@ -63,7 +39,6 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 63 |
)
|
| 64 |
session["stage"] = 1
|
| 65 |
|
| 66 |
-
# Stage 1 β capture name once
|
| 67 |
elif session["stage"] == 1 and not session["name"]:
|
| 68 |
session["name"] = user_input.strip().capitalize()
|
| 69 |
meaning = get_name_meaning(session["name"])
|
|
@@ -74,7 +49,6 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 74 |
)
|
| 75 |
session["stage"] = 2
|
| 76 |
|
| 77 |
-
# Stage 2 β feelings
|
| 78 |
elif session["stage"] == 2:
|
| 79 |
response = (
|
| 80 |
f"π Thanks for sharing that, {session['name']}.\n"
|
|
@@ -82,7 +56,6 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 82 |
)
|
| 83 |
session["stage"] = 3
|
| 84 |
|
| 85 |
-
# Stage 3 β dig deeper
|
| 86 |
elif session["stage"] == 3:
|
| 87 |
response = (
|
| 88 |
f"π€ I hear you, {session['name']}. Your feelings really matter π\n"
|
|
@@ -90,7 +63,6 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 90 |
)
|
| 91 |
session["stage"] = 4
|
| 92 |
|
| 93 |
-
# Stage 4+ β natural psychologist conversation (Groq)
|
| 94 |
else:
|
| 95 |
groq_response = groq_chat(user_input, history)
|
| 96 |
response = groq_response if groq_response else (
|
|
@@ -102,15 +74,15 @@ def chatbot(user_input, history, user_id="default"):
|
|
| 102 |
return response
|
| 103 |
|
| 104 |
# ===============================
|
| 105 |
-
#
|
| 106 |
# ===============================
|
| 107 |
with gr.Blocks(css="""
|
| 108 |
body {background-color: #EAF4F8;}
|
| 109 |
.gradio-container {color: #2D2D2D; font-family: 'Segoe UI', sans-serif;}
|
| 110 |
h2 {color: #4E9A8E; text-align: center; font-weight: bold;}
|
| 111 |
.message.user {
|
| 112 |
-
background-color: #FFFFFF !important;
|
| 113 |
-
color: #000000 !important;
|
| 114 |
border-radius: 16px;
|
| 115 |
box-shadow: 0px 4px 10px rgba(0,0,0,0.12);
|
| 116 |
font-size: 15px;
|
|
@@ -118,8 +90,8 @@ with gr.Blocks(css="""
|
|
| 118 |
font-weight: 600;
|
| 119 |
}
|
| 120 |
.message.bot {
|
| 121 |
-
background-color: #B8A2D1 !important;
|
| 122 |
-
color: #2E3A3A !important;
|
| 123 |
border-radius: 16px;
|
| 124 |
box-shadow: 0px 4px 10px rgba(0,0,0,0.12);
|
| 125 |
font-size: 15px;
|
|
@@ -127,7 +99,7 @@ with gr.Blocks(css="""
|
|
| 127 |
font-weight: 600;
|
| 128 |
}
|
| 129 |
button {
|
| 130 |
-
background-color: #4E9A8E !important;
|
| 131 |
color: white !important;
|
| 132 |
border-radius: 12px !important;
|
| 133 |
font-weight: bold !important;
|
|
@@ -135,7 +107,7 @@ with gr.Blocks(css="""
|
|
| 135 |
}
|
| 136 |
textarea, input {
|
| 137 |
background-color: #FFFFFF !important;
|
| 138 |
-
color: #000000 !important;
|
| 139 |
border: 1px solid #CCC !important;
|
| 140 |
border-radius: 10px !important;
|
| 141 |
padding: 10px !important;
|
|
@@ -145,11 +117,7 @@ with gr.Blocks(css="""
|
|
| 145 |
|
| 146 |
gr.Markdown("<h2>πΏ MindEase: Your Safe Companion πΈ</h2>")
|
| 147 |
|
| 148 |
-
chatbot_ui = gr.Chatbot(
|
| 149 |
-
height=500,
|
| 150 |
-
bubble_full_width=False,
|
| 151 |
-
show_label=False
|
| 152 |
-
)
|
| 153 |
|
| 154 |
with gr.Row():
|
| 155 |
msg = gr.Textbox(placeholder="Type here and press Enter...", scale=10)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# ===============================
|
| 6 |
+
# 1. Groq client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ===============================
|
| 8 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 9 |
|
|
|
|
| 21 |
return response.choices[0].message.content.strip()
|
| 22 |
|
| 23 |
# ===============================
|
| 24 |
+
# 2. User sessions + flow
|
| 25 |
# ===============================
|
| 26 |
user_sessions = {}
|
| 27 |
|
|
|
|
| 32 |
session = user_sessions.get(user_id, {"stage": 0, "name": None})
|
| 33 |
response = ""
|
| 34 |
|
|
|
|
| 35 |
if session["stage"] == 0:
|
| 36 |
response = (
|
| 37 |
"π Hey friend! Iβm really happy youβre here π€ "
|
|
|
|
| 39 |
)
|
| 40 |
session["stage"] = 1
|
| 41 |
|
|
|
|
| 42 |
elif session["stage"] == 1 and not session["name"]:
|
| 43 |
session["name"] = user_input.strip().capitalize()
|
| 44 |
meaning = get_name_meaning(session["name"])
|
|
|
|
| 49 |
)
|
| 50 |
session["stage"] = 2
|
| 51 |
|
|
|
|
| 52 |
elif session["stage"] == 2:
|
| 53 |
response = (
|
| 54 |
f"π Thanks for sharing that, {session['name']}.\n"
|
|
|
|
| 56 |
)
|
| 57 |
session["stage"] = 3
|
| 58 |
|
|
|
|
| 59 |
elif session["stage"] == 3:
|
| 60 |
response = (
|
| 61 |
f"π€ I hear you, {session['name']}. Your feelings really matter π\n"
|
|
|
|
| 63 |
)
|
| 64 |
session["stage"] = 4
|
| 65 |
|
|
|
|
| 66 |
else:
|
| 67 |
groq_response = groq_chat(user_input, history)
|
| 68 |
response = groq_response if groq_response else (
|
|
|
|
| 74 |
return response
|
| 75 |
|
| 76 |
# ===============================
|
| 77 |
+
# 3. Gradio Interface (Warm + Visible)
|
| 78 |
# ===============================
|
| 79 |
with gr.Blocks(css="""
|
| 80 |
body {background-color: #EAF4F8;}
|
| 81 |
.gradio-container {color: #2D2D2D; font-family: 'Segoe UI', sans-serif;}
|
| 82 |
h2 {color: #4E9A8E; text-align: center; font-weight: bold;}
|
| 83 |
.message.user {
|
| 84 |
+
background-color: #FFFFFF !important;
|
| 85 |
+
color: #000000 !important;
|
| 86 |
border-radius: 16px;
|
| 87 |
box-shadow: 0px 4px 10px rgba(0,0,0,0.12);
|
| 88 |
font-size: 15px;
|
|
|
|
| 90 |
font-weight: 600;
|
| 91 |
}
|
| 92 |
.message.bot {
|
| 93 |
+
background-color: #B8A2D1 !important;
|
| 94 |
+
color: #2E3A3A !important;
|
| 95 |
border-radius: 16px;
|
| 96 |
box-shadow: 0px 4px 10px rgba(0,0,0,0.12);
|
| 97 |
font-size: 15px;
|
|
|
|
| 99 |
font-weight: 600;
|
| 100 |
}
|
| 101 |
button {
|
| 102 |
+
background-color: #4E9A8E !important;
|
| 103 |
color: white !important;
|
| 104 |
border-radius: 12px !important;
|
| 105 |
font-weight: bold !important;
|
|
|
|
| 107 |
}
|
| 108 |
textarea, input {
|
| 109 |
background-color: #FFFFFF !important;
|
| 110 |
+
color: #000000 !important;
|
| 111 |
border: 1px solid #CCC !important;
|
| 112 |
border-radius: 10px !important;
|
| 113 |
padding: 10px !important;
|
|
|
|
| 117 |
|
| 118 |
gr.Markdown("<h2>πΏ MindEase: Your Safe Companion πΈ</h2>")
|
| 119 |
|
| 120 |
+
chatbot_ui = gr.Chatbot(height=500, bubble_full_width=False, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
msg = gr.Textbox(placeholder="Type here and press Enter...", scale=10)
|