michailroussos commited on
Commit
eeb0917
·
1 Parent(s): 330d143

changing to my model with transformers

Browse files
Files changed (2) hide show
  1. app.py +25 -17
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,11 +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
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
- #client = InferenceClient("michailroussos/model-mistral_CP1250")
9
 
10
  def respond(
11
  message,
@@ -25,18 +23,28 @@ def respond(
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
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ client = AutoModelForCausalLM.from_pretrained("michailroussos/model-mistral_CP1250", torch_dtype=torch.float16)
6
+ tokenizer = AutoTokenizer.from_pretrained("michailroussos/model-mistral_CP1250")
 
 
 
7
 
8
  def respond(
9
  message,
 
23
 
24
  messages.append({"role": "user", "content": message})
25
 
26
+ # Convert messages to prompt
27
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
28
 
29
+ # Generate response
30
+ inputs = tokenizer(prompt, return_tensors="pt").to(client.device)
31
+
32
+ response = ""
33
+ for _ in range(max_tokens):
34
+ with torch.no_grad():
35
+ outputs = client.generate(
36
+ inputs.input_ids,
37
+ max_new_tokens=1,
38
+ temperature=temperature,
39
+ top_p=top_p,
40
+ do_sample=True,
41
+ )
42
+
43
+ new_token = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
44
+ response += new_token
45
+
46
+ inputs = tokenizer(inputs.input_ids.tolist()[0] + outputs[0][inputs.input_ids.shape[1]:].tolist(), return_tensors="pt")
47
+
48
  yield response
49
 
50
 
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.25.2
 
 
 
1
+ huggingface_hub==0.25.2
2
+ transformers
3
+ torch