cahya commited on
Commit
c6d338c
1 Parent(s): be6f31c

add apps files

Browse files
Files changed (4) hide show
  1. app.py +92 -2
  2. prompts.py +16 -0
  3. requirements.txt +4 -0
  4. start.sh +10 -0
app.py CHANGED
@@ -1,4 +1,94 @@
 
 
 
 
1
  import streamlit as st
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
8
+ backend = "http://fastapi:8000/generate"
9
+
10
+ def process(text: str,
11
+ model_name: str,
12
+ max_len: int,
13
+ temp: float,
14
+ top_k: int,
15
+ top_p: float,
16
+ do_sample: bool,
17
+ server_url: str):
18
+ payload = {"text": text,
19
+ "max_len": max_len,
20
+ "temp": temp,
21
+ "top_k": top_k,
22
+ "top_p": top_p,
23
+ "do_sample": do_sample,
24
+ "model_name": model_name}
25
+ r = requests.post(
26
+ server_url, data=json.dumps(payload), headers={"Content-Type": "application/json"}, timeout=8000
27
+ )
28
+
29
+ return json.loads(r.text)
30
+
31
+ st.set_page_config(page_title="Indonesian GPT-2 Demo")
32
+
33
+ st.title("Indonesian GPT-2")
34
+
35
+ st.sidebar.subheader("Configurable parameters")
36
+
37
+ max_len = st.sidebar.text_input(
38
+ "Maximum length",
39
+ value=100,
40
+ help="The maximum length of the sequence to be generated."
41
+ )
42
+
43
+ temp = st.sidebar.slider(
44
+ "Temperature",
45
+ value=1.0,
46
+ min_value=0.0,
47
+ max_value=100.0,
48
+ help="The value used to module the next token probabilities."
49
+ )
50
+
51
+ top_k = st.sidebar.text_input(
52
+ "Top k",
53
+ value=50,
54
+ help="The number of highest probability vocabulary tokens to keep for top-k-filtering."
55
+ )
56
+
57
+ top_p = st.sidebar.text_input(
58
+ "Top p",
59
+ value=1.0,
60
+ 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."
61
+ )
62
+
63
+ do_sample = st.sidebar.selectbox('Sampling?', (True, False), help="Whether or not to use sampling; use greedy decoding otherwise.")
64
+
65
+ st.markdown(
66
+ """Indonesian 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/)."""
67
+ )
68
+
69
+ model_name = st.selectbox('Model',(['GPT-2 Small', 'GPT-2 Medium']))
70
+
71
+ ALL_PROMPTS = list(PROMPT_LIST.keys())+["Custom"]
72
+ prompt = st.selectbox('Prompt', ALL_PROMPTS, index=len(ALL_PROMPTS)-1)
73
+
74
+ if prompt == "Custom":
75
+ prompt_box = "Enter your text here"
76
+ else:
77
+ prompt_box = random.choice(PROMPT_LIST[prompt])
78
+
79
+ text = st.text_area("Enter text", prompt_box)
80
+
81
+ if st.button("Run"):
82
+ with st.spinner(text="Getting results..."):
83
+ st.subheader("Result")
84
+ result = process(text=text,
85
+ model_name=model_name,
86
+ max_len=max_len,
87
+ temp=temp,
88
+ top_k=top_k,
89
+ top_p=top_p,
90
+ do_sample=do_sample,
91
+ server_url=backend)
92
+ st.write(result["result"].replace("\n", " \n"))
93
+ st.text("English translation")
94
+ st.write(translate(result["result"], "en", "id").replace("\n", " \n"))
prompts.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ PROMPT_LIST = {
2
+ "Resep masakan (recipe)": [
3
+ "Berikut adalah cara memasak sate ayam: ",
4
+ "Langkah-langkah membuat kastengel: ",
5
+ "Berikut adalah bahan-bahan membuat nastar: "
6
+ ],
7
+ "Puisi (poetry)": [
8
+ "Aku ingin jadi merpati\nTerbang di langit yang damai\nBernyanyi-nyanyi tentang masa depan\n",
9
+ "Terdiam aku satu persatu dengan tatapan binar\nSenyawa merasuk dalam sukma membuat lara\nKefanaan membentuk kelemahan"
10
+ ],
11
+ "Cerpen (short story)": [
12
+ "Putri memakai sepatunya dengan malas. Kalau bisa, selama seminggu ini ia bolos sekolah saja. Namun, Mama pasti akan marah. Ulangan tengah semester telah selesai. Minggu ini, di sekolah sedang berlangsung pekan olahraga.",
13
+ "\"Wah, hari ini cerah sekali ya,\" ucap Budi ketika ia keluar rumah.",
14
+ "Sewindu sudah kita tak berjumpa, rinduku padamu sudah tak terkira."
15
+ ]
16
+ }
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ requests==2.24.0
3
+ requests-toolbelt==0.9.1
4
+ mtranslate
start.sh ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ if [ "$DEBUG" = true ] ; then
5
+ echo 'Debugging - ON'
6
+ nodemon --exec streamlit run main.py
7
+ else
8
+ echo 'Debugging - OFF'
9
+ streamlit run main.py
10
+ fi