Team Ai
Apppublic

EthanSmurf/MathsWithBloom

sourceHugging Facebigscience-bloom-rail-1.0updated 4y agoView on Hugging Face
0likes
app.py83 linesDownload Raw Back to root
1import gradio as gr2import requests3import json4import os5from screenshot import (6    before_prompt,7    prompt_to_generation,8    after_generation,9    js_save,10    js_load_script,11)12 13def inference(input_sentence, max_length, seed=42):14       parameters = {15            "max_new_tokens": max_length,16            "do_sample": False,17            "seed": seed,18            "early_stopping": False,19            "length_penalty": 0.0,20            "eos_token_id": None,21       }22 23    payload = {24        "inputs": input_sentence, 25        "parameters": parameters,26        "options" : {27            "use_cache": False28            } 29         }30 31    data = query(payload)32 33    if "error" in data:34        return (None, None, f"<span style='color:red'>ERROR: {data['error']} </span>")35 36    generation = data[0]["generated_text"].split(input_sentence, 1)[1]37    return (38        before_prompt39        + input_sentence40        + prompt_to_generation41        + generation42        + after_generation,43        data[0]["generated_text"],44        "",45    )46 47 48if __name__ == "__main__":49    demo = gr.Blocks()50    with demo:51        with gr.Row():52            gr.Markdown(value=description)53        with gr.Row():54            with gr.Column():55                text = gr.Textbox(56                    label="Input",57                    value=" ",  # should be set to " " when plugged into a real API58                )59                tokens = gr.Slider(1, 64, value=32, step=1, label="Tokens to generate")60 61                with gr.Row():62                    submit = gr.Button("Submit")63            with gr.Column():64                text_error = gr.Markdown(label="Log information")65                text_out = gr.Textbox(label="Output")66                display_out.set_event_trigger(67                    "load",68                    fn=None,69                    inputs=None,70                    outputs=None,71                    no_target=True,72                    js=js_load_script,73                )74        with gr.Row():75            gr.Examples(examples=examples, inputs=[text, tokens, sampling, sampling2])76 77        submit.click(78            inference,79            inputs=[text, tokens, sampling, sampling2],80            outputs=[display_out, text_out, text_error],81        )82 83    demo.launch()