Spaces:
Build error
Build error
Commit ·
a2ea3bb
1
Parent(s): 2b70c4a
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233import openaiimport osopenai.api_key = os.environ["API_KEY"]prompt_templates = {"Math Teacher": "I want you to act as a math teacher. I will provide some mathematical equations or concepts, and it will be your job to explain them in easy-to-understand terms. This could include providing step-by-step instructions for solving a problem, demonstrating various techniques with visuals or suggesting online resources for further study. My first request is 'I need help understanding how probability works.'"}def submit_message(prompt):prompt_template = prompt_templates["Math Teacher"]system_prompt = ""if prompt_template:system_prompt = prompt_template + "\n"prompt_msg = prompt + "\n"try:completion = openai.Completion.create(engine="text-davinci-002",prompt=system_prompt + prompt_msg,max_tokens=1024,n=1,stop=None,temperature=0.7,)response = completion.choices[0].textexcept Exception as e:response = f"Sorry, an error occurred: {e}"return responseprint(submit_message("I need help understanding how probability works."))1234567891011121314151617181920212223242526272829303132333435363738import openaiimport osimport gradio as gr# Fetching API key from environment variableAPI_KEY = os.environ['API_KEY']# Setting up OpenAI API credentialsopenai.api_key = API_KEYmodel_engine = 'text-davinci-002'prompt = 'Please solve the following math problem: '# Defining the function to solve math problemsdef math_tutor(problem):response = openai.Completion.create(engine='gpt-3.5-turbo-0301',prompt=prompt + problem,max_tokens=50,n=1,stop=None,temperature=0.7,)answer = response.choices[0].text.strip()return answer# Creating a Gradio interface for the math tutoriface = gr.Interface(fn=math_tutor,inputs=gr.inputs.Textbox(label="Enter a math problem"),outputs="text",title="Math Tutor",description="Enter a math problem and the Math Tutor will solve it for you!",theme="compact")# Running the Gradio interfaceif __name__ == '__main__':iface.launch()
app.py+36-31