Spaces:
Runtime error
Runtime error
import json | |
import os | |
import tempfile | |
import gradio as gr | |
from huggingface_hub import HfApi | |
inputs_description = """This is a description of the inputs that the prompt expects. | |
{{input_var}}: {{Description}} | |
... | |
""" | |
usage_description = """Below is a code snippet for how to use the prompt. | |
``` | |
{{Code snippet}} | |
``` | |
""" | |
input_variables_description = "Comma-separated list of input variables. E.g. question,name" | |
template_description = "Imagine you're a teacher called {name}. A student asks the following question: {question}. What do you answer?" | |
api = HfApi() | |
def submit(name, description, inputs_description, usage_description, input_variables, template, token): | |
variables = input_variables.split(",") | |
card = f""" | |
--- | |
tags: | |
- langchain | |
- prompt | |
--- | |
# Description of {name} | |
{description} | |
## Inputs | |
{inputs_description} | |
## Usage | |
{usage_description} | |
""" | |
with tempfile.TemporaryDirectory() as tmpdir: | |
with open(os.path.join(tmpdir, "prompt.json"), "w") as f: | |
data = { | |
'input_variables': variables, | |
'output_parser': None, | |
"template": template, | |
"template_format": "f-string" | |
} | |
json.dump(data, f) | |
with open(os.path.join(tmpdir, "README.md"), "w") as f: | |
f.write(card) | |
name = name.replace(" ", "_") | |
res = api.upload_folder( | |
repo_id="osanseviero/langchain_hub_test", | |
folder_path=tmpdir, | |
path_in_repo=f"prompts/{name}", | |
token=token, | |
repo_type="dataset", | |
create_pr=True | |
) | |
return "Success! Check out https://huggingface.co/datasets/osanseviero/langchain_hub_test/discussions" | |
with gr.Blocks() as form: | |
gr.Markdown("# LangChain Hub Form") | |
gr.Markdown("## Submit a prompt") | |
name = gr.Textbox(lines=1, placeholder="Name for the prompt", label="Name") | |
high_level_description = gr.Textbox(lines=2, placeholder="High level text description of the prompt, including use cases.", interactive=True, label="Description") | |
inputs_description = gr.Textbox(lines=3, value=inputs_description, interactive=True, label="Inputs Description") | |
usage_description = gr.Textbox(lines=3, value=usage_description, interactive=True, label="Usage Description") | |
input_variables = gr.Textbox(value=input_variables_description, interactive=True, label="Input Variables") | |
template = gr.Textbox(lines=3, value=template_description, interactive=True, label="Template (use the input variables with {})") | |
token = gr.Textbox(label="Write Token (from https://hf.co/settings/tokens)", type="password") | |
btn = gr.Button(value="Open PR") | |
inputs = [name, high_level_description, inputs_description, usage_description, input_variables, template, token] | |
output = gr.Textbox() | |
btn.click(submit, inputs=inputs, outputs=[output]) | |
form.launch(debug=True) |