srush commited on
Commit
f9148b5
·
1 Parent(s): 8634377

Upload with huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +24 -33
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,48 +1,39 @@
1
- # Adapted from Prompt-aided Language Models [PAL](https://arxiv.org/pdf/2211.10435.pdf).
 
2
 
3
- import minichain
4
 
5
- # PAL Prompt
 
6
 
7
- class PalPrompt(minichain.TemplatePrompt):
8
- template_file = "pal.pmpt.tpl"
9
 
10
- # Prompt to run and print python code.
11
 
12
- class PyPrompt(minichain.Prompt):
13
- def prompt(self, inp):
14
- return inp + "\nprint(solution())"
15
 
16
- def parse(self, response, inp):
17
- return int(response)
 
18
 
19
- # Chain the prompts.
 
20
 
21
- with minichain.start_chain("pal") as backend:
22
- prompt = PalPrompt(backend.OpenAI()).chain(PyPrompt(backend.Python()))
23
- # result = prompt({"question": question})
24
 
25
  question = "Melanie is a door-to-door saleswoman. She sold a third of her " \
26
  "vacuum cleaners at the green house, 2 more to the red house, and half of " \
27
  "what was left at the orange house. If Melanie has 5 vacuum cleaners left, " \
28
  "how many did she start with?"
29
-
30
- prompt.to_gradio(fields =["question"],
31
- examples=[question]).launch()
32
 
33
- # View prompt examples.
 
 
 
 
 
34
 
35
- # # + tags=["hide_inp"]
36
- # PalPrompt().show(
37
- # {"question": "Joe has 10 cars and Bobby has 12. How many do they have together?"},
38
- # "def solution():\n\treturn 10 + 12",
39
- # )
40
- # # -
41
-
42
- # # + tags=["hide_inp"]
43
- # PyPrompt().show("def solution():\n\treturn 10 + 12", "22")
44
- # # -
45
-
46
- # # View the log.
47
-
48
- # minichain.show_log("pal.log")
 
1
+ desc = """
2
+ ### Prompt-aided Language Models
3
 
4
+ Chain for answering complex problems by code generation and execution. [[Code](https://github.com/srush/MiniChain/blob/main/examples/pal.py)]
5
 
6
+ (Adapted from Prompt-aided Language Models [PAL](https://arxiv.org/pdf/2211.10435.pdf)).
7
+ """
8
 
9
+ # $
 
10
 
11
+ from minichain import prompt, show, OpenAI, Python
12
 
13
+ @prompt(OpenAI(), template_file="pal.pmpt.tpl")
14
+ def pal_prompt(model, question):
15
+ return model(dict(question=question))
16
 
17
+ @prompt(Python())
18
+ def python(model, inp):
19
+ return int(model(inp + "\nprint(solution())"))
20
 
21
+ def pal(question):
22
+ return python(pal_prompt(question))
23
 
24
+ # $
 
 
25
 
26
  question = "Melanie is a door-to-door saleswoman. She sold a third of her " \
27
  "vacuum cleaners at the green house, 2 more to the red house, and half of " \
28
  "what was left at the orange house. If Melanie has 5 vacuum cleaners left, " \
29
  "how many did she start with?"
 
 
 
30
 
31
+ gradio = show(pal,
32
+ examples=[question],
33
+ subprompts=[pal_prompt, python],
34
+ description=desc,
35
+ code=open("pal.py", "r").read().split("$")[1].strip().strip("#").strip(),
36
+ )
37
 
38
+ if __name__ == "__main__":
39
+ gradio.launch()
 
 
 
 
 
 
 
 
 
 
 
 
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