File size: 1,725 Bytes
6a29418
 
 
 
 
 
 
 
 
de88b8c
 
6a29418
de88b8c
 
 
 
 
7ec8bbf
de88b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import numpy as np
import requests
import base64
import os

API_ENDPOINT = os.getenv('API_ENDPOINT')
API_KEY = os.getenv('API_KEY')

title = "<h1><center>Markup-to-Image Diffusion Models with Scheduled Sampling</center></h1>"
description = "<center>Yuntian Deng, Noriyuki Kojima, Alexander M. Rush</center>"

with gr.Blocks() as demo:
    gr.Markdown(title)
    gr.Markdown(description)
    with gr.Row():
        with gr.Column(scale=2):
            textbox = gr.Textbox(label=r'Type LaTeX formula below and click "Generate"', lines=1, max_lines=1, placeholder='Type LaTeX formula here and click "Generate"', value=r'\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}')
            submit_btn = gr.Button("Generate", elem_id="btn")
        with gr.Column(scale=3):
            slider = gr.Slider(0, 1000, value=0, label='step (out of 1000)')
            image = gr.Image(label="Rendered Image", show_label=False, elem_id="image")
    inputs = [textbox]
    outputs = [slider, image, submit_btn]
    def infer(formula):
        data = {'formula': formula, 'api_key': API_KEY}
        with requests.post(url=API_ENDPOINT, data=data, timeout=600, stream=True) as r:
            i = 0
            for line in r.iter_lines():
                response = line.decode('ascii').strip()
                r = base64.decodebytes(response.encode('ascii'))
                q = np.frombuffer(r, dtype=np.float32).reshape((64, 320, 3))
                i += 1
                yield i, q, submit_btn.update(visible=False)
            yield i, q, submit_btn.update(visible=True)
    submit_btn.click(fn=infer, inputs=inputs, outputs=outputs)
demo.queue(concurrency_count=20, max_size=200).launch(enable_queue=True)