Spaces:
Sleeping
Sleeping
Create scenario_advisor.py
Browse files- scenario_advisor.py +48 -0
scenario_advisor.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from langchain_google_genai import GoogleGenerativeAI
|
4 |
+
|
5 |
+
# Load environment variables
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
def get_scenario_based_response(scenario_description):
|
9 |
+
"""
|
10 |
+
Generate a Constitution of India based legal analysis for a given scenario.
|
11 |
+
"""
|
12 |
+
|
13 |
+
# Get API key from environment or Streamlit secrets
|
14 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
15 |
+
|
16 |
+
if not api_key:
|
17 |
+
return "Error: Google API Key not found. Please configure the API key in the application settings."
|
18 |
+
|
19 |
+
try:
|
20 |
+
llm = GoogleGenerativeAI(model="gemini-1.5-flash", api_key=api_key)
|
21 |
+
|
22 |
+
prompt = f"""
|
23 |
+
You are a Constitutional law expert specializing in the Constitution of India.
|
24 |
+
Analyze the following scenario and provide a factual, unbiased explanation:
|
25 |
+
|
26 |
+
Scenario:
|
27 |
+
{scenario_description}
|
28 |
+
|
29 |
+
Your response should include:
|
30 |
+
1. Relevant Articles, Schedules, or Amendments (cite exact numbers and names).
|
31 |
+
2. Explanation of how they apply to the scenario.
|
32 |
+
3. Possible legal interpretations or outcomes.
|
33 |
+
4. Any relevant Supreme Court or High Court precedents (if applicable).
|
34 |
+
5. Limitations or areas of uncertainty in constitutional interpretation.
|
35 |
+
|
36 |
+
Use clear, simple language so a non-lawyer can understand.
|
37 |
+
Format the answer with clear headings and bullet points.
|
38 |
+
"""
|
39 |
+
|
40 |
+
response = llm.invoke(prompt)
|
41 |
+
return response if isinstance(response, str) else str(response)
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error generating response: {str(e)}"
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
scenario = "A state government passes a law restricting online speech criticizing its ministers, citing maintenance of public order. Is this constitutional?"
|
48 |
+
print(get_scenario_based_response(scenario))
|