osanseviero
commited on
Commit
•
3a00052
1
Parent(s):
5c9b9be
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
from huggingface_hub import HfApi
|
7 |
+
|
8 |
+
inputs_description = """This is a description of the inputs that the prompt expects.
|
9 |
+
|
10 |
+
{{input_var}}: {{Description}}
|
11 |
+
...
|
12 |
+
"""
|
13 |
+
usage_description = """Below is a code snippet for how to use the prompt.
|
14 |
+
|
15 |
+
```
|
16 |
+
{{Code snippet}}
|
17 |
+
```
|
18 |
+
"""
|
19 |
+
input_variables_description = "Comma-separated list of input variables. E.g. question,name"
|
20 |
+
template_description = "Imagine you're a teacher called {name}. A student asks the following question: {question}. What do you answer?"
|
21 |
+
|
22 |
+
api = HfApi()
|
23 |
+
|
24 |
+
def submit(name, description, inputs_description, usage_description, input_variables, template, token):
|
25 |
+
variables = input_variables.split(",")
|
26 |
+
|
27 |
+
card = f"""
|
28 |
+
---
|
29 |
+
tags:
|
30 |
+
- langchain
|
31 |
+
- prompt
|
32 |
+
---
|
33 |
+
|
34 |
+
# Description of {name}
|
35 |
+
|
36 |
+
{description}
|
37 |
+
|
38 |
+
## Inputs
|
39 |
+
|
40 |
+
{inputs_description}
|
41 |
+
|
42 |
+
## Usage
|
43 |
+
|
44 |
+
{usage_description}
|
45 |
+
"""
|
46 |
+
|
47 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
48 |
+
with open(os.path.join(tmpdir, "prompt.json"), "w") as f:
|
49 |
+
data = {
|
50 |
+
'input_variables': variables,
|
51 |
+
'output_parser': None,
|
52 |
+
"template": template,
|
53 |
+
"template_format": "f-string"
|
54 |
+
}
|
55 |
+
json.dump(data, f)
|
56 |
+
|
57 |
+
with open(os.path.join(tmpdir, "README.md"), "w") as f:
|
58 |
+
f.write(card)
|
59 |
+
|
60 |
+
name = name.replace(" ", "_")
|
61 |
+
res = api.upload_folder(
|
62 |
+
repo_id="osanseviero/langchain_hub_test",
|
63 |
+
folder_path=tmpdir,
|
64 |
+
path_in_repo=f"prompts/{name}",
|
65 |
+
token=token,
|
66 |
+
repo_type="dataset",
|
67 |
+
create_pr=True
|
68 |
+
)
|
69 |
+
return "Success! Check out https://huggingface.co/datasets/osanseviero/langchain_hub_test/discussions"
|
70 |
+
|
71 |
+
with gr.Blocks() as form:
|
72 |
+
gr.Markdown("# LangChain Hub Form")
|
73 |
+
gr.Markdown("## Submit a prompt")
|
74 |
+
name = gr.Textbox(lines=1, placeholder="Name for the prompt", label="Name")
|
75 |
+
high_level_description = gr.Textbox(lines=2, placeholder="High level text description of the prompt, including use cases.", interactive=True, label="Description")
|
76 |
+
inputs_description = gr.Textbox(lines=3, value=inputs_description, interactive=True, label="Inputs Description")
|
77 |
+
usage_description = gr.Textbox(lines=3, value=usage_description, interactive=True, label="Usage Description")
|
78 |
+
|
79 |
+
input_variables = gr.Textbox(value=input_variables_description, interactive=True, label="Input Variables")
|
80 |
+
template = gr.Textbox(lines=3, value=template_description, interactive=True, label="Template (use the input variables with {})")
|
81 |
+
token = gr.Textbox(label="Write Token (from https://hf.co/settings/tokens)", type="password")
|
82 |
+
|
83 |
+
btn = gr.Button(value="Open PR")
|
84 |
+
inputs = [name, high_level_description, inputs_description, usage_description, input_variables, template, token]
|
85 |
+
output = gr.Textbox()
|
86 |
+
btn.click(submit, inputs=inputs, outputs=[output])
|
87 |
+
|
88 |
+
form.launch(debug=True)
|