RaniRahbani commited on
Commit
ab4b6cd
·
verified ·
1 Parent(s): c738e2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -2,41 +2,55 @@ 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("unsloth/Llama-3.2-1B-Instruct")
8
 
9
 
10
  def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
- # system_message,
14
- # max_tokens,
15
- # temperature,
16
- # top_p,
17
  ):
18
-
19
- system_message = "You are a Dietician Assistant specializing in providing general guidance on diet, "
20
- "nutrition, and healthy eating habits. Answer questions thoroughly with scientifically "
21
- "backed advice, practical tips, and easy-to-understand explanations. Keep in mind that "
22
- "your role is to assist, not replace a registered dietitian, so kindly remind users to "
23
- "consult a professional for personalized advice when necessary."
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  max_tokens = 512
25
  temperature = 0.7
26
  top_p = 0.95
27
-
 
28
  messages = [{"role": "system", "content": system_message}]
29
 
 
30
  for val in history:
31
  if val[0]:
32
  messages.append({"role": "user", "content": val[0]})
33
  if val[1]:
34
  messages.append({"role": "assistant", "content": val[1]})
35
 
 
36
  messages.append({"role": "user", "content": message})
37
 
38
  response = ""
39
 
 
40
  for message in client.chat_completion(
41
  messages,
42
  max_tokens=max_tokens,
@@ -45,33 +59,15 @@ def respond(
45
  top_p=top_p,
46
  ):
47
  token = message.choices[0].delta.content
48
-
49
  response += token
50
  yield response
51
 
52
 
53
- """
54
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
55
- """
56
  with gr.Blocks() as demo:
57
- chat = gr.ChatInterface(
58
- respond,
59
- # additional_inputs=[
60
- # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
61
- # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
62
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
- # gr.Slider(
64
- # minimum=0.1,
65
- # maximum=1.0,
66
- # value=0.95,
67
- # step=0.05,
68
- # label="Top-p (nucleus sampling)",
69
- # ),
70
- # ],
71
- )
72
- # Send a default message when the interface loads
73
- chat.send_message("Hi there! I'm your Dietician Assistant, here to help you with general advice on diet, nutrition, and healthy eating habits. Let's explore your questions.")
74
-
75
 
76
  if __name__ == "__main__":
77
  demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
 
4
  """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs:
6
+ https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
  client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
9
 
10
 
11
  def respond(
12
  message,
13
+ history: list[tuple[str, str]] = None, # Default history as None to avoid mutable issues
 
 
 
 
14
  ):
15
+ if history is None:
16
+ history = [
17
+ (
18
+ None,
19
+ "Hi there! I'm your Dietician Assistant, here to help with general advice on diet, "
20
+ "nutrition, and healthy eating habits. Let's explore your questions.",
21
+ )
22
+ ]
23
+
24
+ # System message describing the assistant's role
25
+ system_message = (
26
+ "You are a Dietician Assistant specializing in providing general guidance on diet, "
27
+ "nutrition, and healthy eating habits. Answer questions thoroughly with scientifically "
28
+ "backed advice, practical tips, and easy-to-understand explanations. Keep in mind that "
29
+ "your role is to assist, not replace a registered dietitian, so kindly remind users to "
30
+ "consult a professional for personalized advice when necessary."
31
+ )
32
+
33
+ # Define model parameters
34
  max_tokens = 512
35
  temperature = 0.7
36
  top_p = 0.95
37
+
38
+ # Initialize the message history with the system message
39
  messages = [{"role": "system", "content": system_message}]
40
 
41
+ # Add previous history to the message chain
42
  for val in history:
43
  if val[0]:
44
  messages.append({"role": "user", "content": val[0]})
45
  if val[1]:
46
  messages.append({"role": "assistant", "content": val[1]})
47
 
48
+ # Append the new user message
49
  messages.append({"role": "user", "content": message})
50
 
51
  response = ""
52
 
53
+ # Generate the response in a streaming fashion
54
  for message in client.chat_completion(
55
  messages,
56
  max_tokens=max_tokens,
 
59
  top_p=top_p,
60
  ):
61
  token = message.choices[0].delta.content
 
62
  response += token
63
  yield response
64
 
65
 
66
+ # Set up the Gradio ChatInterface with an initial history
 
 
67
  with gr.Blocks() as demo:
68
+ gr.ChatInterface(respond, value=[("None",
69
+ "Hi there! I'm your Dietician Assistant, here to help with general advice on diet, "
70
+ "nutrition, and healthy eating habits. Let's explore your questions.")])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  if __name__ == "__main__":
73
  demo.launch()