Spaces:
Runtime error
Runtime error
File size: 6,372 Bytes
f527676 19ada47 f527676 872b774 f527676 19ada47 f527676 7b8690f f527676 7b8690f f527676 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
# %% auto 0
__all__ = ['title', 'description', 'query_chat_api', 'inference_chat']
# %% app.ipynb 0
import gradio as gr
import requests
import json
import requests
# %% app.ipynb 1
def query_chat_api(
model_id,
inputs,
temperature,
top_p
):
API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
headers = {"Authorization": "Bearer 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", "x-wait-for-model": "1"}
payload = {
"inputs": inputs,
"parameters": {
"temperature": temperature,
"top_p": top_p,
"do_sample": True,
"max_length": 512,
},
}
response = requests.post(API_URL, json=payload, headers=headers)
if response.status_code == 200:
return response.json()
else:
return "Error: " + response.text
# %% app.ipynb 4
def inference_chat(
model_id,
prompt_template,
text_input,
temperature,
top_p,
history=[],
):
with open(f"prompt_templates/{prompt_template}.json", "r") as f:
prompt_template = json.load(f)
history.append(text_input)
inputs = prompt_template["prompt"].format(human_input=text_input)
output = query_chat_api(model_id, inputs, temperature, top_p)
history.append(" " + output[0]["generated_text"])
chat = [
(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)
] # convert to tuples of list
return {chatbot: chat, state: history}
# %% app.ipynb 12
title = """<h1 align="center">Chatty Language Models</h1>"""
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
```
User: <utterance>
Assistant: <utterance>
User: <utterance>
Assistant: <utterance>
...
```
In this app, you can explore the outputs of several language models conditioned on different conversational prompts. The models are trained on different datasets and have different objectives, so they will have different personalities and strengths.
So far, the following prompts are available:
* `langchain_default`: The default prompt used in the [LangChain library](https://github.com/hwchase17/langchain/blob/bc53c928fc1b221d0038b839d111039d31729def/langchain/chains/conversation/prompt.py#L4). Around 67 tokens long.
* `openai_chatgpt`: The prompt used in the OpenAI ChatGPT model. Around 261 tokens long.
* `deepmind_sparrow`: The prompt used in the DeepMind Sparrow model (Table 7 of [their paper](https://arxiv.org/abs/2209.14375)). Around 880 tokens long.
* `deepmind_gopher`: The prompt used in the DeepMind Gopher model (Table A30 of [their paper](https://arxiv.org/abs/2112.11446)). Around 791 tokens long.
* `anthropic_hhh`: The prompt used in the [Anthropic HHH models](https://gist.github.com/jareddk/2509330f8ef3d787fc5aaac67aab5f11#file-hhh_prompt-txt). A whopping 6,341 tokens long!
As you can see, most of these prompts exceed the maximum context size of models like Flan-T5, so an error usually means the Inference API has timed out.
"""
# %% app.ipynb 13
with gr.Blocks(
css="""
.message.svelte-w6rprc.svelte-w6rprc.svelte-w6rprc {font-size: 20px; margin-top: 20px}
#component-21 > div.wrap.svelte-w6rprc {height: 600px;}
"""
) as iface:
state = gr.State([])
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column(scale=1):
model_id = gr.Dropdown(
choices=["google/flan-t5-xl"],
value="google/flan-t5-xl",
label="Model",
interactive=True,
)
prompt_template = gr.Dropdown(
choices=[
"langchain_default",
"openai_chatgpt",
"deepmind_sparrow",
"deepmind_gopher",
"anthropic_hhh",
],
value="langchain_default",
label="Prompt Template",
interactive=True,
)
temperature = gr.Slider(
minimum=0.5,
maximum=3.0,
value=1.0,
step=0.1,
interactive=True,
label="Temperature",
)
top_p = gr.Slider(
minimum=-0,
maximum=1.0,
value=0.95,
step=0.05,
interactive=True,
label="Top-p (nucleus sampling)",
)
with gr.Column(scale=1.8):
with gr.Row():
with gr.Column(
scale=1.5,
):
chatbot = gr.Chatbot(
label="Chat Output",
)
with gr.Column():
chat_input = gr.Textbox(lines=1, label="Chat Input")
chat_input.submit(
inference_chat,
[
model_id,
prompt_template,
chat_input,
temperature,
top_p,
state,
],
[chatbot, state],
)
with gr.Row():
clear_button = gr.Button(value="Clear", interactive=True)
clear_button.click(
lambda: ("", [], []),
[],
[chat_input, chatbot, state],
queue=False,
)
submit_button = gr.Button(
value="Submit", interactive=True, variant="primary"
)
submit_button.click(
inference_chat,
[
model_id,
prompt_template,
chat_input,
temperature,
top_p,
state,
],
[chatbot, state],
)
iface.launch()
|