Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,51 @@
|
|
| 1 |
-
from transformers import BloomTokenizerFast, BloomModel
|
| 2 |
import torch
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import transformers
|
| 3 |
+
import numpy as np
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
|
| 8 |
+
|
| 9 |
+
hf_hub_download("OpenDungeon/gpt-j-8bit-ffbgem", "model.pt")
|
| 10 |
+
|
| 11 |
+
qmodel = torch.load("model.pt")
|
| 12 |
+
|
| 13 |
+
def PrintContinuation(prompt, local_model, single_hook=None, batch=1, limit_tokens = 50):
|
| 14 |
+
past_key_values = None # used to keep track of conversation history
|
| 15 |
+
input_dict = tokenizer([prompt] * batch, return_tensors='pt', padding=False)
|
| 16 |
+
output = [""] * batch
|
| 17 |
+
|
| 18 |
+
with torch.inference_mode():
|
| 19 |
+
for i in range(limit_tokens + 20):
|
| 20 |
+
if i == 5:
|
| 21 |
+
start_time = time.perf_counter()
|
| 22 |
+
|
| 23 |
+
outputs = local_model.forward(**input_dict, use_cache=True, past_key_values=past_key_values)
|
| 24 |
+
last_logits = outputs.logits[:, -1]
|
| 25 |
+
|
| 26 |
+
for j in range(batch):
|
| 27 |
+
last_logits[j, last_logits[j].topk(k=10).indices] += 10
|
| 28 |
+
|
| 29 |
+
past_key_values = outputs.past_key_values
|
| 30 |
+
token_ix = torch.multinomial(last_logits.softmax(-1), 1)
|
| 31 |
+
output = [stream + tokenizer.decode(ix) for stream, ix in zip(output, token_ix)]
|
| 32 |
+
|
| 33 |
+
if single_hook is not None:
|
| 34 |
+
single_hook(tokenizer.decode(token_ix[0]))
|
| 35 |
+
if i == limit_tokens:
|
| 36 |
+
print()
|
| 37 |
+
print((time.perf_counter() - start_time) / (i - 4), "s per token")
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
input_dict = dict(input_ids=token_ix)
|
| 41 |
+
print()
|
| 42 |
+
return output
|
| 43 |
+
|
| 44 |
+
import streamlit as st
|
| 45 |
+
|
| 46 |
+
def process(text):
|
| 47 |
+
return text[::-1]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
text = st.text_area("Prompt")
|
| 51 |
+
t.markdown(f"## {process(text)[0:i]}...")
|