Spaces:
Sleeping
Sleeping
Benjamin Gonzalez
commited on
Commit
·
839fca3
1
Parent(s):
b461977
use gpu is available
Browse files- app.py +27 -6
- pre-requirements.txt +1 -1
app.py
CHANGED
@@ -5,17 +5,15 @@ import gradio as gr
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
|
6 |
model = AutoModelForCausalLM.from_pretrained(
|
7 |
"microsoft/phi-2",
|
8 |
-
torch_dtype=torch.float32,
|
9 |
-
device_map="cpu",
|
10 |
trust_remote_code=True,
|
11 |
)
|
12 |
|
13 |
|
14 |
def generate(prompt, length):
|
15 |
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
|
16 |
-
if length
|
17 |
-
length = len(inputs)
|
18 |
-
outputs = model.generate(**inputs, max_length=length)
|
19 |
return tokenizer.batch_decode(outputs)[0]
|
20 |
|
21 |
|
@@ -26,9 +24,32 @@ demo = gr.Interface(
|
|
26 |
label="prompt",
|
27 |
value="Write a detailed analogy between mathematics and a lighthouse.",
|
28 |
),
|
29 |
-
gr.Number(value=
|
30 |
],
|
31 |
outputs="text",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
|
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
|
6 |
model = AutoModelForCausalLM.from_pretrained(
|
7 |
"microsoft/phi-2",
|
8 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
9 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
10 |
trust_remote_code=True,
|
11 |
)
|
12 |
|
13 |
|
14 |
def generate(prompt, length):
|
15 |
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
|
16 |
+
outputs = model.generate(**inputs, max_length=length if length >= len(inputs) else len(inputs))
|
|
|
|
|
17 |
return tokenizer.batch_decode(outputs)[0]
|
18 |
|
19 |
|
|
|
24 |
label="prompt",
|
25 |
value="Write a detailed analogy between mathematics and a lighthouse.",
|
26 |
),
|
27 |
+
gr.Number(value=100, label="max length", maximum=1000),
|
28 |
],
|
29 |
outputs="text",
|
30 |
+
examples=[
|
31 |
+
[
|
32 |
+
"Instruct: Write a detailed analogy between mathematics and a lighthouse.",
|
33 |
+
50,
|
34 |
+
],
|
35 |
+
[
|
36 |
+
"Instruct: Write a detailed analogy between mathematics and a lighthouse.\nOutput:",
|
37 |
+
50,
|
38 |
+
],
|
39 |
+
[
|
40 |
+
"Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestions?\nBob: ",
|
41 |
+
100,
|
42 |
+
],
|
43 |
+
[
|
44 |
+
'''def print_prime(n):
|
45 |
+
"""
|
46 |
+
Print all primes between 1 and n
|
47 |
+
"""\n''',
|
48 |
+
200,
|
49 |
+
],
|
50 |
+
],
|
51 |
+
title="Microsoft Phi-2",
|
52 |
+
description="Unofficial demo of Microsoft Phi-2, a high performing model with only 2.7B parameters.",
|
53 |
)
|
54 |
|
55 |
|
pre-requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
--extra-index-url https://download.pytorch.org/whl/
|
2 |
torch==2.1.0
|
3 |
wheel
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cu121
|
2 |
torch==2.1.0
|
3 |
wheel
|