Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +30 -49
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,62 +1,43 @@
|
|
| 1 |
-
#
|
| 2 |
-
# Adapted from LangChain
|
| 3 |
-
# [BashChain](https://langchain.readthedocs.io/en/latest/modules/chains/examples/llm_bash.html)
|
| 4 |
-
|
| 5 |
-
import minichain
|
| 6 |
-
|
| 7 |
-
# Prompt that asks LLM to produce a bash command.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
class CLIPrompt(minichain.TemplatePrompt):
|
| 11 |
-
template_file = "bash.pmpt.tpl"
|
| 12 |
-
|
| 13 |
-
def parse(self, out: str, inp):
|
| 14 |
-
out = out.strip()
|
| 15 |
-
assert out.startswith("```bash")
|
| 16 |
-
return out.split("\n")[1:-1]
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Prompt that runs the bash command.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
class BashPrompt(minichain.Prompt):
|
| 23 |
-
def prompt(self, inp) -> str:
|
| 24 |
-
return ";".join(inp).replace("\n", "")
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
question = (
|
| 34 |
|
| 35 |
-
|
| 36 |
-
prompt = CLIPrompt(backend.OpenAI()).chain(BashPrompt(backend.BashProcess()))
|
| 37 |
|
| 38 |
|
| 39 |
-
prompt
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
).launch()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# {"question": "list the files in the directory"}, """```bash\nls\n```"""
|
| 52 |
-
# )
|
| 53 |
-
# # -
|
| 54 |
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
# BashPrompt().show(["ls", "cat file.txt"], "hello")
|
| 58 |
-
# # -
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
# minichain.show_log("bash.log")
|
|
|
|
| 1 |
+
# + tags=["hide_inp"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
desc = """
|
| 4 |
+
### Bash Command Suggestion
|
| 5 |
|
| 6 |
+
Chain that ask for a command-line question and then runs the bash command. [[Code](https://github.com/srush/MiniChain/blob/main/examples/bash.py)]
|
| 7 |
|
| 8 |
+
(Adapted from LangChain [BashChain](https://langchain.readthedocs.io/en/latest/modules/chains/examples/llm_bash.html))
|
| 9 |
+
"""
|
| 10 |
+
# -
|
| 11 |
|
| 12 |
+
# $
|
|
|
|
| 13 |
|
| 14 |
+
from minichain import show, prompt, OpenAI, Bash
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
+
@prompt(OpenAI(), template_file = "bash.pmpt.tpl")
|
| 18 |
+
def cli_prompt(model, query):
|
| 19 |
+
x = model(dict(question=query))
|
| 20 |
+
return "\n".join(x.strip().split("\n")[1:-1])
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@prompt(Bash())
|
| 23 |
+
def bash_run(model, x):
|
| 24 |
+
return model(x)
|
| 25 |
|
| 26 |
+
def bash(query):
|
| 27 |
+
return bash_run(cli_prompt(query))
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
+
# $
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
gradio = show(bash,
|
| 33 |
+
subprompts=[cli_prompt, bash_run],
|
| 34 |
+
examples=['Go up one directory, and then into the minichain directory,'
|
| 35 |
+
'and list the files in the directory',
|
| 36 |
+
"Please write a bash script that prints 'Hello World' to the console."],
|
| 37 |
+
out_type="markdown",
|
| 38 |
+
description=desc,
|
| 39 |
+
code=open("bash.py", "r").read().split("$")[1].strip().strip("#").strip(),
|
| 40 |
+
)
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
gradio.launch()
|
| 43 |
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
gradio
|
| 2 |
git+https://github.com/srush/minichain@gradio
|
| 3 |
manifest-ml
|
|
|
|
|
|
| 1 |
+
gradio==3.21.0
|
| 2 |
git+https://github.com/srush/minichain@gradio
|
| 3 |
manifest-ml
|
| 4 |
+
faiss-cpu
|