Spaces:
Sleeping
Sleeping
michailroussos
commited on
Commit
·
eeb0917
1
Parent(s):
330d143
changing to my model with transformers
Browse files- app.py +25 -17
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
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 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|