MadsGalsgaard commited on
Commit
61d946f
·
verified ·
1 Parent(s): c6b0242

indent error

Browse files
Files changed (1) hide show
  1. app.py +72 -14
app.py CHANGED
@@ -1,3 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
@@ -6,7 +59,6 @@ For more information on `huggingface_hub` Inference API support, please check th
6
  """
7
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
8
 
9
- ## None type
10
  def respond(
11
  message: str,
12
  history: list[tuple[str, str]], # This will not be used
@@ -15,40 +67,46 @@ def respond(
15
  temperature: float,
16
  top_p: float,
17
  ):
 
18
  messages = [{"role": "system", "content": system_message}]
19
-
20
- # Append only the latest user message
21
-
22
-
23
-
24
-
25
-
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
 
30
  try:
31
  # Generate response from the model
32
- for message in client.chat_completion(
33
- messages,
34
  max_tokens=max_tokens,
35
  stream=True,
36
  temperature=temperature,
37
  top_p=top_p,
38
  ):
39
- if message.choices[0].delta.content is not None:
40
- token = message.choices[0].delta.content
41
  response += token
42
  yield response
43
  except Exception as e:
44
  yield f"An error occurred: {e}"
45
 
46
  """
47
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
48
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ],
50
  )
51
 
52
-
53
  if __name__ == "__main__":
54
  demo.launch()
 
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("meta-llama/Meta-Llama-3-8B-Instruct")
8
+
9
+ # ## None type
10
+ # def respond(
11
+ # message: str,
12
+ # history: list[tuple[str, str]], # This will not be used
13
+ # system_message: str,
14
+ # max_tokens: int,
15
+ # temperature: float,
16
+ # top_p: float,
17
+ # ):
18
+ # messages = [{"role": "system", "content": system_message}]
19
+
20
+ # # Append only the latest user message
21
+
22
+
23
+
24
+
25
+
26
+ # messages.append({"role": "user", "content": message})
27
+
28
+ # response = ""
29
+
30
+ # try:
31
+ # # Generate response from the model
32
+ # for message in client.chat_completion(
33
+ # messages,
34
+ # max_tokens=max_tokens,
35
+ # stream=True,
36
+ # temperature=temperature,
37
+ # top_p=top_p,
38
+ # ):
39
+ # if message.choices[0].delta.content is not None:
40
+ # token = message.choices[0].delta.content
41
+ # response += token
42
+ # yield response
43
+ # except Exception as e:
44
+ # yield f"An error occurred: {e}"
45
+ # ],
46
+ # )
47
+
48
+
49
+ # if __name__ == "__main__":
50
+ # demo.launch()
51
+
52
+
53
+
54
  import gradio as gr
55
  from huggingface_hub import InferenceClient
56
 
 
59
  """
60
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
61
 
 
62
  def respond(
63
  message: str,
64
  history: list[tuple[str, str]], # This will not be used
 
67
  temperature: float,
68
  top_p: float,
69
  ):
70
+ # Build the messages list
71
  messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
72
  messages.append({"role": "user", "content": message})
73
 
74
  response = ""
75
 
76
  try:
77
  # Generate response from the model
78
+ for msg in client.chat_completion(
79
+ messages=messages,
80
  max_tokens=max_tokens,
81
  stream=True,
82
  temperature=temperature,
83
  top_p=top_p,
84
  ):
85
+ if msg.choices[0].delta.content is not None:
86
+ token = msg.choices[0].delta.content
87
  response += token
88
  yield response
89
  except Exception as e:
90
  yield f"An error occurred: {e}"
91
 
92
  """
93
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
94
  """
95
+ demo = gr.ChatInterface(
96
+ respond,
97
+ additional_inputs=[
98
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
99
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
100
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
101
+ gr.Slider(
102
+ minimum=0.1,
103
+ maximum=1.0,
104
+ value=0.95,
105
+ step=0.05,
106
+ label="Top-p (nucleus sampling)",
107
+ ),
108
  ],
109
  )
110
 
 
111
  if __name__ == "__main__":
112
  demo.launch()