EN3IMI commited on
Commit
48be6c1
·
verified ·
1 Parent(s): e05be61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
2
- import gradio as gr
3
  import weaviate
4
  import requests
 
5
  from weaviate.classes.init import Auth
6
  from weaviate.classes.query import Rerank
7
  from transformers import AutoTokenizer, pipeline, AutoModelForSeq2SeqLM
@@ -29,6 +30,8 @@ def connect_to_db():
29
  )
30
  return client
31
 
 
 
32
  # ========== تحميل الموديلات ==========
33
  model_checkpoint = "EN3IMI/RouterAraBERT"
34
  tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
@@ -156,7 +159,6 @@ def llm_response_laws(query, docs):
156
 
157
  # ========== النظام الرئيسي ==========
158
  def rag_system(user_input):
159
- client = connect_to_db()
160
  queries, faq_docs = search_for_faq(user_input, client)
161
  use_laws = router_decision(queries)
162
 
@@ -166,10 +168,16 @@ def rag_system(user_input):
166
  law_docs = search_for_laws(user_input, client)
167
  answer = llm_response_laws(user_input, law_docs)
168
 
169
-
170
- client.close()
171
  return answer
172
 
 
 
 
 
 
 
 
 
173
  # ========== واجهة Gradio ==========
174
  with gr.Blocks() as demo:
175
  gr.Markdown("## 🤖 AILS RAG System")
 
1
  import os
2
+ import atexit
3
  import weaviate
4
  import requests
5
+ import gradio as gr
6
  from weaviate.classes.init import Auth
7
  from weaviate.classes.query import Rerank
8
  from transformers import AutoTokenizer, pipeline, AutoModelForSeq2SeqLM
 
30
  )
31
  return client
32
 
33
+ client = connect_to_db()
34
+
35
  # ========== تحميل الموديلات ==========
36
  model_checkpoint = "EN3IMI/RouterAraBERT"
37
  tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
 
159
 
160
  # ========== النظام الرئيسي ==========
161
  def rag_system(user_input):
 
162
  queries, faq_docs = search_for_faq(user_input, client)
163
  use_laws = router_decision(queries)
164
 
 
168
  law_docs = search_for_laws(user_input, client)
169
  answer = llm_response_laws(user_input, law_docs)
170
 
 
 
171
  return answer
172
 
173
+ @atexit.register
174
+ def close_client():
175
+ try:
176
+ client.close()
177
+ print("Weaviate client closed cleanly.")
178
+ except Exception as e:
179
+ print(f"Error closing client: {e}")
180
+
181
  # ========== واجهة Gradio ==========
182
  with gr.Blocks() as demo:
183
  gr.Markdown("## 🤖 AILS RAG System")