Spaces:
Build error
Build error
Initial commit
Browse files- app.py +76 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain import OpenAI
|
| 4 |
+
from langchain.chains import PALChain
|
| 5 |
+
import datetime
|
| 6 |
+
|
| 7 |
+
gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
|
| 8 |
+
prompt = ""
|
| 9 |
+
|
| 10 |
+
def openai_create(prompt):
|
| 11 |
+
print("prompt: " + prompt)
|
| 12 |
+
|
| 13 |
+
# We use temperature of 0.0 because it gives the most predictable, factual answer (i.e. avoids hallucination).
|
| 14 |
+
response = openai.Completion.create(
|
| 15 |
+
model="text-davinci-003",
|
| 16 |
+
prompt=prompt,
|
| 17 |
+
temperature=0.0,
|
| 18 |
+
max_tokens=300,
|
| 19 |
+
top_p=1,
|
| 20 |
+
frequency_penalty=0,
|
| 21 |
+
presence_penalty=0
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
return response.choices[0].text
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def calc_gpt_only(math_problem):
|
| 28 |
+
answer = openai_create(gpt_only_prompt + math_problem + "\n")
|
| 29 |
+
|
| 30 |
+
print("math problem: " + math_problem)
|
| 31 |
+
print("calc_gpt_only answer: " + answer)
|
| 32 |
+
|
| 33 |
+
html = "<pre>" + answer + "</pre>"
|
| 34 |
+
return html
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def calc_gpt_pal(math_problem):
|
| 38 |
+
llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)
|
| 39 |
+
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
|
| 40 |
+
answer = pal_chain.run(math_problem)
|
| 41 |
+
|
| 42 |
+
print("math problem: " + math_problem)
|
| 43 |
+
print("calc_gpt_pal answer: " + answer)
|
| 44 |
+
|
| 45 |
+
html = "<pre>" + answer + "</pre>"
|
| 46 |
+
return html
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
| 50 |
+
|
| 51 |
+
with block:
|
| 52 |
+
title = gr.Markdown("""<h3><center>Comparing GPT math results</center></h3>""")
|
| 53 |
+
answer_html = gr.Markdown()
|
| 54 |
+
|
| 55 |
+
request = gr.Textbox(label="Math question:",
|
| 56 |
+
placeholder="Ex: What is the sum of the first 10 prime numbers?")
|
| 57 |
+
with gr.Row():
|
| 58 |
+
gpt_only = gr.Button(value="GPT Only", variant="secondary").style(full_width=False)
|
| 59 |
+
gpt_pal = gr.Button(value="GPT w/PAL", variant="secondary").style(full_width=False)
|
| 60 |
+
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=["42 times 81",
|
| 63 |
+
"Olivia has $23. She bought five bagels for $3 each. How much money does she have left?",
|
| 64 |
+
"What is the sum of the first 21 Fibonacci numbers?",
|
| 65 |
+
"Jane quit her job on Mar 20, 2020. 176 days have passed since then. What is the date tomorrow in MM/DD/YYYY?"],
|
| 66 |
+
inputs=request
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
gr.HTML("See <a href='https://reasonwithpal.com/'>PAL: Program-aided Language Models</a>")
|
| 70 |
+
|
| 71 |
+
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
| 72 |
+
|
| 73 |
+
gpt_only.click(calc_gpt_only, inputs=[request], outputs=[answer_html])
|
| 74 |
+
gpt_pal.click(calc_gpt_pal, inputs=[request], outputs=[answer_html])
|
| 75 |
+
|
| 76 |
+
block.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|
| 3 |
+
langchain
|
| 4 |
+
datetime
|