yashMahajan commited on
Commit
e6b2aac
·
verified ·
1 Parent(s): 26cfe56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Load your model
5
- model_name = "Atulit23/meta-llama-indian-constitution"
6
- pipe = pipeline("text-generation", model=model_name)
7
 
8
  def respond(
9
  message,
@@ -13,9 +11,13 @@ def respond(
13
  temperature,
14
  top_p,
15
  ):
 
 
16
  messages = [{"role": "system", "content": system_message}]
17
-
18
- for val in history:
 
 
19
  if val[0]:
20
  messages.append({"role": "user", "content": val[0]})
21
  if val[1]:
@@ -23,15 +25,38 @@ def respond(
23
 
24
  messages.append({"role": "user", "content": message})
25
 
26
- response = pipe(messages[-1]["content"], max_length=max_tokens, temperature=temperature, top_p=top_p)
27
- return response[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Create a Gradio ChatInterface
30
  demo = gr.ChatInterface(
31
  respond,
32
  additional_inputs=[
33
- gr.Textbox(value="You are a friendly Chatbot focused on the Indian Constitution.", label="System message"),
34
- gr.Slider(minimum=1, maximum=2048, value=50, step=1, label="Max new tokens"),
35
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
36
  gr.Slider(
37
  minimum=0.1,
@@ -41,8 +66,6 @@ demo = gr.ChatInterface(
41
  label="Top-p (nucleus sampling)",
42
  ),
43
  ],
44
- title="Constitutional Chatbot",
45
- description="Ask questions related to the Indian Constitution."
46
  )
47
 
48
  if __name__ == "__main__":
 
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,
 
11
  temperature,
12
  top_p,
13
  ):
14
+ # Limit the number of historical messages to manage memory
15
+ max_history_length = 10
16
  messages = [{"role": "system", "content": system_message}]
17
+
18
+ # Add recent history
19
+ recent_history = history[-max_history_length:] # Only keep the most recent messages
20
+ for val in recent_history:
21
  if val[0]:
22
  messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
 
25
 
26
  messages.append({"role": "user", "content": message})
27
 
28
+ response = ""
29
+
30
+ try:
31
+ for response_chunk in client.chat_completion(
32
+ messages,
33
+ max_tokens=max_tokens,
34
+ stream=True,
35
+ temperature=temperature,
36
+ top_p=top_p,
37
+ ):
38
+ token = response_chunk.choices[0].delta.content
39
+ response += token
40
+
41
+ # Implement a basic check for relevance
42
+ if not is_constitution_related(response):
43
+ response = "Sorry, I can only answer questions related to the Constitution of India."
44
+
45
+ yield response
46
+
47
+ except MemoryError:
48
+ yield "Error: Memory limit exceeded. Please try again later."
49
+
50
+ def is_constitution_related(response):
51
+ # Perform a simple check to see if the response seems related to the Constitution
52
+ # This can be improved based on specific needs and feedback
53
+ return "constitution" in response.lower() or "article" in response.lower()
54
 
 
55
  demo = gr.ChatInterface(
56
  respond,
57
  additional_inputs=[
58
+ 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"),
59
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
  gr.Slider(
62
  minimum=0.1,
 
66
  label="Top-p (nucleus sampling)",
67
  ),
68
  ],
 
 
69
  )
70
 
71
  if __name__ == "__main__":