Spaces:
Runtime error
Runtime error
aaditkamat
commited on
Commit
•
21af8bd
1
Parent(s):
68ed92d
Install transformers dependency (#1)
Browse files- Install transformers dependency (f274ca866a4c5cdfd36394ed24e858445ab4893d)
- Update python script (231f15f5049f32e1080c2b1aeb6a2c8182964302)
- app.py +29 -19
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,10 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def respond(
|
@@ -25,20 +46,9 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
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
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("AIDC-AI/Marco-o1")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("AIDC-AI/Marco-o1")
|
7 |
+
|
8 |
+
def load_model_and_tokenizer(path):
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(path, trust_remote_code=True).to('cuda:0')
|
11 |
+
model.eval()
|
12 |
+
return tokenizer, model
|
13 |
+
|
14 |
+
def generate_response(model, tokenizer,
|
15 |
+
input_ids, attention_mask,
|
16 |
+
max_new_tokens=4096):
|
17 |
+
generated_ids = input_ids
|
18 |
+
with torch.inference_mode():
|
19 |
+
for _ in range(max_new_tokens):
|
20 |
+
outputs = model(input_ids=generated_ids, attention_mask=attention_mask)
|
21 |
+
next_token_id = torch.argmax(outputs.logits[:, -1, :], dim=-1).unsqueeze(-1)
|
22 |
+
generated_ids = torch.cat([generated_ids, next_token_id], dim=-1)
|
23 |
+
attention_mask = torch.cat([attention_mask, torch.ones_like(next_token_id)], dim=-1)
|
24 |
+
new_token = tokenizer.decode(next_token_id.squeeze(), skip_special_tokens=True)
|
25 |
+
print(new_token, end='', flush=True)
|
26 |
+
if next_token_id.item() == tokenizer.eos_token_id:
|
27 |
+
break
|
28 |
+
return tokenizer.decode(generated_ids[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
29 |
|
30 |
|
31 |
def respond(
|
|
|
46 |
|
47 |
messages.append({"role": "user", "content": message})
|
48 |
|
49 |
+
text = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
|
50 |
+
model_inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=4096).to('cuda:0')
|
51 |
+
yield generate_response(model, tokenizer, model_inputs.input_ids, model_inputs.attention_mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
"""
|
54 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
transformers==4.46.3
|