Spaces:
Sleeping
Sleeping
Saif Rehman Nasir
commited on
Commit
·
eed9e08
1
Parent(s):
b9f58f3
Refactor code
Browse files
app.py
CHANGED
@@ -6,13 +6,13 @@ device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
6 |
|
7 |
model = torch.load('saved_model.pth', map_location= torch.device(device), weights_only=False)
|
8 |
|
9 |
-
def generate_text(context, num_of_tokens, temperature=1.0):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
|
17 |
|
18 |
|
@@ -29,8 +29,12 @@ with gr.Blocks() as demo:
|
|
29 |
context,
|
30 |
num_of_tokens,tmp
|
31 |
]
|
|
|
|
|
|
|
|
|
32 |
generate_btn = gr.Button(value="Generate")
|
33 |
outputs = [gr.Textbox(label= "Generated text: ")]
|
34 |
-
generate_btn.click(fn =
|
35 |
|
36 |
demo.launch()
|
|
|
6 |
|
7 |
model = torch.load('saved_model.pth', map_location= torch.device(device), weights_only=False)
|
8 |
|
9 |
+
# def generate_text(context, num_of_tokens, temperature=1.0):
|
10 |
+
# if context == None or context == '':
|
11 |
+
# idx = torch.zeros((1,1), dtype=torch.long)
|
12 |
+
# else:
|
13 |
+
# idx = torch.tensor(encode(context), dtype=torch.long).unsqueeze(0)
|
14 |
|
15 |
+
# yield model.generate(idx, max_new_tokens=num_of_tokens,temperature=temperature)[0].tolist()
|
16 |
|
17 |
|
18 |
|
|
|
29 |
context,
|
30 |
num_of_tokens,tmp
|
31 |
]
|
32 |
+
if context == None or context == '':
|
33 |
+
idx = torch.zeros((1,1), dtype=torch.long)
|
34 |
+
else:
|
35 |
+
idx = torch.tensor(encode(context), dtype=torch.long).unsqueeze(0)
|
36 |
generate_btn = gr.Button(value="Generate")
|
37 |
outputs = [gr.Textbox(label= "Generated text: ")]
|
38 |
+
generate_btn.click(fn = model.generate(idx, max_new_tokens=num_of_tokens,temperature=tmp)[0].tolist() , inputs= inputs, outputs= outputs)
|
39 |
|
40 |
demo.launch()
|