Ritesh-hf commited on
Commit
02223fb
1 Parent(s): 473df30

initial commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,8 @@ saved_model/**/* 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
 
 
 
 
 
 
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
+ faiss_recursive_split_word_doc_index/index.faiss filter=lfs diff=lfs merge=lfs -text
37
+ faiss_recursive_split_word_doc_index/index.pkl filter=lfs diff=lfs merge=lfs -text
38
+ faiss_excel_doc_index/index.faiss filter=lfs diff=lfs merge=lfs -text
39
+ faiss_excel_doc_index/index.pkl filter=lfs diff=lfs merge=lfs -text
40
+ combined_recursive_keyword_retriever.pkl filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "-k", "eventlet", "app:app"]
README.md CHANGED
@@ -1,11 +1,10 @@
1
  ---
2
- title: ADAFSA FLASK APP DEMO
3
- emoji: 🚀
4
- colorFrom: yellow
5
- colorTo: red
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
 
1
  ---
2
+ title: ADAFSA RAG DEMO FLASK
3
+ emoji: 📉
4
+ colorFrom: red
5
+ colorTo: blue
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from flask import Flask, request, render_template
4
+ from flask_cors import CORS
5
+ from flask_socketio import SocketIO, emit, join_room, leave_room
6
+ from langchain_core.output_parsers import StrOutputParser
7
+ from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder
8
+ from langchain_core.runnables import RunnablePassthrough
9
+ from langchain_huggingface.embeddings import HuggingFaceEmbeddings
10
+ from langchain.retrievers.document_compressors import EmbeddingsFilter
11
+ from langchain.retrievers import ContextualCompressionRetriever
12
+ from langchain.retrievers import EnsembleRetriever
13
+ from langchain_community.vectorstores import FAISS
14
+ from langchain_groq import ChatGroq
15
+ from langchain import hub
16
+ import pickle
17
+ import os
18
+
19
+
20
+
21
+ # Load environment variables
22
+ load_dotenv(".env")
23
+ USER_AGENT = os.getenv("USER_AGENT")
24
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
25
+ SECRET_KEY = os.getenv("SECRET_KEY")
26
+ SESSION_ID_DEFAULT = "abc123"
27
+
28
+ # Set environment variables
29
+ os.environ['USER_AGENT'] = USER_AGENT
30
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
31
+ os.environ["TOKENIZERS_PARALLELISM"] = 'true'
32
+
33
+ # Initialize Flask app and SocketIO with CORS
34
+ app = Flask(__name__)
35
+ CORS(app)
36
+ socketio = SocketIO(app, cors_allowed_origins="*")
37
+ app.config['SESSION_COOKIE_SECURE'] = True # Use HTTPS
38
+ app.config['SESSION_COOKIE_HTTPONLY'] = True
39
+ app.config['SECRET_KEY'] = SECRET_KEY
40
+
41
+
42
+ embed_model = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-multilingual-base", model_kwargs={"trust_remote_code":True})
43
+ llm = ChatGroq(
44
+ model="llama-3.1-8b-instant",
45
+ temperature=0.0,
46
+ max_tokens=1024,
47
+ max_retries=2
48
+ )
49
+
50
+ excel_vectorstore = FAISS.load_local(folder_path="./faiss_excel_doc_index", embeddings=embed_model, allow_dangerous_deserialization=True)
51
+ word_vectorstore = FAISS.load_local(folder_path="./faiss_recursive_split_word_doc_index", embeddings=embed_model, allow_dangerous_deserialization=True)
52
+ excel_vectorstore.merge_from(word_vectorstore)
53
+ combined_vectorstore = excel_vectorstore
54
+
55
+ with open('combined_recursive_keyword_retriever.pkl', 'rb') as f:
56
+ combined_keyword_retriever = pickle.load(f)
57
+ combined_keyword_retriever.k = 100
58
+
59
+ semantic_retriever = combined_vectorstore.as_retriever(search_type="mmr", search_kwargs={'k': 100})
60
+
61
+
62
+ # initialize the ensemble retriever
63
+ ensemble_retriever = EnsembleRetriever(
64
+ retrievers=[combined_keyword_retriever, semantic_retriever], weights=[0.5, 0.5]
65
+ )
66
+
67
+
68
+ embeddings_filter = EmbeddingsFilter(embeddings=embed_model, similarity_threshold=0.5)
69
+ compression_retriever = ContextualCompressionRetriever(
70
+ base_compressor=embeddings_filter, base_retriever=ensemble_retriever
71
+ )
72
+
73
+ template = """
74
+ User: You are an Arabic AI Assistant that follows instructions extremely well.
75
+ Please be truthful and give direct answers. Please tell 'I don't know' if user query is not in CONTEXT.
76
+ Generate the response in Arabic. Use bullet/number lists wherever necessary. If the response includes any English words or numbers,
77
+ format them so that they are displayed from left to right within the Arabic text.
78
+ Use the appropriate Unicode control characters to achieve this. For example,
79
+ place the Left-to-Right embedding character (U+202A) before the English word or number,
80
+ and the Pop Directional Formatting character (U+202C) afterward.
81
+
82
+ Example:
83
+ Input: "What year is it?"
84
+ Output: "ما هو العام؟ \u202A2023\u202C"
85
+
86
+ Give detail but concise answers explaining all the important parts.
87
+ Keep in mind, you will lose the job, if you answer out of CONTEXT questions
88
+
89
+ CONTEXT: {context}
90
+ Query: {question}
91
+
92
+ Remember only return AI answer
93
+ Assistant:
94
+ """
95
+
96
+ prompt = ChatPromptTemplate.from_template(template)
97
+ output_parser = StrOutputParser()
98
+
99
+ def format_docs(docs):
100
+ return "\n\n".join(doc.page_content for doc in docs)
101
+
102
+
103
+ rag_chain = (
104
+ {"context": compression_retriever.with_config(run_name="Docs") | format_docs, "question": RunnablePassthrough()}
105
+ | prompt
106
+ | llm
107
+ | output_parser
108
+ )
109
+
110
+ # Function to handle WebSocket connection
111
+ @socketio.on('connect')
112
+ def handle_connect():
113
+ emit('connection_response', {'message': 'Connected successfully.'}, room=request.sid)
114
+
115
+ @socketio.on('ping')
116
+ def handle_ping(data):
117
+ emit('ping_response', {'message': 'Healthy Connection.'}, room=request.sid)
118
+
119
+ # Function to handle WebSocket disconnection
120
+ @socketio.on('disconnect')
121
+ def handle_disconnect():
122
+ emit('connection_response', {'message': 'Disconnected successfully.'})
123
+
124
+ # Function to handle WebSocket messages
125
+ @socketio.on('message')
126
+ def handle_message(data):
127
+ question = data.get('question')
128
+ try:
129
+ for chunk in rag_chain.stream(question):
130
+ emit('response', chunk, room=request.sid)
131
+ except Exception as e:
132
+ emit('response', {"error": "An error occurred while processing your request."}, room=request.sid)
133
+
134
+
135
+ # Home route
136
+ @app.route("/")
137
+ def index_view():
138
+ return render_template('chat.html')
139
+
140
+ # Main function to run the app
141
+ if __name__ == '__main__':
142
+ socketio.run(app, debug=True)
combined_recursive_keyword_retriever.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:71c816aa5e0cb849c3c9f36ca72ecf7b0968d0fd5ab5a63a3316223e68d5398d
3
+ size 8449174
faiss_excel_doc_index/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:93af2db742f688ef046e298394b29bb6124c97bc01c841aa9e7d11d95ace38d3
3
+ size 441538605
faiss_excel_doc_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79bf3786db209a1e32e72906d3ab7edce408ff000728b7af28b55bc729c02285
3
+ size 58044220
faiss_recursive_split_word_doc_index/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4e21d5d78d4acf373e94ae40d43fcad7b724207b7b4c18455cc1fc613b6c01f5
3
+ size 14736429
faiss_recursive_split_word_doc_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:27889ba1e7400d896ad677b1e545fd7a01ee16b8d2dbd3c2b9c6431d5b0ff50d
3
+ size 4029431
requirements.txt ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohappyeyeballs==2.4.0
3
+ aiohttp==3.10.5
4
+ aiosignal==1.3.1
5
+ annotated-types==0.7.0
6
+ anyio==4.4.0
7
+ async-timeout==4.0.3
8
+ attrs==24.2.0
9
+ bidict==0.23.1
10
+ blinker==1.8.2
11
+ certifi==2024.8.30
12
+ charset-normalizer==3.3.2
13
+ click==8.1.7
14
+ contourpy==1.1.1
15
+ cycler==0.12.1
16
+ dataclasses-json==0.6.7
17
+ distro==1.9.0
18
+ dnspython==2.6.1
19
+ eventlet==0.36.1
20
+ exceptiongroup==1.2.2
21
+ faiss-cpu==1.8.0.post1
22
+ faiss-gpu==1.7.2
23
+ fastapi==0.114.1
24
+ ffmpy==0.4.0
25
+ filelock==3.16.0
26
+ flask==3.0.3
27
+ Flask-Cors==5.0.0
28
+ Flask-SocketIO==5.3.7
29
+ fonttools==4.53.1
30
+ frozenlist==1.4.1
31
+ fsspec==2024.9.0
32
+ gradio==4.44.0
33
+ gradio-client==1.3.0
34
+ greenlet==3.1.0
35
+ groq==0.11.0
36
+ gunicorn==23.0.0
37
+ h11==0.14.0
38
+ httpcore==1.0.5
39
+ httpx==0.27.2
40
+ huggingface-hub==0.24.6
41
+ idna==3.8
42
+ importlib-metadata==8.5.0
43
+ importlib-resources==6.4.5
44
+ itsdangerous==2.2.0
45
+ jinja2==3.1.4
46
+ joblib==1.4.2
47
+ jsonpatch==1.33
48
+ jsonpointer==3.0.0
49
+ kiwisolver==1.4.7
50
+ langchain==0.2.16
51
+ langchain-community==0.2.16
52
+ langchain-core==0.2.39
53
+ langchain-groq==0.1.9
54
+ langchain-huggingface==0.0.3
55
+ langchain-text-splitters==0.2.4
56
+ langsmith==0.1.117
57
+ markdown-it-py==3.0.0
58
+ MarkupSafe==2.1.5
59
+ marshmallow==3.22.0
60
+ matplotlib==3.7.5
61
+ mdurl==0.1.2
62
+ mpmath==1.3.0
63
+ multidict==6.1.0
64
+ mypy-extensions==1.0.0
65
+ networkx==3.1
66
+ numpy==1.24.4
67
+ nvidia-cublas-cu12==12.1.3.1
68
+ nvidia-cuda-cupti-cu12==12.1.105
69
+ nvidia-cuda-nvrtc-cu12==12.1.105
70
+ nvidia-cuda-runtime-cu12==12.1.105
71
+ nvidia-cudnn-cu12==9.1.0.70
72
+ nvidia-cufft-cu12==11.0.2.54
73
+ nvidia-curand-cu12==10.3.2.106
74
+ nvidia-cusolver-cu12==11.4.5.107
75
+ nvidia-cusparse-cu12==12.1.0.106
76
+ nvidia-nccl-cu12==2.20.5
77
+ nvidia-nvjitlink-cu12==12.6.68
78
+ nvidia-nvtx-cu12==12.1.105
79
+ orjson==3.10.7
80
+ packaging==24.1
81
+ pandas==2.0.3
82
+ pillow==10.4.0
83
+ psutil==5.9.8
84
+ pydantic==2.9.1
85
+ pydantic-core==2.23.3
86
+ pydub==0.25.1
87
+ pygments==2.18.0
88
+ pyparsing==3.1.4
89
+ python-dateutil==2.9.0.post0
90
+ python-dotenv==1.0.1
91
+ python-engineio==4.9.1
92
+ python-multipart==0.0.9
93
+ python-socketio==5.11.4
94
+ pytz==2024.2
95
+ PyYAML==6.0.2
96
+ rank-bm25==0.2.2
97
+ regex==2024.7.24
98
+ requests==2.32.3
99
+ rich==13.8.1
100
+ ruff==0.6.4
101
+ safetensors==0.4.5
102
+ scikit-learn==1.3.2
103
+ scipy==1.10.1
104
+ semantic-version==2.10.0
105
+ sentence-transformers==3.1.0
106
+ shellingham==1.5.4
107
+ simple-websocket==1.0.0
108
+ six==1.16.0
109
+ sniffio==1.3.1
110
+ spaces==0.30.2
111
+ SQLAlchemy==2.0.34
112
+ starlette==0.38.5
113
+ sympy==1.13.2
114
+ tenacity==8.5.0
115
+ threadpoolctl==3.5.0
116
+ tokenizers==0.19.1
117
+ tomlkit==0.12.0
118
+ torch==2.4.1
119
+ tqdm==4.66.5
120
+ transformers==4.44.2
121
+ triton==3.0.0
122
+ typer==0.12.5
123
+ typing-extensions==4.12.2
124
+ typing-inspect==0.9.0
125
+ tzdata==2024.1
126
+ urllib3==2.2.2
127
+ uvicorn==0.30.6
128
+ websockets==12.0
129
+ werkzeug==3.0.4
130
+ wsproto==1.2.0
131
+ yarl==1.11.1
132
+ zipp==3.20.1
static/bg-image.jpg ADDED
static/image.png ADDED
static/logow.png ADDED
templates/chat.html ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>ADAFSA RAG Chatbot Demo</title>
7
+
8
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
9
+ <link rel="preconnect" href="https://fonts.gstatic.com">
10
+ <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
11
+ <link href="https://fonts.googleapis.com/css2?family=Urbanist:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
12
+ <link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">
13
+ <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
14
+
15
+ <script src="https://cdn.tailwindcss.com"></script>
16
+
17
+ <style>
18
+ *{
19
+ direction: rtl;
20
+ }
21
+ .main-chat-container{
22
+ position: relative;
23
+ background-color: rgb(0, 0, 0, 0.4);
24
+ font-family: "DM Sans", sans-serif;
25
+ }
26
+
27
+ .chat-container-wrapper{
28
+ width: 100%;
29
+ height: 100vh;
30
+ background-color: white;
31
+ border: 1px solid rgb(221, 221, 221);
32
+ display: flex;
33
+ flex-direction: column;
34
+ box-shadow: 0px 0px 15px 8px rgba(255,255,255,0.1);
35
+ z-index: 9999;
36
+ /* border: 10 px solid red; */
37
+ font-family: "DM Sans", sans-serif;
38
+ }
39
+
40
+ /* @media(max-width: 800px){
41
+ .chat-container-wrapper{
42
+ width: calc(100% - 30px);
43
+ margin: auto;
44
+ }
45
+ } */
46
+
47
+ .chat-container-wrapper * {
48
+ font-family: "DM Sans", sans-serif;
49
+ }
50
+
51
+ /* .chat-container-wrapper .chatbot-header{
52
+ display: flex;
53
+ flex-direction: row;
54
+ align-items: center;
55
+ justify-content: space-between;
56
+ } */
57
+
58
+ .chat-container-wrapper a{
59
+ color: rgb(11, 172, 235);
60
+ text-decoration:underline;
61
+ }
62
+
63
+ .d-none{
64
+ display: none;
65
+ }
66
+ .main-chat-container{
67
+ transition: opacity 0.5s ease-in-out;
68
+ }
69
+
70
+ /* Hide scrollbar for Chrome, Safari and Opera */
71
+ .styled-scrolllbar::-webkit-scrollbar {
72
+ display: none;
73
+ }
74
+
75
+ /* Hide scrollbar for IE, Edge and Firefox */
76
+ ::-webkit-scrollbar {
77
+ width: 6px;
78
+ border-radius: 5px;
79
+ }
80
+
81
+ /* Track */
82
+ ::-webkit-scrollbar-track {
83
+ background: #f1f1f1;
84
+ }
85
+
86
+ /* Handle */
87
+ ::-webkit-scrollbar-thumb {
88
+ background: #888;
89
+ border-radius: 10px;
90
+ }
91
+
92
+ /* Handle on hover */
93
+ ::-webkit-scrollbar-thumb:hover {
94
+ background: #555;
95
+ }
96
+
97
+ /* Hide scrollbar for Chrome, Safari and Opera */
98
+ .hide-scrolllbar::-webkit-scrollbar {
99
+ display: none;
100
+ }
101
+
102
+ /* Hide scrollbar for IE, Edge and Firefox */
103
+ .hide-scrolllbar {
104
+ -ms-overflow-style: none; /* IE and Edge */
105
+ scrollbar-width: none; /* Firefox */
106
+ }
107
+
108
+ .question-item:hover{
109
+ background-color: rgb(245, 245, 245);
110
+ }
111
+
112
+ .sendbtn{
113
+ background-color: black;
114
+ transition: all 0.2s ease-in-out;
115
+ animation: scale-out 0.1s;
116
+ }
117
+
118
+ .sendbtn:hover{
119
+ color: white;
120
+ scale: 1.1;
121
+ }
122
+ .sendbtn:hover svg{
123
+ stroke: white
124
+ }
125
+
126
+ div[contenteditable]:empty:before {
127
+ content: attr(data-placeholder);
128
+ display: inline-block;
129
+ pointer-events: none;
130
+ }
131
+
132
+ /* div[contenteditable]:focus:empty:before {
133
+ content: 'Start typing';
134
+ } */
135
+ /* HTML: <div class="loader"></div> */
136
+ /* HTML: <div class="loader"></div> */
137
+ .chat-loader {
138
+ display: flex;
139
+ align-items: center;
140
+ gap: 6px;
141
+ padding: 8px 10px;
142
+ }
143
+ .chat-loader .dot {
144
+ width: 8px;
145
+ aspect-ratio: 1;
146
+ border-radius: 50%;
147
+ background-color: gray;
148
+ animation: l5 0.6s ease-in-out infinite;
149
+ }
150
+ .chat-loader .dot:nth-child(1){
151
+ animation-delay: 0s;
152
+ }
153
+ .chat-loader .dot:nth-child(2){
154
+ animation-delay: 0.2s;
155
+ }
156
+ .chat-loader .dot:nth-child(3){
157
+ animation-delay: 0.3s;
158
+ }
159
+ .message-content *{
160
+ width: 100%;
161
+ font-size: 16px;
162
+ }
163
+ .neumorphic-box-shadow{
164
+ border-radius: 50px;
165
+ background: #e0e0e0;
166
+ box-shadow: 20px 20px 17px #cecece,
167
+ -20px -20px 17px #f2f2f2;
168
+ }
169
+
170
+ @keyframes l5 {
171
+ 0% {scale: 0.2; opacity: 0.2;}
172
+ 33% {scale: 0.5; opacity: 0.5;}
173
+ 66% {scale: 0.7; opacity: 0.7;}
174
+ 100%{scale: 1; opacity: 1; }
175
+ }
176
+ .pulse {
177
+ animation: pulse-animation 2s infinite;
178
+ }
179
+
180
+ @keyframes pulse-animation {
181
+ 0% {
182
+ scale: 0.9;
183
+ box-shadow: 0 0 0 0px rgba(0, 0, 0, 0.2);
184
+ }
185
+ 50%{
186
+ scale: 1
187
+ }
188
+ 100% {
189
+ scale: 0.9;
190
+ box-shadow: 0 0 0 20px rgba(0, 0, 0, 0);
191
+ }
192
+ }
193
+
194
+ #userwayAccessibilityIcon{
195
+ display: none;
196
+ }
197
+
198
+
199
+ /*small{*/
200
+ /*font-size: 12px;*/
201
+ /*}*/
202
+
203
+ .bg-special-secondary{
204
+ background-color: rgb(230, 230, 230);
205
+ }
206
+ .message-content *:not(i){
207
+ font-family: "DM Sans", sans-serif;
208
+ }
209
+
210
+ .message-content *:not(h1,h2,h3,h4,h5,h6){
211
+ font-weight: 500;
212
+ }
213
+
214
+ .message-content strong{
215
+ color: black;
216
+ font-weight: bold;
217
+ }
218
+ .message-content h3:not(:first-of-type){
219
+ margin: 18px 0px 10px 0px;
220
+ color: black;
221
+ }
222
+ .message-content h4{
223
+ margin: 15px 0px 8px 0px;
224
+ }
225
+ .message-content h5, .message-content h6{
226
+ margin: 12px 0px 6px 0px;
227
+ }
228
+ .message-content ol, .message-content ul{
229
+ margin: 18px 0px;
230
+ }
231
+ .message-content ol li, .message-content ul li{
232
+ margin: 12px 0px ;
233
+ }
234
+ .message-content p{
235
+ margin-bottom: 18px;
236
+ text-wrap: pretty;
237
+ }
238
+ .message-content a{
239
+ text-wrap: pretty;
240
+ }
241
+ .loading-text{
242
+ position: relative;
243
+ }
244
+ .loading-text::after{
245
+ content: '....';
246
+ animation: loading-text-animation 1s infinite;
247
+ }
248
+ @keyframes loading-text-animation {
249
+ 0%{
250
+ content: ''
251
+ }
252
+ 25%{
253
+ content: '.'
254
+ }
255
+ 50%{
256
+ content: '..'
257
+ }
258
+ 75%{
259
+ content: '...'
260
+ }
261
+ 100%{
262
+ content: '....'
263
+ }
264
+ }
265
+
266
+ *{
267
+ unicode-bidi:bidi-override;
268
+ direction: RTL;
269
+ }
270
+
271
+ </style>
272
+
273
+
274
+ </head>
275
+ <body>
276
+ <div class="main-chat-container show-chatbot flex items-center justify-center">
277
+ <div class="w-full chat-container-wrapper hide-scrolllbar p-sm-4 p-3 relative">
278
+ <div class="max-w-5xl mx-auto chatbot-header w-full flex flex-row-reverse items-center justify-between pb-2" style="direction: ltr;">
279
+ <div style="display: flex; align-items: center; gap: 12px;">
280
+ <img src="./static/logow.png" class="w-52" alt="logo">
281
+ <!-- <span class="text-dark mb-0 chatbot-title" style="font-weight: 800; font-size: 20px; direction: ltr;">ADAFSA Chatbot</span> -->
282
+ </div>
283
+ <div class="flex items-center" style="gap: 5px;">
284
+ <button class="clearBtn flex items-center justify-center gap-1 px-4 py-2 bg-black rounded-full" type="button" style="direction: ltr;">
285
+ <span class="flex items-center justify-center" style="width: 20px; height: 20px;">
286
+ <svg width="24px" height="24px" viewBox="-3.2 -3.2 22.40 22.40" xmlns="http://www.w3.org/2000/svg" fill="#050505" stroke="#ffffff" stroke-width="0.064"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path fill="#ffffff" d="M13.9907,1.31133017e-07 C14.8816,1.31133017e-07 15.3277,1.07714 14.6978,1.70711 L13.8556,2.54922 C14.421,3.15654 14.8904,3.85028 15.2448,4.60695 C15.8028,5.79836 16.0583,7.109 15.9888,8.42277 C15.9193,9.73654 15.5268,11.0129 14.8462,12.1388 C14.1656,13.2646 13.2178,14.2053 12.0868,14.8773 C10.9558,15.5494 9.67655,15.9322 8.3623,15.9918 C7.04804,16.0514 5.73937,15.7859 4.55221,15.2189 C3.36505,14.652 2.33604,13.8009 1.55634,12.7413 C0.776635,11.6816 0.270299,10.446 0.0821822,9.14392 C0.00321229,8.59731 0.382309,8.09018 0.928918,8.01121 C1.47553,7.93224 1.98266,8.31133 2.06163,8.85794 C2.20272,9.83451 2.58247,10.7612 3.16725,11.556 C3.75203,12.3507 4.52378,12.989 5.41415,13.4142 C6.30452,13.8394 7.28602,14.0385 8.27172,13.9939 C9.25741,13.9492 10.2169,13.6621 11.0651,13.158 C11.9133,12.6539 12.6242,11.9485 13.1346,11.1041 C13.6451,10.2597 13.9395,9.30241 13.9916,8.31708 C14.0437,7.33175 13.8521,6.34877 13.4336,5.45521 C13.178,4.90949 12.8426,4.40741 12.4402,3.96464 L11.7071,4.69779 C11.0771,5.32776 9.99996,4.88159 9.99996,3.99069 L9.99996,1.31133017e-07 L13.9907,1.31133017e-07 Z M1.499979,4 C2.05226,4 2.499979,4.44772 2.499979,5 C2.499979,5.55229 2.05226,6 1.499979,6 C0.947694,6 0.499979,5.55228 0.499979,5 C0.499979,4.44772 0.947694,4 1.499979,4 Z M3.74998,1.25 C4.30226,1.25 4.74998,1.69772 4.74998,2.25 C4.74998,2.80229 4.30226,3.25 3.74998,3.25 C3.19769,3.25 2.74998,2.80228 2.74998,2.25 C2.74998,1.69772 3.19769,1.25 3.74998,1.25 Z M6.99998,0 C7.55226,0 7.99998,0.447716 7.99998,1 C7.99998,1.55229 7.55226,2 6.99998,2 C6.44769,2 5.99998,1.55229 5.99998,1 C5.99998,0.447716 6.44769,0 6.99998,0 Z"></path> </g></svg>
287
+ </span>
288
+ <div class="text-white" style="direction: ltr;">Clear</div>
289
+ </button>
290
+
291
+ </div>
292
+ </div>
293
+
294
+ <hr>
295
+
296
+
297
+ <div class="relative chat-container w-full mx-auto hide-scrolllbar rounded-xl mb-2" style="max-width: 800px; flex-grow: 1; padding: 20px 0px; display: flex; flex-direction: column; gap: 10px; text-wrap: pretty; overflow-y: auto; overflow-x: hidden;">
298
+ <div class="start-block load-chat-block w-full flex-grow flex flex-col items-center justify-center bg-white z-10">
299
+ <div class="w-full flex-grow flex flex-col items-center justify-center y-4 text-center">
300
+ <img src="./static/bg-image.jpg" class="w-1/2" alt="adafsa-logo">
301
+ <h1 class="text-5xl font-semibold mb-4" style="direction: ltr;">
302
+ ADAFSA Chatbot Demo
303
+ </h1>
304
+ <h3 class="max-w-xl text-xl text-center text-dark font-medium d-block mx-auto mb-3">
305
+ ابدأ بطرح أسئلتك وسأجيب عليها أو يمكنك استخدام الأسئلة التالية كنقطة بداية
306
+ </h3>
307
+ </div>
308
+ <div class="w-full flex-end mx-auto" style="max-width: 800px;">
309
+ <div class="grid md:grid-cols-3 grid-cols-2 m-0 p-0">
310
+
311
+
312
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
313
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
314
+ الموسم المناسب لزراعة الذرة العلفية ؟
315
+ </small>
316
+ </div>
317
+
318
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
319
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
320
+ ما هي الاحتياجات المائية لتربية الحيوانات؟
321
+ </small>
322
+ </div>
323
+
324
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
325
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
326
+ ما كمية أعلاف الجت المستلمة في منطقة الظفرة عام <span class="inline-block" style="direction: ltr;">2022</span>
327
+ </small>
328
+ </div>
329
+
330
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
331
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
332
+ الموسم المناسب لزراعة الطماطم في الحقل المكشوف بدولة الإمارات؟
333
+ </small>
334
+ </div>
335
+
336
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
337
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
338
+ ما هي خطوات إنتاج الشتلات؟
339
+ </small>
340
+ </div>
341
+
342
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
343
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
344
+ شروط اختيار مكان منحل العسل؟
345
+ </small>
346
+ </div>
347
+
348
+
349
+
350
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
351
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
352
+ ما هو تقييم مطعم قصر نجد؟
353
+ </small>
354
+ </div>
355
+
356
+
357
+
358
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
359
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
360
+ ما مساحات المزارع المروية بالتنقيط في منطقة الرحبة عام 0202
361
+ </small>
362
+ </div>
363
+
364
+
365
+ <div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
366
+ <small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 flex items-center">
367
+ ما مساحات المزارع المروية بالتنقيط في منطقة أبوظبي عام 0202
368
+ </small>
369
+ </div>
370
+ </div>
371
+ </div>
372
+ </div>
373
+
374
+ <div class="absolute w-full h-full top-0 left-0 z-0 flex items-center justify-center">
375
+ <img src="./static/image.png" class="opacity-10" alt="greyscale-image">
376
+ </div>
377
+
378
+ </div>
379
+ <div class="w-full bg-transparent">
380
+ <div class="input-options-container max-w-3xl p-3 flex flex-row items-center justify-between border rounded-full shadow-xl mx-auto">
381
+ <div id="question-box" dir="rtl" class="inline-block px-4 text-2xl" role="textbox" contenteditable="true" data-placeholder="أكتب سؤالك هنا..." style="width: 100%; max-height: 100px; outline: none; font-weight: 500; font-size: 14px; direction: RTL;"></div>
382
+ <button class="sendbtn " style="width: 40px; height: 40px; padding: 8px; aspect-ratio: 1; display: flex; align-items: center; justify-content: center; border-radius: 30px; border: none; color: white;">
383
+ <svg width="30px" height="30px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 6V18M12 6L7 11M12 6L17 11" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
384
+ </button>
385
+ </div>
386
+ </div>
387
+ </div>
388
+ </div>
389
+
390
+ <!-- SocketIO -->
391
+ <script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script>
392
+ <!-- Showdown.js -->
393
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js" integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
394
+
395
+ <script>
396
+
397
+ // Initialize Socket.IO client
398
+ var socket = io.connect('http://127.0.0.1:5000/', {
399
+ transports: ['websocket']
400
+ });
401
+
402
+ setInterval(()=>{
403
+ socket.emit('ping', { "message": "I am alive!"});
404
+ }, 30000)
405
+
406
+ // Empty response variable to keep track of response
407
+ var response="";
408
+
409
+ // Converter for Markdown to HTML
410
+ var converter = new showdown.Converter();
411
+
412
+ // Function to load a chat block indicating that the system is searching
413
+ function loadChatBlock(){
414
+ let lastMessageElement = $(".chat-container div:last-child");
415
+ if(lastMessageElement.hasClass("response-block")){
416
+ return;
417
+ }
418
+ const isRTL = true;
419
+ $(".chat-container").append(
420
+ `
421
+ <div class="chat-block load-chat-block w-full flex flex-row items-center 'justify-start bg-secondary p-3 rounded-xl z-10" style="background-color: rgba(242, 255, 225, 0.678);">
422
+ <div style="width: fit-content; margin-bottom: 5px; display: flex; align-items: center; gap: 15px;">
423
+ <span class="p-2 border-2" style="display: flex; align-items: center; justify-content: center; border-radius: 50px; background-color: white;">
424
+ <svg fill="#000000" width="24px" height="24px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path d="M21.928 11.607c-.202-.488-.635-.605-.928-.633V8c0-1.103-.897-2-2-2h-6V4.61c.305-.274.5-.668.5-1.11a1.5 1.5 0 0 0-3 0c0 .442.195.836.5 1.11V6H5c-1.103 0-2 .897-2 2v2.997l-.082.006A1 1 0 0 0 1.99 12v2a1 1 0 0 0 1 1H3v5c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5a1 1 0 0 0 1-1v-1.938a1.006 1.006 0 0 0-.072-.455zM5 20V8h14l.001 3.996L19 12v2l.001.005.001 5.995H5z"></path><ellipse cx="8.5" cy="12" rx="1.5" ry="2"></ellipse><ellipse cx="15.5" cy="12" rx="1.5" ry="2"></ellipse><path d="M8 16h8v2H8z"></path></g></svg>
425
+ </span>
426
+ </div>
427
+ <div class="px-3 py-1 order-1">
428
+ <div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; font-weight: 500; text-wrap: pretty; text-align: right;">
429
+ <span class="loading-text">البحث</span>
430
+ </div>
431
+ </div>
432
+ </div>
433
+ `
434
+ );
435
+ }
436
+
437
+ function appendQuestion(question) {
438
+ if (!$(".start-block").hasClass("d-none")) {
439
+ $(".start-block").slideUp(200);
440
+ $(".start-block").addClass("d-none");
441
+ $(".start-block").removeClass("flex");
442
+ }
443
+
444
+ const isRTL = true;
445
+
446
+ $(".chat-container").append(`
447
+ <div class="chat-block question-block w-full flex flex-row-reverse items-center justify-end p-3 rtl z-10" style="background-color: rgba(255, 255, 255, 0.529);">
448
+ <div class="flex items-center order-2">
449
+ <span style="width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50px; padding: 3px; border: 2px solid #c2c2c2;">
450
+ <svg fill="#000000" width="36px" height="36px" viewBox="0 0 256 256" id="Flat" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M128,76a44,44,0,1,1-44,44A43.99983,43.99983,0,0,1,128,76Zm48-12h16V80a8,8,0,0,0,16,0V64h16a8,8,0,0,0,0-16H208V32a8,8,0,0,0-16,0V48H176a8,8,0,0,0,0,16Zm45.56006,40.95605a8.00039,8.00039,0,0,0-6.64893,9.15381A89.00044,89.00044,0,0,1,216,128a87.63672,87.63672,0,0,1-22.24182,58.41016,79.7044,79.7044,0,0,0-24.431-22.97461,59.83641,59.83641,0,0,1-82.6543,0A79.70345,79.70345,0,0,0,62.2417,186.41016,87.9498,87.9498,0,0,1,128,40a89.02966,89.02966,0,0,1,13.89062,1.08887,7.99994,7.99994,0,1,0,2.50391-15.80274A104.0826,104.0826,0,0,0,24,128a103.74716,103.74716,0,0,0,33.81934,76.68066,7.94507,7.94507,0,0,0,1.32629,1.18946,103.784,103.784,0,0,0,137.71252-.00293,7.94633,7.94633,0,0,0,1.31678-1.18115A103.74751,103.74751,0,0,0,232,128a105.04749,105.04749,0,0,0-1.28613-16.39453A7.99752,7.99752,0,0,0,221.56006,104.95605Z"></path> </g></svg>
451
+ </span>
452
+ </div>
453
+ <div class="px-3 order-1">
454
+ <div class="message-content pr-2" style="width: 100%; margin: auto; text-wrap: pretty; font-size: 14px; text-align: right;">
455
+ <span> ${question} </span>
456
+ </div>
457
+ </div>
458
+ </div>
459
+ `);
460
+
461
+ $(".chat-container").animate({
462
+ scrollTop: $('.chat-container')[0].scrollHeight
463
+ }, 1000);
464
+ }
465
+
466
+ // Function to handle sending a question via Socket.IO and receiving an answer
467
+ function provideQuestionToAnswer(){
468
+
469
+ response = "";
470
+ let question = $("#question-box").text();
471
+ appendQuestion(question);
472
+ $("#question-box").text("");
473
+
474
+ setTimeout(loadChatBlock, 600);
475
+
476
+ // // Emit question via Socket.IO
477
+ // socket.emit('message', { question: question, session_id: 'abc123' });
478
+
479
+ // Emit question and language via Socket.IO
480
+ socket.emit('message', { question: question, session_id: 'abc123' });
481
+ }
482
+
483
+ function appendAnswer(answer) {
484
+ let lastElement = $(".chat-container .chat-block:last-child");
485
+ const isRTL = true;
486
+
487
+ if (lastElement.hasClass("response-block")) {
488
+ $(".chat-container .chat-block:last-child").find(".message-content").html(answer);
489
+ } else {
490
+ lastElement.remove();
491
+ $(".chat-container").append(`
492
+ <div class="chat-block response-block w-full flex flex-row-reverse items-start justify-end bg-secondary p-3 rounded-xl rtl z-10" style="background-color: rgba(242, 255, 225, 0.529);">
493
+ <div class="flex items-center order-2" >
494
+ <span class="bg-white" style="width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50px; padding: 5px; border: 2px solid #c2c2c2;">
495
+ <svg fill="#000000" width="36px" height="36px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path d="M21.928 11.607c-.202-.488-.635-.605-.928-.633V8c0-1.103-.897-2-2-2h-6V4.61c.305-.274.5-.668.5-1.11a1.5 1.5 0 0 0-3 0c0 .442.195.836.5 1.11V6H5c-1.103 0-2 .897-2 2v2.997l-.082.006A1 1 0 0 0 1.99 12v2a1 1 0 0 0 1 1H3v5c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5a1 1 0 0 0 1-1v-1.938a1.006 1.006 0 0 0-.072-.455zM5 20V8h14l.001 3.996L19 12v2l.001.005.001 5.995H5z"></path><ellipse cx="8.5" cy="12" rx="1.5" ry="2"></ellipse><ellipse cx="15.5" cy="12" rx="1.5" ry="2"></ellipse><path d="M8 16h8v2H8z"></path></g></svg>
496
+ </span>
497
+ </div>
498
+ <div class="px-3 py-1">
499
+ <div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; font-weight: 500; text-wrap: pretty; text-align: right;" style="-webkit-locale: "ar";" >
500
+ ${answer}
501
+ </div>
502
+ </div>
503
+ </div>
504
+ `);
505
+ }
506
+
507
+ $(".chat-container").scrollTop($(".chat-container")[0].scrollHeight);
508
+ }
509
+
510
+
511
+ // Function to handle the answer received from the server
512
+ socket.on('response', (data) => {
513
+ response += data;
514
+ model_response = converter.makeHtml(response);
515
+ appendAnswer(model_response);
516
+ });
517
+
518
+ // Open the chatbot interface
519
+ function openchatbot(){
520
+ console.log("working here");
521
+ $(".main-chat-container").removeClass("hide-chatbot");
522
+ $(".main-chat-container").addClass("show-chatbot");
523
+ $(".startchatbtn").addClass("d-none");
524
+ $(".startchatbtn").removeClass("flex");
525
+ }
526
+
527
+ // Close the chatbot interface
528
+ $(".startchatbtn").click(openchatbot);
529
+
530
+ $(".closechatbot").click(function(){
531
+ $(".main-chat-container").removeClass("show-chatbot");
532
+ $(".main-chat-container").addClass("hide-chatbot");
533
+ $(".startchatbtn").removeClass("d-none");
534
+ $(".startchatbtn").addClass("flex");
535
+ });
536
+
537
+ // Send the question and get the answer
538
+ $(".sendbtn").click(provideQuestionToAnswer);
539
+
540
+ // Handle the Enter key press in the question box
541
+ $(document).ready(() => {
542
+ $("#question-box").text("Write your question here...");
543
+ $("#question-box").text("");
544
+
545
+ $('#question-box').on('keydown', function(event) {
546
+ if (event.key === 'Enter') {
547
+ event.preventDefault();
548
+ provideQuestionToAnswer();
549
+ }
550
+ });
551
+ });
552
+
553
+ // Handle connection errors
554
+ socket.on('connect_error', (error) => {
555
+ console.error("Connection error:", error);
556
+ appendMessage("Sorry, there was a problem connecting to the server. Please try again later.");
557
+ });
558
+
559
+ // // Handle disconnection
560
+ // socket.on('connect', (reason) => {
561
+ // // Emit website category via Socket.IO
562
+ // socket.emit('website', { website: "UAE_Legislation", session_id: 'abc123' });
563
+ // });
564
+
565
+ // Handle disconnection
566
+ socket.on('disconnect', (reason) => {
567
+ console.warn("Disconnected from server:", reason);
568
+ response = "";
569
+ // appendAnswer("You have been disconnected from the server. Please refresh the page to reconnect.");
570
+ socket = io.connect('http://127.0.0.1:5000/', {
571
+ transports: ['websocket']
572
+ });
573
+ });
574
+
575
+ // Clear the chat container
576
+ $(".clearBtn").click(() => {
577
+ $('.chat-container').find('.chat-block').remove();
578
+ $(".start-block").removeClass('d-none');
579
+ $(".start-block").addClass('flex');
580
+ $(".start-block").slideDown();
581
+ });
582
+
583
+ // Handle click on example questions
584
+ $(".question-item").on("click", (e) => {
585
+ let exampleQuestion = e.target.innerHTML;
586
+ $("#question-box").text(exampleQuestion);
587
+ $(".sendbtn").click();
588
+ });
589
+ </script>
590
+
591
+ </body>
592
+ </html>