yashMahajan commited on
Commit
a68aeb4
·
verified ·
1 Parent(s): f542fca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -11,10 +11,10 @@ def respond(
11
  temperature,
12
  top_p,
13
  ):
14
- # Initialize the messages with the system message internally
15
  messages = [{"role": "system", "content": system_message}]
16
 
17
- # Add history to the messages
18
  for val in history:
19
  if val[0]:
20
  messages.append({"role": "user", "content": val[0]})
@@ -37,22 +37,23 @@ def respond(
37
  token = response_chunk.choices[0].delta.content
38
  response += token
39
 
40
- # Post-process the response to ensure relevance
41
- if "Sorry, I can only answer questions related to the Constitution of India." in response:
42
- return "Sorry, I can only answer questions related to the Constitution of India."
43
- elif "The Constitution of India" in response or "Article" in response:
 
 
 
 
44
  return response
45
- elif "scenario" in message.lower() or "case" in message.lower():
46
- # Assuming the model can interpret and handle scenario-based queries
47
- return "For scenarios related to constitutional law, you can refer to relevant acts and articles in the Constitution of India. For instance, if you were hit by a person, you might refer to laws related to personal safety and rights under the Constitution."
48
- else:
49
- return "Sorry, I can only answer questions related to the Constitution of India."
50
 
51
  # Create the Gradio chat interface
52
  demo = gr.ChatInterface(
53
  respond,
54
  additional_inputs=[
55
- gr.Textbox(value="You are a knowledgeable assistant specializing in the Constitution of India. Answer only questions related to the Constitution, including scenario-based queries. If you find that a question is not relevant, inform the user accordingly.", label="System message", visible=False),
56
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
57
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
58
  gr.Slider(
 
11
  temperature,
12
  top_p,
13
  ):
14
+ # Initialize messages with system instructions
15
  messages = [{"role": "system", "content": system_message}]
16
 
17
+ # Add historical conversation
18
  for val in history:
19
  if val[0]:
20
  messages.append({"role": "user", "content": val[0]})
 
37
  token = response_chunk.choices[0].delta.content
38
  response += token
39
 
40
+ response = response.strip()
41
+
42
+ # Basic filter to determine relevance
43
+ if any(keyword in message.lower() for keyword in ["constitution", "article", "law", "legal", "rights", "act", "judiciary", "legislature", "executive"]):
44
+ return response
45
+
46
+ # If message does not relate to Constitution or is not a scenario
47
+ if "scenario" in message.lower() or "case" in message.lower() or "example" in message.lower():
48
  return response
49
+
50
+ return "Sorry, I am a chatbot specializing in questions related to the Constitution of India. Please ask a relevant question."
 
 
 
51
 
52
  # Create the Gradio chat interface
53
  demo = gr.ChatInterface(
54
  respond,
55
  additional_inputs=[
56
+ gr.Textbox(value="You are a knowledgeable assistant specializing in the Constitution of India. Answer only questions related to the Constitution. If the question is irrelevant, inform the user accordingly.", label="System message", visible=False),
57
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
59
  gr.Slider(