Spaces:
Sleeping
Sleeping
error html maker
Browse files- app.py +5 -8
- templates/chat.html +19 -27
app.py
CHANGED
@@ -46,7 +46,7 @@ app.config['SECRET_KEY'] = SECRET_KEY
|
|
46 |
|
47 |
embed_model = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-multilingual-base", model_kwargs={"trust_remote_code":True})
|
48 |
llm = ChatGroq(
|
49 |
-
model="llama-3.1-
|
50 |
temperature=0.0,
|
51 |
max_tokens=1024,
|
52 |
max_retries=2
|
@@ -78,17 +78,14 @@ compression_retriever = ContextualCompressionRetriever(
|
|
78 |
template = """
|
79 |
User Instructions:
|
80 |
|
81 |
-
You are an Arabic AI Assistant focused on providing clear, concise
|
82 |
Always answer truthfully. If the user query is not relevant to the provided CONTEXT, respond stating the reason.
|
83 |
Generate responses in Arabic. Format any English words and numbers appropriately for clarity.
|
84 |
|
85 |
-
|
86 |
-
Use Unicode direction codes to specify the text direction for English words/numbers in response.
|
87 |
-
Use \u202A (Left-to-Right) before and \u202C (Pop Direction Formatting) after any English words or numbers for clarity.
|
88 |
-
|
89 |
-
Round off numbers with decimal integers to two decimals.
|
90 |
|
91 |
Use bullet points or numbered lists where applicable for better organization.
|
|
|
92 |
Provide detailed yet concise answers, covering all important aspects.
|
93 |
Remember, responding outside the CONTEXT may lead to termination of the interaction.
|
94 |
CONTEXT: {context}
|
@@ -129,9 +126,9 @@ def handle_disconnect():
|
|
129 |
@socketio.on('message')
|
130 |
def handle_message(data):
|
131 |
question = data.get('question')
|
132 |
-
print(question)
|
133 |
try:
|
134 |
for chunk in rag_chain.stream(question):
|
|
|
135 |
emit('response', chunk, room=request.sid)
|
136 |
except Exception as e:
|
137 |
emit('response', {"error": "An error occurred while processing your request."}, room=request.sid)
|
|
|
46 |
|
47 |
embed_model = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-multilingual-base", model_kwargs={"trust_remote_code":True})
|
48 |
llm = ChatGroq(
|
49 |
+
model="llama-3.1-8b-instant",
|
50 |
temperature=0.0,
|
51 |
max_tokens=1024,
|
52 |
max_retries=2
|
|
|
78 |
template = """
|
79 |
User Instructions:
|
80 |
|
81 |
+
You are an Arabic AI Assistant focused on providing clear, concise responses.
|
82 |
Always answer truthfully. If the user query is not relevant to the provided CONTEXT, respond stating the reason.
|
83 |
Generate responses in Arabic. Format any English words and numbers appropriately for clarity.
|
84 |
|
85 |
+
Round off numbers with decimal integers to two decimal integers.
|
|
|
|
|
|
|
|
|
86 |
|
87 |
Use bullet points or numbered lists where applicable for better organization.
|
88 |
+
Use <br> tag to break lines and <br><br> for new line.
|
89 |
Provide detailed yet concise answers, covering all important aspects.
|
90 |
Remember, responding outside the CONTEXT may lead to termination of the interaction.
|
91 |
CONTEXT: {context}
|
|
|
126 |
@socketio.on('message')
|
127 |
def handle_message(data):
|
128 |
question = data.get('question')
|
|
|
129 |
try:
|
130 |
for chunk in rag_chain.stream(question):
|
131 |
+
print(chunk)
|
132 |
emit('response', chunk, room=request.sid)
|
133 |
except Exception as e:
|
134 |
emit('response', {"error": "An error occurred while processing your request."}, room=request.sid)
|
templates/chat.html
CHANGED
@@ -332,7 +332,7 @@
|
|
332 |
|
333 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
334 |
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
|
335 |
-
ما كمية أعلاف الجت المستلمة في منطقة الظفرة عام <span class="inline-block"
|
336 |
</small>
|
337 |
</div>
|
338 |
|
@@ -365,15 +365,15 @@
|
|
365 |
|
366 |
|
367 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
368 |
-
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3
|
369 |
-
ما مساحات المزارع المروية بالتنقيط في منطقة الرحبة عام
|
370 |
</small>
|
371 |
</div>
|
372 |
|
373 |
|
374 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
375 |
-
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3
|
376 |
-
ما مساحات المزارع المروية بالتنقيط في منطقة أبوظبي عام
|
377 |
</small>
|
378 |
</div>
|
379 |
</div>
|
@@ -388,7 +388,7 @@
|
|
388 |
|
389 |
<div class="w-full bg-transparent">
|
390 |
<div class="input-options-container max-w-3xl p-3 flex flex-row items-center justify-between border rounded-full shadow-xl mx-auto">
|
391 |
-
<div id="question-box" dir="auto" class="inline-block px-4 text-2xl" contenteditable="true" data-placeholder="أكتب سؤالك هنا..." style="width: 100%; max-height: 100px; outline: none; font-weight: 500; font-size: 14px;"></div>
|
392 |
<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;">
|
393 |
<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>
|
394 |
</button>
|
@@ -456,15 +456,9 @@
|
|
456 |
|
457 |
const englishRegex = /[A-Za-z0-9]+/g;
|
458 |
|
459 |
-
|
460 |
-
// Wrap Arabic text in <span dir="rtl">
|
461 |
-
// content = content.replace(arabicRegex, function(match) {
|
462 |
-
// return `<span dir="rtl">${match}</span>`;
|
463 |
-
// });
|
464 |
-
|
465 |
// Wrap English text and numbers in <span dir="ltr">
|
466 |
formatted_question = question.replace(englishRegex, function(match) {
|
467 |
-
return `<span class="special-span" dir="ltr"
|
468 |
});
|
469 |
|
470 |
$(".chat-container").append(`
|
@@ -506,10 +500,16 @@
|
|
506 |
|
507 |
function appendAnswer(answer) {
|
508 |
let lastElement = $(".chat-container .chat-block:last-child");
|
509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
|
511 |
if (lastElement.hasClass("response-block")) {
|
512 |
-
$(".chat-container .chat-block:last-child").find(".message-content").html(
|
513 |
} else {
|
514 |
lastElement.remove();
|
515 |
$(".chat-container").append(`
|
@@ -521,7 +521,7 @@
|
|
521 |
</div>
|
522 |
<div class="px-3 py-1">
|
523 |
<div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; font-weight: 500; text-wrap: pretty;" style="-webkit-locale: "ar";" >
|
524 |
-
${
|
525 |
</div>
|
526 |
</div>
|
527 |
</div>
|
@@ -535,8 +535,8 @@
|
|
535 |
// Function to handle the answer received from the server
|
536 |
socket.on('response', (data) => {
|
537 |
response += data;
|
538 |
-
model_response = converter.makeHtml(response);
|
539 |
-
appendAnswer(
|
540 |
});
|
541 |
|
542 |
// Open the chatbot interface
|
@@ -606,7 +606,7 @@
|
|
606 |
|
607 |
// Handle click on example questions
|
608 |
$(".question-item").on("click", (e) => {
|
609 |
-
let exampleQuestion = e.target.
|
610 |
$("#question-box").text(exampleQuestion);
|
611 |
$(".sendbtn").click();
|
612 |
});
|
@@ -632,14 +632,6 @@
|
|
632 |
// Regex for English letters and numbers
|
633 |
const englishRegex = /[A-Za-z0-9]+/g;
|
634 |
|
635 |
-
// Escape HTML to prevent injection
|
636 |
-
content = escapeHTML(content);
|
637 |
-
|
638 |
-
// Wrap Arabic text in <span dir="rtl">
|
639 |
-
// content = content.replace(arabicRegex, function(match) {
|
640 |
-
// return `<span dir="rtl">${match}</span>`;
|
641 |
-
// });
|
642 |
-
|
643 |
// Wrap English text and numbers in <span dir="ltr">
|
644 |
content = content.replace(englishRegex, function(match) {
|
645 |
return `<span dir="ltr">${match}</span>`;
|
|
|
332 |
|
333 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
334 |
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
|
335 |
+
ما كمية أعلاف الجت المستلمة في منطقة الظفرة عام <span class="special-span inline-block" dir="ltr"> 2022 </span>
|
336 |
</small>
|
337 |
</div>
|
338 |
|
|
|
365 |
|
366 |
|
367 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
368 |
+
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3 ">
|
369 |
+
ما مساحات المزارع المروية بالتنقيط في منطقة الرحبة عام <span class="special-span inline-block" dir="ltr"> 2020 </span>
|
370 |
</small>
|
371 |
</div>
|
372 |
|
373 |
|
374 |
<div style="cursor: pointer;" class="w-full flex col-6 col-lg-4 p-1">
|
375 |
+
<small style="border-radius: 12px;" class="flex-grow question-item h-100 border px-3 py-3">
|
376 |
+
ما مساحات المزارع المروية بالتنقيط في منطقة أبوظبي عام <span class="special-span inline-block" dir="ltr"> 2020 </span>
|
377 |
</small>
|
378 |
</div>
|
379 |
</div>
|
|
|
388 |
|
389 |
<div class="w-full bg-transparent">
|
390 |
<div class="input-options-container max-w-3xl p-3 flex flex-row items-center justify-between border rounded-full shadow-xl mx-auto">
|
391 |
+
<div id="question-box" dir="auto" class="inline-block px-4 text-2xl overflow-hidden" contenteditable="true" data-placeholder="أكتب سؤالك هنا..." style="width: 100%; max-height: 100px; outline: none; font-weight: 500; font-size: 14px;"></div>
|
392 |
<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;">
|
393 |
<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>
|
394 |
</button>
|
|
|
456 |
|
457 |
const englishRegex = /[A-Za-z0-9]+/g;
|
458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
459 |
// Wrap English text and numbers in <span dir="ltr">
|
460 |
formatted_question = question.replace(englishRegex, function(match) {
|
461 |
+
return `<span class="special-span" dir="ltr"> ${match} </span>`;
|
462 |
});
|
463 |
|
464 |
$(".chat-container").append(`
|
|
|
500 |
|
501 |
function appendAnswer(answer) {
|
502 |
let lastElement = $(".chat-container .chat-block:last-child");
|
503 |
+
|
504 |
+
const englishRegex = /[A-Za-z0-9]+(?:\.[0-9]+)?/g;
|
505 |
+
|
506 |
+
// Wrap English text and numbers in <span dir="ltr">
|
507 |
+
formatted_answer = answer.replace(englishRegex, function(match) {
|
508 |
+
return `<span class="special-span px-1" dir="ltr">${match}</span>`;
|
509 |
+
});
|
510 |
|
511 |
if (lastElement.hasClass("response-block")) {
|
512 |
+
$(".chat-container .chat-block:last-child").find(".message-content").html(formatted_answer);
|
513 |
} else {
|
514 |
lastElement.remove();
|
515 |
$(".chat-container").append(`
|
|
|
521 |
</div>
|
522 |
<div class="px-3 py-1">
|
523 |
<div class="message-content pr-2" style="width: 100%; height: 100%; margin: auto; font-weight: 500; text-wrap: pretty;" style="-webkit-locale: "ar";" >
|
524 |
+
${formatted_answer}
|
525 |
</div>
|
526 |
</div>
|
527 |
</div>
|
|
|
535 |
// Function to handle the answer received from the server
|
536 |
socket.on('response', (data) => {
|
537 |
response += data;
|
538 |
+
// model_response = converter.makeHtml(response);
|
539 |
+
appendAnswer(response);
|
540 |
});
|
541 |
|
542 |
// Open the chatbot interface
|
|
|
606 |
|
607 |
// Handle click on example questions
|
608 |
$(".question-item").on("click", (e) => {
|
609 |
+
let exampleQuestion = e.target.innerText;
|
610 |
$("#question-box").text(exampleQuestion);
|
611 |
$(".sendbtn").click();
|
612 |
});
|
|
|
632 |
// Regex for English letters and numbers
|
633 |
const englishRegex = /[A-Za-z0-9]+/g;
|
634 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
635 |
// Wrap English text and numbers in <span dir="ltr">
|
636 |
content = content.replace(englishRegex, function(match) {
|
637 |
return `<span dir="ltr">${match}</span>`;
|