Spaces:
Runtime error
Runtime error
File size: 9,476 Bytes
208053f 4092a79 208053f 780a591 208053f 780a591 028491e 780a591 208053f 780a591 028491e 780a591 028491e 208053f 15f5208 208053f 028491e |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import gradio as gr
import os
from openai import OpenAI
from generate_prompt import construct_generic_prompt, recommend_config
# Define available tasks and their corresponding datasets
QA = "QA"
SUMMARIZATION = "Summarization"
NLI = "NLI"
NER = "NER"
tasks_datasets = {
QA: ["XQuad", "Indicqa"],
SUMMARIZATION: ["XLSum", "HeSum"],
NLI: ["XNLI"],
NER: ["MasakaNER", "WikiANN"]
}
# List of all languages
languages = [
"English", "Spanish", "French", "German", "Chinese", "Japanese", "Korean", "Italian",
"Portuguese", "Russian", "Arabic", "Hindi", "Bengali", "Turkish", "Vietnamese", "Polish",
"Dutch", "Indonesian", "Malay", "Thai", "Greek", "Swedish", "Hungarian", "Finnish",
"Danish", "Norwegian", "Hebrew", "Czech", "Slovak", "Bulgarian", "Romanian", "Serbian",
"Croatian", "Ukrainian", "Lithuanian", "Latvian", "Estonian", "Filipino", "Icelandic",
"Irish", "Welsh", "Maltese", "Swahili", "Zulu", "Afrikaans"
]
def get_datasets(task):
return tasks_datasets.get(task, [])
with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Row():
gr.Markdown("## Multilingual Prompt Generator")
with gr.Row():
gr.Markdown("### Task Details", elem_id="section-title") # Adding a small title
with gr.Row():
task = gr.Dropdown(label="Task", choices=list(tasks_datasets.keys()), value=QA)
language = gr.Dropdown(label="Source Language", choices=languages, value="English")
model_type = gr.Dropdown(label="Model Type", choices=["Multilingual", "Standard"], value='Multilingual')
config_recommendation = gr.Button("Recommend Configuration")
with gr.Row():
config_prompt = gr.Textbox(label="Recommended Configuration", interactive=False,
placeholder="Recommended Configuration for this scenerio")
with gr.Row():
# examples_selection = gr.Dropdown(["English", "Source"], label="examples", value='English')
# output_selection = gr.Dropdown(["English", "Source"], label="output", value='English')
with gr.Column(scale=2):
gr.Markdown("### Prompt Template", elem_id="section-title") # Adding a small title
# Set the same background style across all components
with gr.Group(elem_id="prompt-background"):
instruction = gr.Textbox(label="Instruction")
with gr.Row(variant="panel"):
zero_shot = gr.Checkbox(label="Zero Shot Setting", value=False)
with gr.Accordion("Few Shot - Select Type of Examples ", open=False, visible=True) as few_shot:
dataset = gr.Dropdown(label="Dataset", choices=tasks_datasets[QA], value="XlSum")
num_examples = gr.Slider(label="Number of examples in context", minimum=1, maximum=10, step=1,
value=3)
with gr.Row(equal_height=True, variant="panel"):
with gr.Accordion(label="Language Component Selection", open=False):
prefix_selection = gr.Dropdown(["English", "Source"], label="prefix", value='English')
context_selection = gr.Dropdown(["English", "Source"], label="context", value='English')
examples_selection = gr.Dropdown(["English", "Source"], label="examples", value='English')
output_selection = gr.Dropdown(["English", "Source"], label="output", value='English')
# Accordion for Few Shot example selection
with gr.Accordion(label="Prompt Input Data", open=False):
question = gr.Textbox(label="Question", visible=True)
context = gr.Textbox(label="Context", visible=True)
text = gr.Textbox(label="Text", visible=False)
sentence = gr.Textbox(label="Sentence", visible=False)
hypothesis = gr.Textbox(label="Hypothesis", visible=False)
premise = gr.Textbox(label="Premise", visible=False)
generate_button = gr.Button("Generate Prompt")
with gr.Row():
prompt = gr.Textbox(label="Generated Prompt", interactive=False, placeholder="Generated prompt will appear here.")
def update_datasets(selected_task):
return gr.Dropdown(choices=get_datasets(selected_task))
def toggle_task_inputs(selected_task):
if selected_task == QA:
return (
gr.update(visible=True), gr.update(visible=True), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
)
elif selected_task == SUMMARIZATION:
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
)
elif selected_task == NER:
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
)
else:
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
)
def toggle_num_examples(zero_shot_value):
# If zero_shot is True, hide the num_examples slider
return gr.update(visible=not zero_shot_value)
def update_language_selection(language):
return gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language}))
def generatePrompt(instruction, num_examples, zero_shot,
task, selected_language, dataset, prefix_selection, context_selection, examples_selection, output_selection,
text, question, context, sentence, hypothesis, premise):
config = {'prefix': str.lower(prefix_selection), 'input': str.lower(context_selection), 'context': str.lower(examples_selection), 'output': str.lower(output_selection)}
if task == QA:
text_example = {
'context': context,
'question': question,
}
elif task == SUMMARIZATION:
text_example = {
'text': text,
}
elif task == NER:
text_example = {
'tokens': sentence,
'ner_tags': ''
}
else:
text_example = {
'hypothesis': hypothesis,
'premise': premise
}
prompt = construct_generic_prompt(task, instruction, text_example, zero_shot, num_examples, selected_language, dataset, config)
return prompt
def respond(message, openai_key, url, chat_history, model, config_input, config_prefix, config_context,
config_output, task, dataset, language, num_examples, zero_shot):
os.environ["OPENAI_API_KEY"] = openai_key
client = OpenAI()
config = {
"input": config_input,
"prefix": config_prefix,
"context": config_context.split(', '),
"output": config_output,
"language": language,
"num_examples": num_examples,
"zero_shot": zero_shot
}
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": message},
{"type": "image_url", "image_url": url},
{"type": "config", "config": config},
{"type": "task", "text": task},
{"type": "dataset", "text": dataset}
],
},
],
max_tokens=1000,
)
out = response.choices[0].message.content
chat_history.append((message, out))
return "", chat_history
# Bind functions to dropdown changes and button click
# task.change(fn=update_datasets, outputs=dataset)
language.change(fn=update_language_selection, inputs=language, outputs=[prefix_selection, context_selection, examples_selection, output_selection])
zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=few_shot)
zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=num_examples)
task.change(fn=update_datasets, inputs=task, outputs=dataset)
task.change(fn=toggle_task_inputs, inputs=task, outputs=[
question, context, text, sentence, hypothesis, premise,
])
generate_button.click(
generatePrompt,
inputs=[
instruction, num_examples, zero_shot,
task, language, dataset, prefix_selection, context_selection, examples_selection, output_selection,
text, question, context, sentence, hypothesis, premise
],
outputs=[prompt]
)
config_recommendation.click(
recommend_config,
inputs=[
task,
language,
model_type
],
outputs=[config_prompt]
)
if __name__ == '__main__':
demo.launch(share=True) |