Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import requests
|
|
| 5 |
from weaviate.classes.init import Auth
|
| 6 |
from weaviate.classes.query import Rerank
|
| 7 |
from transformers import AutoTokenizer, pipeline, AutoModelForSeq2SeqLM
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
# ========== إعداد الاتصال مع Weaviate ==========
|
|
@@ -52,10 +53,10 @@ def search_for_laws(user_query, client):
|
|
| 52 |
|
| 53 |
# ========== دوال القرار ==========
|
| 54 |
def router_decision(queries):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
# ========== LLM responses ==========
|
| 61 |
def llm_response_faq(query, docs):
|
|
@@ -68,7 +69,9 @@ def llm_response_faq(query, docs):
|
|
| 68 |
|
| 69 |
system_prompt = """
|
| 70 |
You are an intelligent assistant specialized in the Jordanian Land and Survey Department. Your task is to provide answers strictly based on the context provided from FAQ files.
|
|
|
|
| 71 |
Guidelines:
|
|
|
|
| 72 |
1. Use only the information available in the provided files. Do not hallucinate or invent any information.
|
| 73 |
2. If the provided context does not contain a relevant answer to the user's question, respond with: "I do not know the answer."
|
| 74 |
3. Correct any spelling or typographical errors present in the extracted text from the files.
|
|
@@ -76,7 +79,9 @@ def llm_response_faq(query, docs):
|
|
| 76 |
5. Do not modify the facts or data from the files; respect the sensitivity of the information.
|
| 77 |
6. Focus only on questions related to Jordanian land, survey, and administrative data.
|
| 78 |
7. Answer in the language of the user's question. Most questions will be in Arabic, so prioritize answering in Arabic when possible.
|
|
|
|
| 79 |
Instructions for answering:
|
|
|
|
| 80 |
- First, identify the most relevant FAQ entry based on the user's question.
|
| 81 |
- Then, provide the answer exactly as it appears in the file, fixing only spelling mistakes and minor formatting issues.
|
| 82 |
- AVOID PROVIDIND INORMATION NOT PRESENT IN THE CONTEXT.
|
|
@@ -113,6 +118,7 @@ def llm_response_laws(query, docs):
|
|
| 113 |
system_prompt = """
|
| 114 |
You are an intelligent assistant specialized in Jordanian laws and legislation.
|
| 115 |
Your task is to provide answers strictly based on the context provided from the legal documents.
|
|
|
|
| 116 |
Guidelines:
|
| 117 |
1. Use only the information available in the provided files. Do not hallucinate.
|
| 118 |
2. If the provided context does not contain a relevant answer, respond in Arabic with: "لا أعلم الجواب".
|
|
@@ -143,20 +149,20 @@ def llm_response_laws(query, docs):
|
|
| 143 |
|
| 144 |
|
| 145 |
# ========== النظام الرئيسي ==========
|
| 146 |
-
def rag_system(user_input
|
| 147 |
client = connect_to_db()
|
| 148 |
-
|
| 149 |
queries, faq_docs = search_for_faq(user_input, client)
|
| 150 |
use_laws = router_decision(queries)
|
| 151 |
|
| 152 |
if use_laws:
|
| 153 |
answer = llm_response_faq(user_input, faq_docs)
|
| 154 |
else:
|
| 155 |
-
|
| 156 |
-
answer = llm_response_laws(user_input,
|
|
|
|
| 157 |
|
| 158 |
client.close()
|
| 159 |
-
return answer
|
| 160 |
|
| 161 |
# ========== واجهة Gradio ==========
|
| 162 |
with gr.Blocks() as demo:
|
|
@@ -164,8 +170,7 @@ with gr.Blocks() as demo:
|
|
| 164 |
inp = gr.Textbox(label="اكتب سؤالك")
|
| 165 |
out = gr.Textbox(label="الإجابة")
|
| 166 |
btn = gr.Button("إرسال")
|
| 167 |
-
btn.click(
|
| 168 |
-
|
| 169 |
|
| 170 |
if __name__ == "__main__":
|
| 171 |
-
demo.launch()
|
|
|
|
| 5 |
from weaviate.classes.init import Auth
|
| 6 |
from weaviate.classes.query import Rerank
|
| 7 |
from transformers import AutoTokenizer, pipeline, AutoModelForSeq2SeqLM
|
| 8 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 9 |
|
| 10 |
|
| 11 |
# ========== إعداد الاتصال مع Weaviate ==========
|
|
|
|
| 53 |
|
| 54 |
# ========== دوال القرار ==========
|
| 55 |
def router_decision(queries):
|
| 56 |
+
results = classifier(queries)
|
| 57 |
+
labels = [res['label'] for res in results]
|
| 58 |
+
numeric_labels = [1 if label == 'LABEL_1' else 0 for label in labels]
|
| 59 |
+
return any(numeric_labels)
|
| 60 |
|
| 61 |
# ========== LLM responses ==========
|
| 62 |
def llm_response_faq(query, docs):
|
|
|
|
| 69 |
|
| 70 |
system_prompt = """
|
| 71 |
You are an intelligent assistant specialized in the Jordanian Land and Survey Department. Your task is to provide answers strictly based on the context provided from FAQ files.
|
| 72 |
+
|
| 73 |
Guidelines:
|
| 74 |
+
|
| 75 |
1. Use only the information available in the provided files. Do not hallucinate or invent any information.
|
| 76 |
2. If the provided context does not contain a relevant answer to the user's question, respond with: "I do not know the answer."
|
| 77 |
3. Correct any spelling or typographical errors present in the extracted text from the files.
|
|
|
|
| 79 |
5. Do not modify the facts or data from the files; respect the sensitivity of the information.
|
| 80 |
6. Focus only on questions related to Jordanian land, survey, and administrative data.
|
| 81 |
7. Answer in the language of the user's question. Most questions will be in Arabic, so prioritize answering in Arabic when possible.
|
| 82 |
+
|
| 83 |
Instructions for answering:
|
| 84 |
+
|
| 85 |
- First, identify the most relevant FAQ entry based on the user's question.
|
| 86 |
- Then, provide the answer exactly as it appears in the file, fixing only spelling mistakes and minor formatting issues.
|
| 87 |
- AVOID PROVIDIND INORMATION NOT PRESENT IN THE CONTEXT.
|
|
|
|
| 118 |
system_prompt = """
|
| 119 |
You are an intelligent assistant specialized in Jordanian laws and legislation.
|
| 120 |
Your task is to provide answers strictly based on the context provided from the legal documents.
|
| 121 |
+
|
| 122 |
Guidelines:
|
| 123 |
1. Use only the information available in the provided files. Do not hallucinate.
|
| 124 |
2. If the provided context does not contain a relevant answer, respond in Arabic with: "لا أعلم الجواب".
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
# ========== النظام الرئيسي ==========
|
| 152 |
+
def rag_system(user_input):
|
| 153 |
client = connect_to_db()
|
|
|
|
| 154 |
queries, faq_docs = search_for_faq(user_input, client)
|
| 155 |
use_laws = router_decision(queries)
|
| 156 |
|
| 157 |
if use_laws:
|
| 158 |
answer = llm_response_faq(user_input, faq_docs)
|
| 159 |
else:
|
| 160 |
+
law_docs = search_for_laws(user_input, client)
|
| 161 |
+
answer = llm_response_laws(user_input, law_docs)
|
| 162 |
+
|
| 163 |
|
| 164 |
client.close()
|
| 165 |
+
return answer
|
| 166 |
|
| 167 |
# ========== واجهة Gradio ==========
|
| 168 |
with gr.Blocks() as demo:
|
|
|
|
| 170 |
inp = gr.Textbox(label="اكتب سؤالك")
|
| 171 |
out = gr.Textbox(label="الإجابة")
|
| 172 |
btn = gr.Button("إرسال")
|
| 173 |
+
btn.click(rag_system, inp, out)
|
|
|
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
demo.launch()"
|