huohguohbo's picture
Update app.py
540f76d
raw
history blame
1.12 kB
import openai
import re
import gradio as gr
# Define a function to solve math problems
def solve_math_problem(prompt, api_key):
# Set up the OpenAI API credentials
openai.api_key = api_key
# Send the prompt to OpenAI's GPT-3 engine
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.5,
)
# Extract the answer from the response
answer = response.choices[0].text.strip()
# Remove any non-numeric characters from the answer
answer = re.sub("[^0-9\.]", "", answer)
return float(answer)
# Define the Gradio interface
iface = gr.Interface(
fn=solve_math_problem,
inputs=[
gr.inputs.Textbox(label="Enter a math problem"),
gr.inputs.Textbox(label="Enter your OpenAI API key", type="password")
],
outputs=gr.outputs.Textbox(label="Answer"),
title="OpenAI Calculator",
description="Enter a math problem and let OpenAI solve it for you!",
live=True,
button_text="Calculate"
)
# Launch the Gradio interface
iface.launch()