Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,64 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, StopStringCriteria, StoppingCriteriaList
|
4 |
+
import torch
|
5 |
+
|
6 |
+
import torch._dynamo
|
7 |
+
torch._dynamo.config.suppress_errors = True
|
8 |
+
|
9 |
+
import os
|
10 |
+
# os.system("nvidia-smi")
|
11 |
+
# print("TORCH_CUDA", torch.cuda.is_available())
|
12 |
+
|
13 |
+
|
14 |
+
print("loading model")
|
15 |
+
# Load the tokenizer and model
|
16 |
+
repo_name = "nvidia/Hymba-1.5B-Instruct"
|
17 |
+
# repo_name = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
18 |
+
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_name, trust_remote_code=True)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(repo_name, trust_remote_code=True)
|
21 |
+
|
22 |
+
model = model.cuda().to(torch.bfloat16)
|
23 |
+
|
24 |
+
print("model is loaded")
|
25 |
+
|
26 |
+
|
27 |
+
# Chat with Hymba
|
28 |
+
# prompt = input()
|
29 |
+
prompt = "Who are you?"
|
30 |
+
|
31 |
+
messages = [
|
32 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
33 |
+
]
|
34 |
+
messages.append({"role": "user", "content": prompt})
|
35 |
+
|
36 |
+
# Apply chat template
|
37 |
+
tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to('cuda')
|
38 |
+
stopping_criteria = StoppingCriteriaList([StopStringCriteria(tokenizer=tokenizer, stop_strings="</s>")])
|
39 |
+
|
40 |
+
print("generating prompt")
|
41 |
+
|
42 |
+
|
43 |
+
outputs = model.generate(
|
44 |
+
tokenized_chat,
|
45 |
+
max_new_tokens=256,
|
46 |
+
do_sample=False,
|
47 |
+
temperature=0.7,
|
48 |
+
use_cache=True,
|
49 |
+
stopping_criteria=stopping_criteria
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
)
|
51 |
+
input_length = tokenized_chat.shape[1]
|
52 |
+
response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
|
53 |
+
|
54 |
+
print(f"Model response: {response}")
|
55 |
+
|
56 |
+
|
57 |
|
58 |
+
def greet(name):
|
59 |
+
print(f"User: prompt")
|
60 |
+
print(f"Model response: {response}")
|
61 |
+
# return "Hello " + name + "!!"
|
62 |
|
63 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
64 |
+
demo.launch()
|