liang1234 commited on
Commit
ff73263
·
verified ·
1 Parent(s): 55cb9ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,6 +1,9 @@
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
  """
@@ -24,20 +27,24 @@ def respond(
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
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b")
6
+ model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b")
7
  """
8
  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
9
  """
 
27
  messages.append({"role": "assistant", "content": val[1]})
28
 
29
  messages.append({"role": "user", "content": message})
 
30
  response = ""
31
+ inputs = tokenizer(message, return_tensors="pt")
32
+
33
+ generate_ids = model.generate(inputs.input_ids, max_length=30)
34
+ response = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
35
+ # Load model directly
36
+ yield response
37
+ # for message in client.chat_completion(
38
+ # messages,
39
+ # max_tokens=max_tokens,
40
+ # stream=True,
41
+ # temperature=temperature,
42
+ # top_p=top_p,
43
+ # ):
44
+ # token = message.choices[0].delta.content
45
 
46
+ # response += token
47
+ # yield response
 
 
 
 
 
 
 
 
 
48
 
49
  """
50
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface