yashMahajan commited on
Commit
4ca0f56
·
verified ·
1 Parent(s): 903f026

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -1,12 +1,8 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
-
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -27,25 +23,31 @@ def respond(
27
 
28
  response = ""
29
 
30
- for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
  ):
37
- token = message.choices[0].delta.content
38
-
39
  response += token
40
- yield response
41
 
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
 
 
 
 
 
 
 
 
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
  gr.Slider(
@@ -58,6 +60,5 @@ demo = gr.ChatInterface(
58
  ],
59
  )
60
 
61
-
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
 
6
  def respond(
7
  message,
8
  history: list[tuple[str, str]],
 
23
 
24
  response = ""
25
 
26
+ for response_chunk in client.chat_completion(
27
  messages,
28
  max_tokens=max_tokens,
29
  stream=True,
30
  temperature=temperature,
31
  top_p=top_p,
32
  ):
33
+ token = response_chunk.choices[0].delta.content
 
34
  response += token
 
35
 
36
+ # Implement a basic check for relevance
37
+ if not is_constitution_related(response):
38
+ response = "Sorry, I can only answer questions related to the Constitution of India."
39
+
40
+ yield response
41
+
42
+ def is_constitution_related(response):
43
+ # Perform a simple check to see if the response seems related to the Constitution
44
+ # This can be improved based on specific needs and feedback
45
+ return "constitution" in response.lower() or "article" in response.lower()
46
+
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
+ gr.Textbox(value="You are a knowledgeable assistant specializing in the Constitution of India. Only provide answers related to the Constitution. If the question is not related, inform the user accordingly.", label="System message"),
51
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
  gr.Slider(
 
60
  ],
61
  )
62
 
 
63
  if __name__ == "__main__":
64
+ demo.launch()