text-generation-webui/modules/api.py

53 lines
1.5 KiB
Python
Raw Normal View History

import json
import gradio as gr
from modules import shared
from modules.text_generation import generate_reply
2023-04-14 17:11:47 +02:00
# set this to True to rediscover the fn_index using the browser DevTools
VISIBLE = False
def generate_reply_wrapper(string):
2023-04-16 22:33:28 +02:00
# Provide defaults so as to not break the API on the client side when new parameters are added
generate_params = {
2023-04-16 22:33:28 +02:00
'max_new_tokens': 200,
'do_sample': True,
2023-04-16 22:33:28 +02:00
'temperature': 0.5,
'top_p': 1,
'typical_p': 1,
2023-04-16 22:33:28 +02:00
'repetition_penalty': 1.1,
'encoder_repetition_penalty': 1,
2023-04-16 22:33:28 +02:00
'top_k': 0,
'min_length': 0,
'no_repeat_ngram_size': 0,
'num_beams': 1,
'penalty_alpha': 0,
'length_penalty': 1,
'early_stopping': False,
2023-04-16 22:33:28 +02:00
'seed': -1,
'add_bos_token': True,
2023-04-19 18:52:21 +02:00
'custom_stopping_strings': '',
2023-04-16 22:33:28 +02:00
'truncation_length': 2048,
'ban_eos_token': False,
'skip_special_tokens': True,
2023-04-19 18:52:21 +02:00
'stopping_strings': [],
}
params = json.loads(string)
2023-04-16 22:33:28 +02:00
generate_params.update(params[1])
2023-04-19 18:52:21 +02:00
stopping_strings = generate_params.pop('stopping_strings')
for i in generate_reply(params[0], generate_params, stopping_strings=stopping_strings):
yield i
def create_apis():
2023-04-14 17:11:47 +02:00
t1 = gr.Textbox(visible=VISIBLE)
t2 = gr.Textbox(visible=VISIBLE)
dummy = gr.Button(visible=VISIBLE)
input_params = [t1]
output_params = [t2] + [shared.gradio[k] for k in ['markdown', 'html']]
dummy.click(generate_reply_wrapper, input_params, output_params, api_name='textgen')