import gradio as gr import re import requests import json title = "BLOOM" 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." API_URL = "https://hfbloom.ngrok.io/generate" HF_API_TOKEN = st.secrets["HF_API_TOKEN"] examples = [ ['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:'] ] def safe_text(text): text = text.replace('%', '\\%25') text = text.replace('#', '\\%23') text = text.replace('+', '\\%2B') text = text.replace('*', '\\%2A') text = text.replace('&', '\\%26') text = re.sub(r"([$_*\[\]()~`>\#\+\-=|\.!{}])", r"\\\1", text) return f"
{text}
def query(payload): response = requests.request("POST", API_URL, json=payload) return json.loads(response.content.decode("utf-8")) def inference(input_sentence, max_length=16, temperature=0.1, greedy_decoding=False, top_k=0, top_p=1, seed=42): top_k = None if top_k == 0 else top_k payload = {"inputs": input_sentence, "parameters": {"max_new_tokens": max_length, "top_k": top_k, "top_p": top_p, "temperature": temperature, "do_sample": not greedy_decoding, "seed": seed}} data = query( payload ) return data[0]['generated_text'][len(input_sentence):] gr.Interface( inference, [gr.inputs.Textbox(label="Input")], gr.outputs.Textbox(label="Output"), examples=examples, # article=article, title=title, description=description).launch(enable_queue=True)