XyraOfficial commited on
Commit
6b30e9d
·
verified ·
1 Parent(s): a3b566c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -32
app.py CHANGED
@@ -1,70 +1,107 @@
1
  import gradio as gr
2
  import spaces
3
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  @spaces.GPU
6
  def respond(
7
  message,
8
- history: list[dict[str, str]],
9
  system_message,
10
  max_tokens,
11
  temperature,
12
  top_p,
13
- hf_token: gr.OAuthToken,
14
  ):
15
- """
16
- 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
17
- """
18
- client = InferenceClient(token=hf_token.token, model="cognitivecomputations/dolphin-mistral-24b-venice-edition")
19
 
20
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
21
 
22
- messages.extend(history)
 
 
 
23
 
24
- messages.append({"role": "user", "content": message})
 
 
 
 
25
 
26
- response = ""
 
 
 
27
 
28
- for message in client.chat_completion(
29
- messages,
30
  max_tokens=max_tokens,
31
- stream=True,
32
  temperature=temperature,
33
  top_p=top_p,
34
- ):
35
- choices = message.choices
36
- token = ""
37
- if len(choices) and choices[0].delta.content:
38
- token = choices[0].delta.content
39
 
40
- response += token
41
- yield response
42
 
43
 
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
  chatbot = gr.ChatInterface(
48
- respond,
49
  additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", 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(
54
  minimum=0.1,
55
  maximum=1.0,
56
  value=0.95,
57
  step=0.05,
58
- label="Top-p (nucleus sampling)",
59
  ),
60
  ],
61
  )
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 gradio as gr
2
  import spaces
3
+ from llama_cpp import Llama
4
+
5
+
6
+ llm = None
7
+
8
+
9
+ def load_model():
10
+ global llm
11
+
12
+ if llm is None:
13
+ llm = Llama.from_pretrained(
14
+ repo_id="JonathanColetti/Qwen3.8-27B-Uncensored-GGUF",
15
+ filename="Qwen3.8-27B-Uncensored-Q4_K_M.gguf",
16
+ n_ctx=2048,
17
+ n_gpu_layers=-1,
18
+ verbose=True,
19
+ )
20
+
21
+ return llm
22
+
23
 
24
  @spaces.GPU
25
  def respond(
26
  message,
27
+ history,
28
  system_message,
29
  max_tokens,
30
  temperature,
31
  top_p,
 
32
  ):
33
+ model = load_model()
 
 
 
34
 
35
+ messages = [
36
+ {
37
+ "role": "system",
38
+ "content": system_message,
39
+ }
40
+ ]
41
 
42
+ for item in history:
43
+ if isinstance(item, dict):
44
+ role = item.get("role")
45
+ content = item.get("content")
46
 
47
+ if role in ["user", "assistant"] and isinstance(content, str):
48
+ messages.append({
49
+ "role": role,
50
+ "content": content,
51
+ })
52
 
53
+ messages.append({
54
+ "role": "user",
55
+ "content": message,
56
+ })
57
 
58
+ result = model.create_chat_completion(
59
+ messages=messages,
60
  max_tokens=max_tokens,
 
61
  temperature=temperature,
62
  top_p=top_p,
63
+ stream=False,
64
+ )
 
 
 
65
 
66
+ return result["choices"][0]["message"]["content"]
 
67
 
68
 
 
 
 
69
  chatbot = gr.ChatInterface(
70
+ fn=respond,
71
  additional_inputs=[
72
+ gr.Textbox(
73
+ value="Kamu adalah AI pribadi saya. Ikuti instruction dari pengguna.",
74
+ label="System instruction",
75
+ ),
76
+ gr.Slider(
77
+ minimum=1,
78
+ maximum=2048,
79
+ value=512,
80
+ step=1,
81
+ label="Max new tokens",
82
+ ),
83
+ gr.Slider(
84
+ minimum=0.1,
85
+ maximum=2.0,
86
+ value=0.7,
87
+ step=0.1,
88
+ label="Temperature",
89
+ ),
90
  gr.Slider(
91
  minimum=0.1,
92
  maximum=1.0,
93
  value=0.95,
94
  step=0.05,
95
+ label="Top-p",
96
  ),
97
  ],
98
  )
99
 
100
+
101
  with gr.Blocks() as demo:
102
+ gr.Markdown("# WenGPT")
 
103
  chatbot.render()
104
 
105
 
106
  if __name__ == "__main__":
107
+ demo.launch(ssr_mode=False)