ThomasSimonini's picture
Update app.py
513e175
raw
history blame
No virus
2.9 kB
import gradio as gr
from gradio.inputs import Textbox, Slider
import requests
# Template
title = "A conversation with some NPC in a Tavern 🍻"
description = ""
article = """
<p> If you liked don't forget to πŸ’– the project πŸ₯° </p>
<h2> Parameters: </h2>
<ul>
<li><i>message</i>: what you want to say to the NPC.</li>
<li><i>npc_name</i>: name of the NPC.</li>
<li><i>npc_prompt</i>: prompt of the NPC, we can modify it to see if results are better.</li>
<li><i>top_p</i>: control how deterministic the model is in generating a response.</li>
<li><i>temperature</i>: (sampling temperature) higher values means the model will take more risks.</li>
<li><i>max_new_tokens</i>: Max number of tokens in generation.</li>
</ul>
<img src='http://www.simoninithomas.com/test/gandalf.jpg', alt="Gandalf"/>"""
theme="huggingface"
context_setup = prompt
context = context_setup
interlocutor_names = ["Player", npc_name]
# Builds the prompt from what previously happened
def build_prompt(conversation, context):
prompt = context + "\n"
for user_msg, resp_msg in conversation:
line = "\n- " + interlocutor_names[0] + ":" + user_msg
prompt += line
line = "\n- " + interlocutor_names[1] + ":" + resp_msg
prompt += line
prompt += ""
return prompt
# Recognize what the model said, if it used the correct format
def clean_chat_output(txt, prompt):
delimiter = "\n- "+interlocutor_names[0]
output = txt.replace(prompt, '')
output = output[:output.find(delimiter)]
return output
def chat(top_p, temperature, max_new_tokens, message):
history = gr.get_state() or []
history.append((message, ""))
gr.set_state(history)
conversation = history
prompt = build_prompt(conversation, context)
# Build JSON
json_ = {"inputs": prompt,
"parameters":
{
"top_p": top_p,
"temperature": temperature,
"max_new_tokens": max_new_tokens,
"return_full_text": False
}}
output = query(json_)
output = output[0]['generated_text']
answer = clean_chat_output(output, prompt)
response = answer
history[-1] = (message, response)
gr.set_state(history)
return response, history
#io = gr.Interface.load("huggingface/EleutherAI/gpt-j-6B")
iface = gr.Interface(fn=chat,
inputs=[Textbox(label="message"),
Textbox(label="npc_name"),
Textbox(label="prompt"),
Slider(minimum=0.5, maximum=1, step=0.05, default=0.9, label="top_p"),
Slider(minimum=0.5, maximum=1.5, step=0.1, default=1.1, label="temperature"),
Slider(minimum=20, maximum=250, step=10, default=50, label="max_new_tokens"),
"text",
"state"],
outputs=["chatbot","state"],
#examples="",
allow_screenshot=True,
allow_flagging=True,
title=title,
article=article,
theme=theme)
if __name__ == "__main__":
iface.launch()