vietdata commited on
Commit
fd9353f
1 Parent(s): 68ceb16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,12 +1,17 @@
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
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,34 +20,39 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.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
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
@@ -59,6 +69,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
 
 
 
 
4
 
5
+ # Load the model and tokenizer locally in bfloat16 precision
6
+ model_name = "vietdata/llama32_1b_pub"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.bfloat16, # Load model in bfloat16 precision
11
+ device_map="auto" if torch.cuda.is_available() else None, # Automatically map to available devices
12
+ )
13
 
14
+ # Define the respond function
15
  def respond(
16
  message,
17
  history: list[tuple[str, str]],
 
20
  temperature,
21
  top_p,
22
  ):
23
+ from transformers import TextGenerationPipeline
24
 
25
+ # Build the conversation context
26
+ prompt = system_message + "\n"
27
+ for user_msg, bot_msg in history:
28
+ if user_msg:
29
+ prompt += f"User: {user_msg}\n"
30
+ if bot_msg:
31
+ prompt += f"Bot: {bot_msg}\n"
32
+ prompt += f"User: {message}\nBot:"
33
 
34
+ # Set up a text generation pipeline
35
+ pipe = TextGenerationPipeline(
36
+ model=model,
37
+ tokenizer=tokenizer,
38
+ device=torch.cuda.current_device() if torch.cuda.is_available() else -1
39
+ )
40
 
41
+ # Generate the response
42
+ response = pipe(
43
+ prompt,
44
+ max_length=len(prompt) + max_tokens,
 
 
45
  temperature=temperature,
46
  top_p=top_p,
47
+ pad_token_id=tokenizer.eos_token_id
48
+ )[0]["generated_text"]
49
 
50
+ # Extract the generated part only
51
+ generated_response = response[len(prompt):]
52
+ yield generated_response
53
 
54
 
55
+ # Gradio app definition
 
 
56
  demo = gr.ChatInterface(
57
  respond,
58
  additional_inputs=[
 
69
  ],
70
  )
71
 
 
72
  if __name__ == "__main__":
73
  demo.launch()