Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
from mtranslate import translate
|
| 4 |
+
from prompts import PROMPT_LIST
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import random
|
| 7 |
+
headers = {}
|
| 8 |
+
MODELS = {
|
| 9 |
+
"GPT-2 Base": {
|
| 10 |
+
"url": "https://api-inference.huggingface.co/models/BigSalmon/InformalToFormalLincoln14"
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
def query(payload, model_name):
|
| 14 |
+
data = json.dumps(payload)
|
| 15 |
+
print("model url:", MODELS[model_name]["url"])
|
| 16 |
+
response = requests.request(
|
| 17 |
+
"POST", MODELS[model_name]["url"], headers=headers, data=data)
|
| 18 |
+
return json.loads(response.content.decode("utf-8"))
|
| 19 |
+
def process(text: str,
|
| 20 |
+
model_name: str,
|
| 21 |
+
max_len: int,
|
| 22 |
+
temp: float,
|
| 23 |
+
top_k: int,
|
| 24 |
+
top_p: float):
|
| 25 |
+
payload = {
|
| 26 |
+
"inputs": text,
|
| 27 |
+
"parameters": {
|
| 28 |
+
"max_new_tokens": max_len,
|
| 29 |
+
"top_k": top_k,
|
| 30 |
+
"top_p": top_p,
|
| 31 |
+
"temperature": temp,
|
| 32 |
+
"repetition_penalty": 2.0,
|
| 33 |
+
},
|
| 34 |
+
"options": {
|
| 35 |
+
"use_cache": True,
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
return query(payload, model_name)
|
| 39 |
+
st.set_page_config(page_title="Thai GPT2 Demo")
|
| 40 |
+
st.title("π Thai GPT2")
|
| 41 |
+
st.sidebar.subheader("Configurable parameters")
|
| 42 |
+
max_len = st.sidebar.text_input(
|
| 43 |
+
"Maximum length",
|
| 44 |
+
value=100,
|
| 45 |
+
help="The maximum length of the sequence to be generated."
|
| 46 |
+
)
|
| 47 |
+
temp = st.sidebar.slider(
|
| 48 |
+
"Temperature",
|
| 49 |
+
value=1.0,
|
| 50 |
+
min_value=0.1,
|
| 51 |
+
max_value=100.0,
|
| 52 |
+
help="The value used to module the next token probabilities."
|
| 53 |
+
)
|
| 54 |
+
top_k = st.sidebar.text_input(
|
| 55 |
+
"Top k",
|
| 56 |
+
value=50,
|
| 57 |
+
help="The number of highest probability vocabulary tokens to keep for top-k-filtering."
|
| 58 |
+
)
|
| 59 |
+
top_p = st.sidebar.text_input(
|
| 60 |
+
"Top p",
|
| 61 |
+
value=0.95,
|
| 62 |
+
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."
|
| 63 |
+
)
|
| 64 |
+
do_sample = st.sidebar.selectbox(
|
| 65 |
+
'Sampling?', (True, False), help="Whether or not to use sampling; use greedy decoding otherwise.")
|
| 66 |
+
st.markdown(
|
| 67 |
+
"""Thai GPT-2 demo. Part of the [Huggingface JAX/Flax event](https://discuss.huggingface.co/t/open-to-the-community-community-week-using-jax-flax-for-nlp-cv/)."""
|
| 68 |
+
)
|
| 69 |
+
model_name = st.selectbox('Model', (['GPT-2 Base']))
|
| 70 |
+
ALL_PROMPTS = list(PROMPT_LIST.keys())+["Custom"]
|
| 71 |
+
prompt = st.selectbox('Prompt', ALL_PROMPTS, index=len(ALL_PROMPTS)-1)
|
| 72 |
+
if prompt == "Custom":
|
| 73 |
+
prompt_box = "Enter your text here"
|
| 74 |
+
else:
|
| 75 |
+
prompt_box = random.choice(PROMPT_LIST[prompt])
|
| 76 |
+
text = st.text_area("Enter text", prompt_box)
|
| 77 |
+
if st.button("Run"):
|
| 78 |
+
with st.spinner(text="Getting results..."):
|
| 79 |
+
st.subheader("Result")
|
| 80 |
+
print(f"maxlen:{max_len}, temp:{temp}, top_k:{top_k}, top_p:{top_p}")
|
| 81 |
+
result = process(text=text,
|
| 82 |
+
model_name=model_name,
|
| 83 |
+
max_len=int(max_len),
|
| 84 |
+
temp=temp,
|
| 85 |
+
top_k=int(top_k),
|
| 86 |
+
top_p=float(top_p))
|
| 87 |
+
print("result:", result)
|
| 88 |
+
if "error" in result:
|
| 89 |
+
if type(result["error"]) is str:
|
| 90 |
+
st.write(f'{result["error"]}. Please try it again in about {result["estimated_time"]:.0f} seconds')
|
| 91 |
+
else:
|
| 92 |
+
if type(result["error"]) is list:
|
| 93 |
+
for error in result["error"]:
|
| 94 |
+
st.write(f'{error}')
|
| 95 |
+
else:
|
| 96 |
+
result = result[0]["generated_text"]
|
| 97 |
+
st.write(result.replace("\n", " \n"))
|
| 98 |
+
st.text("Thai πΉπ to English π¬π§ translation")
|
| 99 |
+
st.write(translate(result, "en", "th").replace("\n", " \n"))
|