Spaces:
Runtime error
Runtime error
osanseviero
commited on
Commit
•
94a93b4
1
Parent(s):
f08e1f0
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
|
6 |
+
title = "BLOOM"
|
7 |
+
description = "Gradio Demo for BLOOM. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
|
8 |
+
|
9 |
+
API_URL = "https://hfbloom.ngrok.io/generate"
|
10 |
+
HF_API_TOKEN = st.secrets["HF_API_TOKEN"]
|
11 |
+
|
12 |
+
|
13 |
+
examples = [
|
14 |
+
['A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses the word whatpu is: We were traveling in Africa and we saw these very cute whatpus. To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses the word farduddle is:']
|
15 |
+
]
|
16 |
+
|
17 |
+
def safe_text(text):
|
18 |
+
text = text.replace('%', '\\%25')
|
19 |
+
text = text.replace('#', '\\%23')
|
20 |
+
text = text.replace('+', '\\%2B')
|
21 |
+
text = text.replace('*', '\\%2A')
|
22 |
+
text = text.replace('&', '\\%26')
|
23 |
+
text = re.sub(r"([$_*\[\]()~`>\#\+\-=|\.!{}])", r"\\\1", text)
|
24 |
+
return f"<pre>{text}</pre>
|
25 |
+
|
26 |
+
|
27 |
+
def query(payload):
|
28 |
+
response = requests.request("POST", API_URL, json=payload)
|
29 |
+
return json.loads(response.content.decode("utf-8"))
|
30 |
+
|
31 |
+
def inference(input_sentence, max_length=16, temperature=0.1, greedy_decoding=False, top_k=0, top_p=1, seed=42):
|
32 |
+
top_k = None if top_k == 0 else top_k
|
33 |
+
payload = {"inputs": input_sentence,
|
34 |
+
"parameters": {"max_new_tokens": max_length, "top_k": top_k, "top_p": top_p, "temperature": temperature,
|
35 |
+
"do_sample": not greedy_decoding, "seed": seed}}
|
36 |
+
data = query(
|
37 |
+
payload
|
38 |
+
)
|
39 |
+
return data[0]['generated_text'][len(input_sentence):]
|
40 |
+
|
41 |
+
|
42 |
+
gr.Interface(
|
43 |
+
inference,
|
44 |
+
[gr.inputs.Textbox(label="Input")],
|
45 |
+
gr.outputs.Textbox(label="Output"),
|
46 |
+
examples=examples,
|
47 |
+
# article=article,
|
48 |
+
title=title,
|
49 |
+
description=description).launch(enable_queue=True)
|