Nischal Subedi
commited on
Commit
·
4ebf92d
1
Parent(s):
b14d947
UI x
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import re
|
|
| 7 |
import gradio as gr
|
| 8 |
try:
|
| 9 |
# Assuming vector_db.py exists in the same directory or is installed
|
| 10 |
-
from vector_db import VectorDatabase
|
| 11 |
except ImportError:
|
| 12 |
print("Error: Could not import VectorDatabase from vector_db.py.")
|
| 13 |
print("Please ensure vector_db.py exists in the same directory and is correctly defined.")
|
|
@@ -755,14 +755,14 @@ Answer:"""
|
|
| 755 |
# --- Section 3: Query Input and State Selection Card ---
|
| 756 |
with gr.Group(elem_classes="dashboard-card-section"):
|
| 757 |
gr.Markdown("<h3 class='sub-section-title'>Ask Your Question</h3>") # Centered by CSS
|
| 758 |
-
with gr.Row(elem_classes="input-row"):
|
| 759 |
with gr.Column(elem_classes="input-field", scale=3):
|
| 760 |
query_input = gr.Textbox(
|
| 761 |
label="Question", placeholder="E.g., What are the rules for security deposit returns in my state?",
|
| 762 |
lines=5, max_lines=10,
|
| 763 |
elem_classes=["input-field-group"]
|
| 764 |
)
|
| 765 |
-
with gr.Column(elem_classes="input-field", scale=1):
|
| 766 |
state_input = gr.Dropdown(
|
| 767 |
label="Select State", choices=dropdown_choices, value=initial_value,
|
| 768 |
allow_custom_value=False,
|
|
@@ -777,7 +777,7 @@ Answer:"""
|
|
| 777 |
gr.Markdown("<h3 class='sub-section-title'>Legal Assistant's Response</h3>") # Centered by CSS
|
| 778 |
output = gr.Markdown(
|
| 779 |
value="<div class='placeholder output-card'>The answer will appear here after submitting your query.</div>",
|
| 780 |
-
elem_classes="output-content-wrapper output-card"
|
| 781 |
)
|
| 782 |
|
| 783 |
# --- Section 5: Example Questions Section (NO ACCORDION, direct display) ---
|
|
@@ -812,9 +812,9 @@ Answer:"""
|
|
| 812 |
)
|
| 813 |
clear_button.click(
|
| 814 |
fn=lambda: (
|
| 815 |
-
"",
|
| 816 |
-
"",
|
| 817 |
-
initial_value,
|
| 818 |
"<div class='placeholder output-card'>Inputs cleared. Ready for your next question.</div>"
|
| 819 |
),
|
| 820 |
inputs=[], outputs=[api_key_input, query_input, state_input, output]
|
|
@@ -827,7 +827,7 @@ if __name__ == "__main__":
|
|
| 827 |
logging.info("Starting Landlord-Tenant Rights Bot application...")
|
| 828 |
try:
|
| 829 |
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 830 |
-
DEFAULT_PDF_PATH = os.path.join(SCRIPT_DIR, "tenant-landlord.pdf")
|
| 831 |
DEFAULT_DB_PATH = os.path.join(SCRIPT_DIR, "chroma_db")
|
| 832 |
|
| 833 |
PDF_PATH = os.getenv("PDF_PATH", DEFAULT_PDF_PATH)
|
|
@@ -845,21 +845,21 @@ if __name__ == "__main__":
|
|
| 845 |
logging.error(f"FATAL: PDF file at '{PDF_PATH}' exists but is not readable. Check file permissions.")
|
| 846 |
print(f"\n--- PERMISSION ERROR ---\nPDF file ('{os.path.basename(PDF_PATH)}') found but not readable at: {PDF_PATH}\nPlease check file permissions (e.g., using 'chmod +r' in terminal).\n---------------------------\n")
|
| 847 |
exit(1)
|
| 848 |
-
|
| 849 |
logging.info(f"PDF file '{os.path.basename(PDF_PATH)}' found and is readable.")
|
| 850 |
|
| 851 |
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
|
| 852 |
rag = RAGSystem(vector_db=vector_db_instance)
|
| 853 |
-
|
| 854 |
# Ensure vector_db.py has process_and_load_pdf and it's called appropriately
|
| 855 |
# Example: if rag.load_pdf handles it internally based on db content.
|
| 856 |
# If not, you might need to call vector_db_instance.process_and_load_pdf(PDF_PATH) here
|
| 857 |
# For now, assuming rag.load_pdf is sufficient as per original structure.
|
| 858 |
-
rag.load_pdf(PDF_PATH)
|
| 859 |
|
| 860 |
app_interface = rag.gradio_interface()
|
| 861 |
SERVER_PORT = int(os.getenv("PORT", 7860)) # Use PORT env var if on Spaces/Cloud, else 7860
|
| 862 |
-
|
| 863 |
logging.info(f"Launching Gradio app on http://0.0.0.0:{SERVER_PORT}")
|
| 864 |
print(f"\n--- Gradio App Running ---\nAccess at: http://localhost:{SERVER_PORT} or your public Spaces URL\n--------------------------\n")
|
| 865 |
app_interface.launch(server_name="0.0.0.0", server_port=SERVER_PORT, share=False) # share=False is typical for Spaces
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
try:
|
| 9 |
# Assuming vector_db.py exists in the same directory or is installed
|
| 10 |
+
from vector_db import VectorDatabase
|
| 11 |
except ImportError:
|
| 12 |
print("Error: Could not import VectorDatabase from vector_db.py.")
|
| 13 |
print("Please ensure vector_db.py exists in the same directory and is correctly defined.")
|
|
|
|
| 755 |
# --- Section 3: Query Input and State Selection Card ---
|
| 756 |
with gr.Group(elem_classes="dashboard-card-section"):
|
| 757 |
gr.Markdown("<h3 class='sub-section-title'>Ask Your Question</h3>") # Centered by CSS
|
| 758 |
+
with gr.Row(elem_classes="input-row"):
|
| 759 |
with gr.Column(elem_classes="input-field", scale=3):
|
| 760 |
query_input = gr.Textbox(
|
| 761 |
label="Question", placeholder="E.g., What are the rules for security deposit returns in my state?",
|
| 762 |
lines=5, max_lines=10,
|
| 763 |
elem_classes=["input-field-group"]
|
| 764 |
)
|
| 765 |
+
with gr.Column(elem_classes="input-field", scale=1):
|
| 766 |
state_input = gr.Dropdown(
|
| 767 |
label="Select State", choices=dropdown_choices, value=initial_value,
|
| 768 |
allow_custom_value=False,
|
|
|
|
| 777 |
gr.Markdown("<h3 class='sub-section-title'>Legal Assistant's Response</h3>") # Centered by CSS
|
| 778 |
output = gr.Markdown(
|
| 779 |
value="<div class='placeholder output-card'>The answer will appear here after submitting your query.</div>",
|
| 780 |
+
elem_classes="output-content-wrapper output-card"
|
| 781 |
)
|
| 782 |
|
| 783 |
# --- Section 5: Example Questions Section (NO ACCORDION, direct display) ---
|
|
|
|
| 812 |
)
|
| 813 |
clear_button.click(
|
| 814 |
fn=lambda: (
|
| 815 |
+
"",
|
| 816 |
+
"",
|
| 817 |
+
initial_value,
|
| 818 |
"<div class='placeholder output-card'>Inputs cleared. Ready for your next question.</div>"
|
| 819 |
),
|
| 820 |
inputs=[], outputs=[api_key_input, query_input, state_input, output]
|
|
|
|
| 827 |
logging.info("Starting Landlord-Tenant Rights Bot application...")
|
| 828 |
try:
|
| 829 |
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 830 |
+
DEFAULT_PDF_PATH = os.path.join(SCRIPT_DIR, "tenant-landlord.pdf")
|
| 831 |
DEFAULT_DB_PATH = os.path.join(SCRIPT_DIR, "chroma_db")
|
| 832 |
|
| 833 |
PDF_PATH = os.getenv("PDF_PATH", DEFAULT_PDF_PATH)
|
|
|
|
| 845 |
logging.error(f"FATAL: PDF file at '{PDF_PATH}' exists but is not readable. Check file permissions.")
|
| 846 |
print(f"\n--- PERMISSION ERROR ---\nPDF file ('{os.path.basename(PDF_PATH)}') found but not readable at: {PDF_PATH}\nPlease check file permissions (e.g., using 'chmod +r' in terminal).\n---------------------------\n")
|
| 847 |
exit(1)
|
| 848 |
+
|
| 849 |
logging.info(f"PDF file '{os.path.basename(PDF_PATH)}' found and is readable.")
|
| 850 |
|
| 851 |
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
|
| 852 |
rag = RAGSystem(vector_db=vector_db_instance)
|
| 853 |
+
|
| 854 |
# Ensure vector_db.py has process_and_load_pdf and it's called appropriately
|
| 855 |
# Example: if rag.load_pdf handles it internally based on db content.
|
| 856 |
# If not, you might need to call vector_db_instance.process_and_load_pdf(PDF_PATH) here
|
| 857 |
# For now, assuming rag.load_pdf is sufficient as per original structure.
|
| 858 |
+
rag.load_pdf(PDF_PATH)
|
| 859 |
|
| 860 |
app_interface = rag.gradio_interface()
|
| 861 |
SERVER_PORT = int(os.getenv("PORT", 7860)) # Use PORT env var if on Spaces/Cloud, else 7860
|
| 862 |
+
|
| 863 |
logging.info(f"Launching Gradio app on http://0.0.0.0:{SERVER_PORT}")
|
| 864 |
print(f"\n--- Gradio App Running ---\nAccess at: http://localhost:{SERVER_PORT} or your public Spaces URL\n--------------------------\n")
|
| 865 |
app_interface.launch(server_name="0.0.0.0", server_port=SERVER_PORT, share=False) # share=False is typical for Spaces
|