yashMahajan commited on
Commit
d08d0e4
·
verified ·
1 Parent(s): fb5dc61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,7 +1,8 @@
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,
@@ -24,26 +25,32 @@ def respond(
24
  response = ""
25
 
26
  # Get the model's response
27
- for response_chunk in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- token = response_chunk.choices[0].delta.content
35
- response += token
36
-
37
- # Post-process the response to ensure relevance
38
- if "Sorry, I can only answer questions related to the Constitution of India." in response:
39
- response = "Sorry, I can only answer questions related to the Constitution of India."
 
 
 
40
 
41
  yield response
42
 
 
 
 
 
43
  demo = gr.ChatInterface(
44
  respond,
45
  additional_inputs=[
46
- gr.Textbox(value="You are a knowledgeable assistant specializing in the Constitution of India. Answer only questions related to the Constitution. If you find that a question is not relevant, inform the user accordingly.", label="System message"),
47
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
48
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
49
  gr.Slider(
 
1
  import gradio as gr
2
+ from gradio_client import Client
3
 
4
+ # Initialize the client with the new model
5
+ client = Client("nikravan/Bitnet-1B")
6
 
7
  def respond(
8
  message,
 
25
  response = ""
26
 
27
  # Get the model's response
28
+ try:
29
+ result = client.predict(
30
+ api_name="/chat",
31
+ messages=messages,
32
+ max_tokens=max_tokens,
33
+ temperature=temperature,
34
+ top_p=top_p
35
+ )
36
+ response = result['text'] # Adjust based on the actual output format
37
+
38
+ # Post-process the response to ensure relevance
39
+ if not is_constitution_related(response):
40
+ response = "Sorry, I can only answer questions related to the Constitution of India."
41
+
42
+ except Exception as e:
43
+ response = f"Error: {str(e)}"
44
 
45
  yield response
46
 
47
+ def is_constitution_related(response):
48
+ # Perform a simple check to see if the response seems related to the Constitution
49
+ return "constitution" in response.lower() or "article" in response.lower()
50
+
51
  demo = gr.ChatInterface(
52
  respond,
53
  additional_inputs=[
 
54
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
  gr.Slider(