File size: 3,933 Bytes
8bad1e9
d8f6ddc
8bad1e9
d8f6ddc
8bad1e9
 
 
 
 
 
 
 
 
a68aeb4
d8f6ddc
8bad1e9
a68aeb4
d8f6ddc
 
 
 
 
d08d0e4
7a56033
d8f6ddc
d08d0e4
d8f6ddc
fb5dc61
d8f6ddc
 
 
 
 
 
 
 
 
 
 
a68aeb4
 
733226a
 
 
 
 
 
 
 
d638867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24ebdf
733226a
d08d0e4
7a56033
8bad1e9
 
 
55a66c1
8bad1e9
 
 
 
 
 
 
 
 
 
 
 
 
fb5dc61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
from huggingface_hub import InferenceClient

client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    # Initialize messages with system instructions
    messages = [{"role": "system", "content": system_message}]

    # Add historical conversation
    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})

    # Add the new user message
    messages.append({"role": "user", "content": message})

    response = ""

    # Get the model's response
    for response_chunk in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = response_chunk.choices[0].delta.content
        response += token

    response = response.strip()

    # Enhanced context-specific relevance check
    if is_relevant_to_constitution(response):
        return response

    # If response does not meet relevance criteria
    return "Sorry, I can only provide information related to the Constitution of India. Please ask a question related to the Constitution."

def is_relevant_to_constitution(response):
    # Keywords to check in the response
    relevant_keywords = [
        "constitution", "article", "law", "legal", "rights", "act", "judiciary", 
        "legislature", "executive", "fundamental", "amendment", "provision", 
        "policy", "directive", "supreme court", "high court", "legislation", 
        "government", "election", "parliament", "state", "central", "reform", 
        "citizen", "equality", "democracy", "directive principles", "fundamental duties", 
        "preamble", "enforcement", "federalism", "separation of powers", "justice", 
        "republic", "state legislature", "union territory", "bill", "ordinance", 
        "convention", "charter", "treaty", "declaration", "proclamation", "amendments", 
        "compensation", "grievance", "judicial review", "secularism", "socialism", 
        "pluralism", "sovereignty", "autonomy", "independence", "integrity", "caste", 
        "reservation", "minorities", "discrimination", "fundamental rights", 
        "emergency", "state emergency", "national emergency", "local bodies", 
        "tribunal", "ombudsman", "civil rights", "criminal justice", "human rights"
    ]

    # Check if response contains relevant keywords
    return any(keyword in response.lower() for keyword in relevant_keywords)

# Create the Gradio chat interface
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(value="You are an expert assistant specializing in the Constitution of India. Your role is to provide accurate and detailed information about any part of the Constitution, including articles, amendments, schedules, and related legal concepts. When asked about a specific article, amendment, or legal term, provide a comprehensive explanation. If you are unsure or if the query seems to be about something other than the Constitution of India, do your best to relate it to the Constitution. Ensure that all responses are accurate, clear, and directly relevant to the question. If an article, amendment, or term is mentioned, assume it exists and provide the best possible explanation or details about it. ", label="System message", visible=False),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)

if __name__ == "__main__":
    demo.launch()