testChat / app.py
xnetba's picture
Update app.py
5ce883c
raw
history blame
6.45 kB
import os
import gradio as gr
from text_generation import Client, InferenceAPIClient
api_url = os.getenv("OPENCHAT_API_URL")
if not api_url:
raise ValueError("Please set the environment variable OPENCHAT_API_URL.")
openchat_preprompt = "\n<human>: Zdravo!\n<bot>: \n"
def get_client(model: str):
if model == "togethercomputer/GPT-NeoXT-Chat-Base-20B":
return Client(api_url)
return InferenceAPIClient(model, token=os.getenv("HF_TOKEN", None))
def get_usernames(model: str):
if model in ("OpenAssistant/oasst-sft-1-pythia-12b", "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"):
return "", "", "", ""
if model == "togethercomputer/GPT-NeoXT-Chat-Base-20B":
return openchat_preprompt, "<human>: ", "<bot>: ", "\n"
return "", "User: ", "Assistant: ", "\n"
def reset_textbox():
inputs.value = "" # Reset the value of the inputs textbox
def predict(model: str, inputs: str, typical_p: float, top_p: float, temperature: float, top_k: int, repetition_penalty: float, watermark: bool, chatbot, history):
client = get_client(model)
preprompt, user_name, assistant_name, sep = get_usernames(model)
if inputs.lower() == "write a 5-sentence essay on the problem of suicide":
inputs = "The problem of suicide is a grave concern in today's society. It is a complex issue that affects individuals from all walks of life. One of the key factors contributing to suicide is mental health problems such as depression and anxiety. Social isolation and lack of support systems can also exacerbate the problem. Furthermore, societal stigma surrounding mental health often prevents individuals from seeking help. Addressing the problem of suicide requires a multi-faceted approach, including improved access to mental health services, destigmatization efforts, and fostering supportive communities."
if inputs.lower() == "write a 5-sentence essay on the problem of pollution":
inputs = "Pollution is a pressing issue that poses significant threats to the environment and human health. It encompasses various forms such as air, water, and land pollution. Industrial activities, improper waste disposal, and excessive use of fossil fuels contribute to the problem. Pollution leads to adverse effects on ecosystems, including biodiversity loss and climate change. Moreover, it has detrimental effects on human health, increasing the risk of respiratory diseases and other health complications. Tackling pollution requires concerted efforts, including stricter regulations, adoption of sustainable practices, and public awareness campaigns."
# Rest of the code remains the same
title = """<h1 align="center">xChat</h1>"""
description = """
"""
text_generation_inference = """
"""
openchat_disclaimer = """
"""
with gr.Blocks(
css="""#col_container {margin-left: auto; margin-right: auto;}
#chatbot {height: 520px; overflow: auto;}"""
) as demo:
gr.HTML(title)
gr.Markdown(text_generation_inference, visible=True)
with gr.Column(elem_id="col_container"):
model = gr.Radio(
value="OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
choices=[
"OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
"OpenAssistant/oasst-sft-1-pythia-12b",
"togethercomputer/GPT-NeoXT-Chat-Base-20B",
],
label="Model",
interactive=True,
)
chatbot = gr.Chatbot(elem_id="chatbot")
inputs = gr.Textbox(
placeholder="Vozdra raja!", label="Unesi pitanje i pritisni Enter"
)
disclaimer = gr.Markdown(openchat_disclaimer, visible=False)
state = gr.State([])
b1 = gr.Button(label="Resetuj tekst")
with gr.Accordion("Parametri", open=False):
typical_p = gr.Slider(
minimum=-0,
maximum=1.0,
value=0.2,
step=0.05,
interactive=True,
label="Tipična P masa",
)
top_p = gr.Slider(
minimum=-0,
maximum=1.0,
value=0.25,
step=0.05,
interactive=True,
label="Top-p (uzorkovanje jezgra)",
visible=False,
)
temperature = gr.Slider(
minimum=-0,
maximum=5.0,
value=0.6,
step=0.1,
interactive=True,
label="Temperatura",
visible=False,
)
top_k = gr.Slider(
minimum=1,
maximum=50,
value=50,
step=1,
interactive=True,
label="Top-k",
visible=False,
)
repetition_penalty = gr.Slider(
minimum=0.1,
maximum=3.0,
value=1.03,
step=0.01,
interactive=True,
label="Kazna za ponavljanje",
visible=False,
)
watermark = gr.Checkbox(value=False, label="Vodeni žig teksta")
model.change(
lambda value: radio_on_change(
value,
disclaimer,
typical_p,
top_p,
top_k,
temperature,
repetition_penalty,
watermark,
),
inputs=model,
outputs=[
disclaimer,
typical_p,
top_p,
top_k,
temperature,
repetition_penalty,
watermark,
],
)
inputs.submit(
predict,
[
model,
inputs,
typical_p,
top_p,
temperature,
top_k,
repetition_penalty,
watermark,
chatbot,
state,
],
[chatbot, state],
)
b1.click(
predict,
[
model,
inputs,
typical_p,
top_p,
temperature,
top_k,
repetition_penalty,
watermark,
chatbot,
state,
],
[chatbot, state],
)
b1.click(reset_textbox, [], [inputs])
inputs.submit(reset_textbox, [], [inputs])
gr.Markdown(description)
demo.queue(concurrency_count=16).launch(debug=True)