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

added api url

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -5,28 +5,44 @@ 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
 
@@ -86,9 +102,8 @@ if st.button("Run"):
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"))
 
5
  import streamlit as st
6
  import random
7
 
8
+ headers = {"Authorization": f"Bearer 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"}
9
+ MODELS = {
10
+ "GPT-2 Small": {
11
+ "url": "https://api-inference.huggingface.co/models/flax-community/gpt2-small-indonesian"
12
+ },
13
+ "GPT-2 Medium": {
14
+ "url": "https://api-inference.huggingface.co/models/flax-community/gpt2-medium-indonesian"
15
+ },
16
+ }
17
+
18
+
19
+ def query(payload, model_name):
20
+ data = json.dumps(payload)
21
+ response = requests.request("POST", MODELS[model_name]["url"], headers=headers, data=data)
22
+ return json.loads(response.content.decode("utf-8"))
23
+
24
 
25
  def process(text: str,
26
  model_name: str,
27
  max_len: int,
28
  temp: float,
29
  top_k: int,
30
+ top_p: float):
31
+
32
+ payload = {
33
+ "inputs": text,
34
+ "parameters": {
35
+ "max_new_tokens": max_len,
36
+ "top_k": top_k,
37
+ "top_p": top_p,
38
+ "temperature": temp,
39
+ "repetition_penalty": 2.0,
40
+ },
41
+ "options": {
42
+ "use_cache": True,
43
+ }
44
+ }
45
+ return query(payload, model_name)
46
 
47
  st.set_page_config(page_title="Indonesian GPT-2 Demo")
48
 
 
102
  max_len=max_len,
103
  temp=temp,
104
  top_k=top_k,
105
+ top_p=top_p)[0]["generated_text"]
106
+
107
+ st.write(result.replace("\n", " \n"))
 
108
  st.text("English translation")
109
+ st.write(translate(result, "en", "id").replace("\n", " \n"))