EthanSmurf commited on
Commit
6c081cb
·
1 Parent(s): 0cd833a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+ from screenshot import (
6
+ before_prompt,
7
+ prompt_to_generation,
8
+ after_generation,
9
+ js_save,
10
+ js_load_script,
11
+ )
12
+
13
+ def 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 = {"inputs": input_sentence, "parameters": parameters,"options" : {"use_cache": False} }
24
+
25
+ data = query(payload)
26
+
27
+ if "error" in data:
28
+ return (None, None, f"<span style='color:red'>ERROR: {data['error']} </span>")
29
+
30
+ generation = data[0]["generated_text"].split(input_sentence, 1)[1]
31
+ return (
32
+ before_prompt
33
+ + input_sentence
34
+ + prompt_to_generation
35
+ + generation
36
+ + after_generation,
37
+ data[0]["generated_text"],
38
+ "",
39
+ )
40
+
41
+
42
+ if __name__ == "__main__":
43
+ demo = gr.Blocks()
44
+ with demo:
45
+ with gr.Row():
46
+ gr.Markdown(value=description)
47
+ with gr.Row():
48
+ with gr.Column():
49
+ text = gr.Textbox(
50
+ label="Input",
51
+ value=" ", # should be set to " " when plugged into a real API
52
+ )
53
+ tokens = gr.Slider(1, 64, value=32, step=1, label="Tokens to generate")
54
+
55
+ with gr.Row():
56
+ submit = gr.Button("Submit")
57
+ with gr.Column():
58
+ text_error = gr.Markdown(label="Log information")
59
+ text_out = gr.Textbox(label="Output")
60
+ display_out.set_event_trigger(
61
+ "load",
62
+ fn=None,
63
+ inputs=None,
64
+ outputs=None,
65
+ no_target=True,
66
+ js=js_load_script,
67
+ )
68
+ with gr.Row():
69
+ gr.Examples(examples=examples, inputs=[text, tokens, sampling, sampling2])
70
+
71
+ submit.click(
72
+ inference,
73
+ inputs=[text, tokens, sampling, sampling2],
74
+ outputs=[display_out, text_out, text_error],
75
+ )
76
+
77
+ demo.launch()