CoolFace
Apppublic

mlcocdav/GreenHack

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py75 linesDownload Raw Back to root
1import gradio as gr2import os3import openai4 5from backend import PromptHandler6from prompt_engineering import NLP_TASKS7 8 9def dummy_prompt(prompt, task_type, speed, quality):10    return f"{prompt} {task_type} {speed} {quality}", "text2", "text3", "text4", "text5"11 12 13def dummy_calculate_price(prompt, task_type, speed, quality):14    return (len(prompt) + speed + quality) * (NLP_TASKS.index(task_type) + 1)15 16 17if __name__ == "__main__":18    handler = PromptHandler()19    with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.green,20                                           secondary_hue=gr.themes.colors.green)) as demo:21        with gr.Row():22            with gr.Column():23                with gr.Row():24                    gr.Image(value="logo.png", show_label=False,25                             shape=[150, 30], interactive=False)26            with gr.Column():27                pass28            with gr.Column():29                pass30        with gr.Row():31            with gr.Column():32                prompt = gr.Textbox(label="Prompt",33                                    placeholder="Enter your prompt here ...")34                task_type = gr.Dropdown(35                    label="Task type", choices=NLP_TASKS, value=NLP_TASKS[0],36                    interactive=True, allow_custom_value=False)37                speed = gr.Radio(list(range(1, 6)), label="Speed", value=3,38                                 interactive=True)39                quality = gr.Radio(list(range(1, 6)), label="Quality", value=3,40                                   interactive=True)41 42                with gr.Row():43                    simplified = gr.Checkbox(value=True, label="Simplify prompt", info="Simplify prompt", interactive=True)44                    out = gr.Textbox(label="Price per 1000 tokens - other providers*")45 46                prompt.change(handler.get_price_dollars, [prompt, task_type, speed, quality], out)47                task_type.change(handler.get_price_dollars, [prompt, task_type, speed, quality], out)48                speed.change(handler.get_price_dollars, [prompt, task_type, speed, quality], out)49                quality.change(handler.get_price_dollars, [prompt, task_type, speed, quality], out)50 51                try_btn = gr.Button("Try model")52            with gr.Column():53                model_output = gr.Textbox(label="Model response", lines=6)54                with gr.Row():55                    savings = gr.Textbox(label="SimpleGen price")56                    model_name = gr.Textbox(label="Selected model")57                    inference_speed = gr.Textbox(label="Inference time (ms)")58 59                shortened_prompt = gr.Textbox(label="Simplified Prompt", lines=5)60                    61                try_btn.click(62                    fn=handler.generate,63                    inputs=[prompt, task_type, speed, quality, simplified],64                    outputs=[model_output, inference_speed, model_name,65                             shortened_prompt, savings],66                    api_name="infer")67                with gr.Row():68                    like_btn = gr.Button("Wow, that's cool!",69                                         variant="primary")70                    dislike_btn = gr.Button("Nah, I want it better.")71 72        gr.Markdown(73            "\*  official price from other providers for the model with similar quality and speed")74    demo.launch()75