Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -743,12 +743,20 @@ def generate_with_fallback(messages):
|
|
| 743 |
# ══════════════════════════════════════════════════════════
|
| 744 |
# ask_legal
|
| 745 |
# ══════════════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
| 746 |
def ask_legal_core(question: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
original = question
|
| 748 |
question, norm_log = normalize_question(question)
|
| 749 |
|
| 750 |
if not is_legal_question_llm(question):
|
| 751 |
-
|
| 752 |
'model': 'out_of_scope', 'normalized': question}
|
| 753 |
|
| 754 |
queries = rewrite_query(question)
|
|
@@ -806,7 +814,7 @@ def ask_legal_core(question: str) -> dict:
|
|
| 806 |
coverage = calculate_coverage(question, final_docs)
|
| 807 |
|
| 808 |
if len(final_docs) == 0 or coverage < 0.20:
|
| 809 |
-
|
| 810 |
'model': 'quality_check', 'normalized': question}
|
| 811 |
|
| 812 |
context = build_context(final_docs)
|
|
@@ -816,15 +824,18 @@ def ask_legal_core(question: str) -> dict:
|
|
| 816 |
])
|
| 817 |
|
| 818 |
if not answer:
|
| 819 |
-
|
| 820 |
|
| 821 |
-
|
| 822 |
'answer': post_process(answer, final_docs),
|
| 823 |
'sources': [{'law': d.metadata.get('law_name',''), 'article': d.metadata.get('article_number','')} for d in final_docs[:3]],
|
| 824 |
'coverage': round(coverage * 100),
|
| 825 |
'model': model_used,
|
| 826 |
'normalized': question,
|
| 827 |
}
|
|
|
|
|
|
|
|
|
|
| 828 |
|
| 829 |
# ══════════════════════════════════════════════════════════
|
| 830 |
# FastAPI
|
|
@@ -860,11 +871,11 @@ class QuestionResponse(BaseModel):
|
|
| 860 |
|
| 861 |
@app.get('/')
|
| 862 |
def root():
|
| 863 |
-
|
| 864 |
|
| 865 |
@app.get('/health')
|
| 866 |
def health():
|
| 867 |
-
|
| 868 |
'status': 'healthy',
|
| 869 |
'chunks': vectorstore._collection.count() if vectorstore else 0,
|
| 870 |
'groq_models': [m['name'] for m in ACTIVE_MODELS],
|
|
@@ -913,7 +924,7 @@ async def ask(req: QuestionRequest, request: Request):
|
|
| 913 |
|
| 914 |
@app.get('/stats')
|
| 915 |
def get_stats():
|
| 916 |
-
|
| 917 |
'total': stats['total'],
|
| 918 |
'success': stats['success'],
|
| 919 |
'blocked': stats['blocked'],
|
|
@@ -926,7 +937,7 @@ def get_laws():
|
|
| 926 |
law_names = sorted(set(
|
| 927 |
m.get('law_name','') for m in vectorstore.get()['metadatas'] if m.get('law_name')
|
| 928 |
))
|
| 929 |
-
|
| 930 |
|
| 931 |
if __name__ == '__main__':
|
| 932 |
import uvicorn
|
|
|
|
| 743 |
# ══════════════════════════════════════════════════════════
|
| 744 |
# ask_legal
|
| 745 |
# ══════════════════════════════════════════════════════════
|
| 746 |
+
import hashlib
|
| 747 |
+
_answer_cache: Dict = {}
|
| 748 |
+
|
| 749 |
def ask_legal_core(question: str) -> dict:
|
| 750 |
+
cache_key = hashlib.md5(question.strip().encode()).hexdigest()
|
| 751 |
+
if cache_key in _answer_cache:
|
| 752 |
+
logger.info("Cache hit!")
|
| 753 |
+
return _answer_cache[cache_key]
|
| 754 |
+
|
| 755 |
original = question
|
| 756 |
question, norm_log = normalize_question(question)
|
| 757 |
|
| 758 |
if not is_legal_question_llm(question):
|
| 759 |
+
result = {'answer': OUT_OF_SCOPE_RESPONSE, 'sources': [], 'coverage': 0,
|
| 760 |
'model': 'out_of_scope', 'normalized': question}
|
| 761 |
|
| 762 |
queries = rewrite_query(question)
|
|
|
|
| 814 |
coverage = calculate_coverage(question, final_docs)
|
| 815 |
|
| 816 |
if len(final_docs) == 0 or coverage < 0.20:
|
| 817 |
+
result = {'answer': OUT_OF_SCOPE_RESPONSE, 'sources': [], 'coverage': 0,
|
| 818 |
'model': 'quality_check', 'normalized': question}
|
| 819 |
|
| 820 |
context = build_context(final_docs)
|
|
|
|
| 824 |
])
|
| 825 |
|
| 826 |
if not answer:
|
| 827 |
+
result = {'answer': 'كل الموديلات محجوزة حالياً.', 'sources': [], 'coverage': 0, 'model': ''}
|
| 828 |
|
| 829 |
+
result = {
|
| 830 |
'answer': post_process(answer, final_docs),
|
| 831 |
'sources': [{'law': d.metadata.get('law_name',''), 'article': d.metadata.get('article_number','')} for d in final_docs[:3]],
|
| 832 |
'coverage': round(coverage * 100),
|
| 833 |
'model': model_used,
|
| 834 |
'normalized': question,
|
| 835 |
}
|
| 836 |
+
if result['model'] not in ['out_of_scope', 'quality_check', '']:
|
| 837 |
+
_answer_cache[cache_key] = result
|
| 838 |
+
return result
|
| 839 |
|
| 840 |
# ══════════════════════════════════════════════════════════
|
| 841 |
# FastAPI
|
|
|
|
| 871 |
|
| 872 |
@app.get('/')
|
| 873 |
def root():
|
| 874 |
+
result = {'name':'Saudi Legal AI','version':'4.2.0','status':'running','docs':'/docs'}
|
| 875 |
|
| 876 |
@app.get('/health')
|
| 877 |
def health():
|
| 878 |
+
result = {
|
| 879 |
'status': 'healthy',
|
| 880 |
'chunks': vectorstore._collection.count() if vectorstore else 0,
|
| 881 |
'groq_models': [m['name'] for m in ACTIVE_MODELS],
|
|
|
|
| 924 |
|
| 925 |
@app.get('/stats')
|
| 926 |
def get_stats():
|
| 927 |
+
result = {
|
| 928 |
'total': stats['total'],
|
| 929 |
'success': stats['success'],
|
| 930 |
'blocked': stats['blocked'],
|
|
|
|
| 937 |
law_names = sorted(set(
|
| 938 |
m.get('law_name','') for m in vectorstore.get()['metadatas'] if m.get('law_name')
|
| 939 |
))
|
| 940 |
+
result = {'laws': law_names, 'total': len(law_names)}
|
| 941 |
|
| 942 |
if __name__ == '__main__':
|
| 943 |
import uvicorn
|