thanglekdi commited on
Commit
4fe7f43
·
1 Parent(s): 7740cf7
Files changed (1) hide show
  1. app.py +50 -23
app.py CHANGED
@@ -1,42 +1,69 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- # Load PhoGPT-4B-Chat model and tokenizer
5
  tokenizer = AutoTokenizer.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
6
  model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
7
 
8
- def respond(message, history, system_message, max_tokens, temperature, top_p):
9
- messages = f"{system_message}\n"
10
- for user_msg, bot_msg in history:
11
- if user_msg:
12
- messages += f"User: {user_msg}\n"
13
- if bot_msg:
14
- messages += f"Bot: {bot_msg}\n"
15
- messages += f"User: {message}\nBot:"
16
-
17
- inputs = tokenizer(messages, return_tensors="pt")
18
- outputs = model.generate(
19
- **inputs,
20
- max_new_tokens=max_tokens,
 
 
 
 
 
 
 
 
 
 
 
21
  temperature=temperature,
22
  top_p=top_p,
23
- do_sample=True,
24
- pad_token_id=tokenizer.eos_token_id
25
- )
26
- full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
- response = full_output.replace(messages, "").strip()
28
- yield response
29
 
 
 
30
 
 
 
 
 
31
  demo = gr.ChatInterface(
32
  respond,
33
  additional_inputs=[
34
- gr.Textbox(value="Bạn là một chatbot người Việt thân thiện.", label="System message"),
35
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
36
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
37
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
38
  ],
39
  )
40
 
 
41
  if __name__ == "__main__":
42
- 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("HuggingFaceH4/zephyr-7b-beta")
8
+
9
+ # Load model directly
10
  from transformers import AutoTokenizer, AutoModelForCausalLM
11
 
 
12
  tokenizer = AutoTokenizer.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
13
  model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
14
 
15
+ def respond(
16
+ message,
17
+ history: list[tuple[str, str]],
18
+ system_message,
19
+ max_tokens,
20
+ temperature,
21
+ top_p,
22
+ ):
23
+ messages = [{"role": "system", "content": system_message}]
24
+
25
+ for val in history:
26
+ if val[0]:
27
+ messages.append({"role": "user", "content": val[0]})
28
+ if val[1]:
29
+ messages.append({"role": "assistant", "content": val[1]})
30
+
31
+ messages.append({"role": "user", "content": message})
32
+
33
+ response = ""
34
+
35
+ for message in model.chat_completion(
36
+ messages,
37
+ max_tokens=max_tokens,
38
+ stream=True,
39
  temperature=temperature,
40
  top_p=top_p,
41
+ ):
42
+ token = message.choices[0].delta.content
 
 
 
 
43
 
44
+ response += token
45
+ yield response
46
 
47
+
48
+ """
49
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
50
+ """
51
  demo = gr.ChatInterface(
52
  respond,
53
  additional_inputs=[
54
+ gr.Textbox(value=" ", label="System message"),
55
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
56
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
57
+ gr.Slider(
58
+ minimum=0.1,
59
+ maximum=1.0,
60
+ value=0.95,
61
+ step=0.05,
62
+ label="Top-p (nucleus sampling)",
63
+ ),
64
  ],
65
  )
66
 
67
+
68
  if __name__ == "__main__":
69
+ demo.launch()