Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from chromadb_semantic_search_for_dataset import search_cases
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load small multilingual LLM
|
| 6 |
+
qa_pipeline = pipeline(
|
| 7 |
+
"text2text-generation",
|
| 8 |
+
model="google/flan-t5-small" # Change to a better Nepali-supporting model if needed
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def semantic_search_only(search_text):
|
| 12 |
+
results, _ = search_cases(search_text)
|
| 13 |
+
return results
|
| 14 |
+
|
| 15 |
+
def rag_answer(question, search_text):
|
| 16 |
+
# First get top 5 cases
|
| 17 |
+
_, context = search_cases(search_text)
|
| 18 |
+
|
| 19 |
+
# Build RAG prompt
|
| 20 |
+
prompt = (
|
| 21 |
+
f"तपाईं एक कानूनी सहायक हुनुहुन्छ। तलका केसहरूको जानकारी प्रयोग गरेर प्रश्नको जवाफ दिनुहोस्।\n\n"
|
| 22 |
+
f"सन्दर्भ:\n{context}\n\n"
|
| 23 |
+
f"प्रश्न: {question}\n"
|
| 24 |
+
f"उत्तर:"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Generate answer
|
| 28 |
+
answer = qa_pipeline(prompt, max_length=512)[0]['generated_text']
|
| 29 |
+
return answer
|
| 30 |
+
|
| 31 |
+
# Gradio UI with two sections
|
| 32 |
+
with gr.Blocks() as iface:
|
| 33 |
+
gr.Markdown("# 📚 Semantic Search + RAG for Nepali Legal Cases")
|
| 34 |
+
|
| 35 |
+
with gr.Tab("🔍 Semantic Search"):
|
| 36 |
+
search_box = gr.Textbox(label="Search for a case")
|
| 37 |
+
search_output = gr.Textbox(label="Top 5 Similar Cases")
|
| 38 |
+
search_button = gr.Button("Search")
|
| 39 |
+
search_button.click(fn=semantic_search_only, inputs=search_box, outputs=search_output)
|
| 40 |
+
|
| 41 |
+
with gr.Tab("🤖 Ask a Question (RAG)"):
|
| 42 |
+
rag_query = gr.Textbox(label="Ask your question")
|
| 43 |
+
rag_search_context = gr.Textbox(label="Search for a case (context)")
|
| 44 |
+
rag_output = gr.Textbox(label="LLM Answer")
|
| 45 |
+
rag_button = gr.Button("Get Answer")
|
| 46 |
+
rag_button.click(fn=rag_answer, inputs=[rag_query, rag_search_context], outputs=rag_output)
|
| 47 |
+
|
| 48 |
+
iface.launch()
|