Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,40 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from modules.lawbot.rag_with_langchain import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
from ui.gradio_ui import get_ui
|
| 5 |
+
from style import custom_css
|
| 6 |
+
from modules.lawbot.rag_with_langchain import (
|
| 7 |
+
load_documents,
|
| 8 |
+
split_documents,
|
| 9 |
+
create_vectorstore,
|
| 10 |
+
setup_rag_chain
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Global variable to hold the chain
|
| 14 |
+
qa_chain = None
|
| 15 |
+
|
| 16 |
+
def initialize_saarthi():
|
| 17 |
+
"""Initialize the RAG pipeline only once."""
|
| 18 |
+
global qa_chain
|
| 19 |
+
if qa_chain is None:
|
| 20 |
+
print("--- Initializing Saarthi Legal Engine ---")
|
| 21 |
+
try:
|
| 22 |
+
documents = load_documents()
|
| 23 |
+
docs = split_documents(documents)
|
| 24 |
+
vectorstore = create_vectorstore(docs)
|
| 25 |
+
qa_chain = setup_rag_chain(vectorstore)
|
| 26 |
+
print("--- Initialization Complete ---")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"❌ Initialization Error: {e}")
|
| 29 |
+
raise e
|
| 30 |
+
return qa_chain
|
| 31 |
+
|
| 32 |
+
# Create the Blocks interface
|
| 33 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 34 |
+
# We call get_ui, but ensure your UI functions call initialize_saarthi()
|
| 35 |
+
# when they need to perform a search.
|
| 36 |
+
get_ui()
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
# Binding to 0.0.0.0 and port 7860 is required for Hugging Face Spaces
|
| 40 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|