amalsp commited on
Commit
5bf4b6c
Β·
verified Β·
1 Parent(s): 907878c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ import pandas as pd
4
+ import random
5
+
6
+ # Load the Indian Law dataset
7
+ print("Loading Indian Law Dataset...")
8
+ ds = load_dataset("viber1/indian-law-dataset")
9
+
10
+ # Convert to pandas for easier manipulation
11
+ df = pd.DataFrame(ds['train'])
12
+
13
+ print(f"Dataset loaded successfully with {len(df)} entries")
14
+ print(f"Dataset columns: {df.columns.tolist()}")
15
+
16
+ # Preview first few entries
17
+ print("\nFirst 3 entries:")
18
+ for i in range(min(3, len(df))):
19
+ print(f"\nEntry {i+1}:")
20
+ for col in df.columns:
21
+ print(f" {col}: {df.iloc[i][col][:100] if isinstance(df.iloc[i][col], str) else df.iloc[i][col]}...")
22
+
23
+ def search_legal_info(question):
24
+ """Search the dataset for relevant legal information based on user question"""
25
+ if not question or len(question.strip()) == 0:
26
+ return "Please enter a legal question."
27
+
28
+ question_lower = question.lower()
29
+ results = []
30
+
31
+ # Search through the dataset
32
+ for idx, row in df.iterrows():
33
+ # Check all text columns for matches
34
+ for col in df.columns:
35
+ if isinstance(row[col], str) and any(word in row[col].lower() for word in question_lower.split()):
36
+ results.append(row.to_dict())
37
+ break
38
+
39
+ if len(results) >= 5: # Limit to top 5 results
40
+ break
41
+
42
+ if not results:
43
+ return "No relevant information found in the dataset. Try rephrasing your question or use different keywords."
44
+
45
+ # Format the response
46
+ response = "πŸ“‹ **Legal Information Found:**\n\n"
47
+ for i, result in enumerate(results, 1):
48
+ response += f"**Result {i}:**\n"
49
+ for key, value in result.items():
50
+ if value and isinstance(value, str):
51
+ # Truncate long text
52
+ display_value = value[:500] + "..." if len(value) > 500 else value
53
+ response += f"- **{key}**: {display_value}\n"
54
+ response += "\n---\n\n"
55
+
56
+ return response
57
+
58
+ def get_random_sample():
59
+ """Get a random entry from the dataset"""
60
+ random_idx = random.randint(0, len(df) - 1)
61
+ sample = df.iloc[random_idx]
62
+
63
+ response = "πŸ“ **Random Dataset Entry:**\n\n"
64
+ for key, value in sample.items():
65
+ if value and isinstance(value, str):
66
+ display_value = value[:500] + "..." if len(value) > 500 else value
67
+ response += f"**{key}**: {display_value}\n\n"
68
+
69
+ return response
70
+
71
+ # Create Gradio interface
72
+ with gr.Blocks(title="Indian Law Q&A Assistant") as demo:
73
+ gr.Markdown("""
74
+ # πŸ›οΈ Indian Law Q&A Assistant
75
+
76
+ Welcome to the Indian Law Question-Answer Assistant powered by the `viber1/indian-law-dataset`.
77
+
78
+ ### How to use:
79
+ 1. Enter your legal question in the text box below
80
+ 2. Click "Search" to find relevant information from the dataset
81
+ 3. Or click "Random Sample" to explore a random entry from the dataset
82
+
83
+ ---
84
+
85
+ ⚠️ **DISCLAIMER**: This application is for **informational purposes only**. The information provided
86
+ is based on a dataset and should NOT be considered as legal advice. Always consult with a qualified
87
+ legal professional for specific legal matters and guidance.
88
+
89
+ ---
90
+ """)
91
+
92
+ with gr.Row():
93
+ with gr.Column():
94
+ question_input = gr.Textbox(
95
+ label="Your Legal Question",
96
+ placeholder="E.g., What are the provisions related to property rights?",
97
+ lines=3
98
+ )
99
+
100
+ with gr.Row():
101
+ search_btn = gr.Button("πŸ” Search", variant="primary")
102
+ random_btn = gr.Button("🎲 Random Sample")
103
+
104
+ output_box = gr.Markdown(label="Response")
105
+
106
+ # Button actions
107
+ search_btn.click(fn=search_legal_info, inputs=question_input, outputs=output_box)
108
+ random_btn.click(fn=get_random_sample, inputs=None, outputs=output_box)
109
+
110
+ gr.Markdown("""
111
+ ---
112
+
113
+ ### Dataset Information:
114
+ - **Dataset**: viber1/indian-law-dataset
115
+ - **Total Entries**: """ + str(len(df)) + """
116
+ - **Columns**: """ + ", ".join(df.columns.tolist()) + """
117
+
118
+ *Built with πŸ’™ using Gradio and Hugging Face Datasets*
119
+ """)
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch()