NotProCaps commited on
Commit
3998dc6
·
verified ·
1 Parent(s): 0746435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -1,6 +1,20 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
  message,
@@ -11,43 +25,39 @@ def respond(
11
  top_p,
12
  hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="HauhauCS/GLM-4.7-Flash-Uncensored-HauhauCS-Balanced")
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
20
 
 
21
  messages.extend(history)
22
-
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
 
27
- for message in client.chat_completion(
 
28
  messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
  yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
49
  additional_inputs=[
50
- gr.Textbox(value="You are a unlocked coding assistant.", 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(
@@ -62,9 +72,13 @@ chatbot = gr.ChatInterface(
62
 
63
  with gr.Blocks() as demo:
64
  with gr.Sidebar():
 
65
  gr.LoginButton()
 
 
 
 
66
  chatbot.render()
67
 
68
-
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
+ MODEL_ID = "HauhauCS/GLM-4.7-Flash-Uncensored-HauhauCS-Balanced"
6
+
7
+
8
+ def get_token(hf_token: gr.OAuthToken | None) -> str | None:
9
+ """
10
+ Priority:
11
+ 1) Gradio OAuth token (when user clicks LoginButton)
12
+ 2) Space secret HF_TOKEN (Settings -> Secrets)
13
+ """
14
+ if hf_token is not None and getattr(hf_token, "token", None):
15
+ return hf_token.token
16
+ return os.getenv("HF_TOKEN")
17
+
18
 
19
  def respond(
20
  message,
 
25
  top_p,
26
  hf_token: gr.OAuthToken,
27
  ):
28
+ token = get_token(hf_token)
 
 
 
29
 
30
+ # If the repo is gated/private, you need a token.
31
+ # If it's public, token can be None and it may still work depending on the endpoint limits.
32
+ client = InferenceClient(model=MODEL_ID, token=token)
33
 
34
+ messages = [{"role": "system", "content": system_message}]
35
  messages.extend(history)
 
36
  messages.append({"role": "user", "content": message})
37
 
38
  response = ""
39
 
40
+ # Stream output token-by-token
41
+ for chunk in client.chat_completion(
42
  messages,
43
  max_tokens=max_tokens,
44
  stream=True,
45
  temperature=temperature,
46
  top_p=top_p,
47
  ):
48
+ token_text = ""
49
+ if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
50
+ token_text = chunk.choices[0].delta.content
 
51
 
52
+ response += token_text
53
  yield response
54
 
55
 
 
 
 
56
  chatbot = gr.ChatInterface(
57
  respond,
58
  type="messages",
59
  additional_inputs=[
60
+ gr.Textbox(value="You are a helpful coding assistant.", 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(
 
72
 
73
  with gr.Blocks() as demo:
74
  with gr.Sidebar():
75
+ gr.Markdown(f"### Model\n`{MODEL_ID}`")
76
  gr.LoginButton()
77
+ gr.Markdown(
78
+ "If this model requires authentication, click **Login** "
79
+ "or set a Space secret named `HF_TOKEN`."
80
+ )
81
  chatbot.render()
82
 
 
83
  if __name__ == "__main__":
84
  demo.launch()