Spaces:
Runtime error
Runtime error
azizbarank
commited on
Commit
•
3cf1901
1
Parent(s):
1f427a5
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system("pip install transformers")
|
3 |
+
os.system("pip3 install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1+cpu -f "
|
4 |
+
"https://download.pytorch.org/whl/cpu/torch_stable.html")
|
5 |
+
os.system("pip install mtranslate")
|
6 |
+
os.system("pip install requests")
|
7 |
+
os.system("pip install random")
|
8 |
+
|
9 |
+
import transformers
|
10 |
+
import json
|
11 |
+
import random
|
12 |
+
import requests
|
13 |
+
|
14 |
+
from mtranslate import translate
|
15 |
+
import streamlit as st
|
16 |
+
|
17 |
+
|
18 |
+
MODELS = {
|
19 |
+
"GPT-2 Model Recycled From English": {
|
20 |
+
"url": "https://api-inference.huggingface.co/models/GroNLP/gpt2-small-dutch"
|
21 |
+
},
|
22 |
+
}
|
23 |
+
|
24 |
+
PROMPT_LIST = {
|
25 |
+
"Er was eens...": ["Er was eens..."],
|
26 |
+
"Dag.": ["Hallo, mijn naam is "],
|
27 |
+
"Te zijn of niet te zijn?": ["Naar mijn mening is 'zijn'"],
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
def query(payload, model_name):
|
32 |
+
data = json.dumps(payload)
|
33 |
+
print("model url:", MODELS[model_name]["url"])
|
34 |
+
response = requests.request(
|
35 |
+
"POST", MODELS[model_name]["url"], headers={}, data=data
|
36 |
+
)
|
37 |
+
return json.loads(response.content.decode("utf-8"))
|
38 |
+
|
39 |
+
|
40 |
+
def process(
|
41 |
+
text: str, model_name: str, max_len: int, temp: float, top_k: int, top_p: float
|
42 |
+
):
|
43 |
+
payload = {
|
44 |
+
"inputs": text,
|
45 |
+
"parameters": {
|
46 |
+
"max_new_tokens": max_len,
|
47 |
+
"top_k": top_k,
|
48 |
+
"top_p": top_p,
|
49 |
+
"temperature": temp,
|
50 |
+
"repetition_penalty": 2.0,
|
51 |
+
},
|
52 |
+
"options": {
|
53 |
+
"use_cache": True,
|
54 |
+
},
|
55 |
+
}
|
56 |
+
return query(payload, model_name)
|
57 |
+
|
58 |
+
|
59 |
+
# Page
|
60 |
+
st.set_page_config(page_title="Dutch GPT-2 Demo")
|
61 |
+
st.title("Dutch GPT-2")
|
62 |
+
|
63 |
+
|
64 |
+
# Sidebar
|
65 |
+
st.sidebar.subheader("Configurable parameters")
|
66 |
+
|
67 |
+
max_len = st.sidebar.number_input(
|
68 |
+
"Maximum length",
|
69 |
+
value=100,
|
70 |
+
help="The maximum length of the sequence to be generated.",
|
71 |
+
)
|
72 |
+
|
73 |
+
temp = st.sidebar.slider(
|
74 |
+
"Temperature",
|
75 |
+
value=1.0,
|
76 |
+
min_value=0.1,
|
77 |
+
max_value=100.0,
|
78 |
+
help="The value used to module the next token probabilities.",
|
79 |
+
)
|
80 |
+
|
81 |
+
top_k = st.sidebar.number_input(
|
82 |
+
"Top k",
|
83 |
+
value=10,
|
84 |
+
help="The number of highest probability vocabulary tokens to keep for top-k-filtering.",
|
85 |
+
)
|
86 |
+
|
87 |
+
top_p = st.sidebar.number_input(
|
88 |
+
"Top p",
|
89 |
+
value=0.95,
|
90 |
+
help=" If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation.",
|
91 |
+
)
|
92 |
+
|
93 |
+
do_sample = st.sidebar.selectbox(
|
94 |
+
"Sampling?",
|
95 |
+
(True, False),
|
96 |
+
help="Whether or not to use sampling; use greedy decoding otherwise.",
|
97 |
+
)
|
98 |
+
|
99 |
+
|
100 |
+
# Body
|
101 |
+
st.markdown(
|
102 |
+
"""
|
103 |
+
Dutch GPT-2 model (small) is based on the English GPT-2 model:
|
104 |
+
|
105 |
+
Researches [Wietse de Vries](https://www.semanticscholar.org/author/Wietse-de-Vries/144611157) and [M. Nissim](https://www.semanticscholar.org/author/M.-Nissim/2742475)
|
106 |
+
obtained this model by transfering the English GPT-2 model in multiple procedure while exploiting genetic closeness between Dutch and English.
|
107 |
+
|
108 |
+
During this process, they retrained the lexical embeddings of the original English GPT-2 model and did additional fine-tuning of the full Dutch model
|
109 |
+
for better text generation.
|
110 |
+
|
111 |
+
For more information on the model:
|
112 |
+
|
113 |
+
[arXiv](https://arxiv.org/abs/2012.05628)
|
114 |
+
[GitHub](https://github.com/wietsedv/gpt2-recycle)
|
115 |
+
|
116 |
+
"""
|
117 |
+
)
|
118 |
+
|
119 |
+
model_name = st.selectbox("Model", (list(MODELS.keys())))
|
120 |
+
|
121 |
+
ALL_PROMPTS = list(PROMPT_LIST.keys()) + ["Custom"]
|
122 |
+
prompt = st.selectbox("Prompt", ALL_PROMPTS, index=len(ALL_PROMPTS) - 1)
|
123 |
+
if prompt == "Custom":
|
124 |
+
prompt_box = "Enter your text here"
|
125 |
+
else:
|
126 |
+
prompt_box = random.choice(PROMPT_LIST[prompt])
|
127 |
+
|
128 |
+
text = st.text_area("Enter text", prompt_box)
|
129 |
+
|
130 |
+
if st.button("Run"):
|
131 |
+
with st.spinner(text="Getting results..."):
|
132 |
+
st.subheader("Result")
|
133 |
+
print(f"maxlen:{max_len}, temp:{temp}, top_k:{top_k}, top_p:{top_p}")
|
134 |
+
result = process(
|
135 |
+
text=text,
|
136 |
+
model_name=model_name,
|
137 |
+
max_len=int(max_len),
|
138 |
+
temp=temp,
|
139 |
+
top_k=int(top_k),
|
140 |
+
top_p=float(top_p),
|
141 |
+
)
|
142 |
+
print("result:", result)
|
143 |
+
if "error" in result:
|
144 |
+
if type(result["error"]) is str:
|
145 |
+
st.write(f'{result["error"]}.', end=" ")
|
146 |
+
if "estimated_time" in result:
|
147 |
+
st.write(
|
148 |
+
f'Please try again in about {result["estimated_time"]:.0f} seconds.'
|
149 |
+
)
|
150 |
+
else:
|
151 |
+
if type(result["error"]) is list:
|
152 |
+
for error in result["error"]:
|
153 |
+
st.write(f"{error}")
|
154 |
+
else:
|
155 |
+
result = result[0]["generated_text"]
|
156 |
+
st.write(result.replace("\n", " \n"))
|
157 |
+
st.text("English translation")
|
158 |
+
st.write(translate(result, "en", "nl").replace("\n", " \n"))
|
159 |
+
|
160 |
+
st.sidebar.subheader("Citation")
|
161 |
+
st.markdown(
|
162 |
+
"""
|
163 |
+
|
164 |
+
```
|
165 |
+
@inproceedings{de-vries-nissim-2021-good,
|
166 |
+
title = "As Good as New. How to Successfully Recycle {E}nglish {GPT}-2 to Make Models for Other Languages",
|
167 |
+
author = "de Vries, Wietse and
|
168 |
+
Nissim, Malvina",
|
169 |
+
booktitle = "Findings of the Association for Computational Linguistics: ACL-IJCNLP 2021",
|
170 |
+
month = aug,
|
171 |
+
year = "2021",
|
172 |
+
address = "Online",
|
173 |
+
publisher = "Association for Computational Linguistics",
|
174 |
+
url = "https://aclanthology.org/2021.findings-acl.74",
|
175 |
+
doi = "10.18653/v1/2021.findings-acl.74",
|
176 |
+
pages = "836--846",
|
177 |
+
}
|
178 |
+
```
|
179 |
+
|
180 |
+
|
181 |
+
"""
|
182 |
+
)
|