Spaces:
Sleeping
Sleeping
Commit
·
4ec1a37
1
Parent(s):
5e8d0e4
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,33 @@
|
|
| 1 |
-
import os
|
| 2 |
import openai
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Return the response, stripping any leading or trailing whitespace
|
| 36 |
-
return generated_text.strip()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Define the Gradio interface
|
| 40 |
-
interface = gr.Interface(
|
| 41 |
-
fn=chatbot,
|
| 42 |
-
inputs="text",
|
| 43 |
-
outputs="text",
|
| 44 |
-
title="Math Teacher Chatbot",
|
| 45 |
-
description="Ask me any math questions you have and I'll do my best to help!",
|
| 46 |
-
examples=[
|
| 47 |
-
["What's the Pythagorean theorem?"],
|
| 48 |
-
["What's the area of a circle with radius 5?"],
|
| 49 |
-
["What's the derivative of x^2?"],
|
| 50 |
-
]
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
# Launch the interface
|
| 54 |
-
interface.launch()
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import csv
|
| 3 |
+
|
| 4 |
+
openai.api_key = "YOUR_API_KEY"
|
| 5 |
+
|
| 6 |
+
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.'"}
|
| 7 |
+
|
| 8 |
+
def submit_message(prompt):
|
| 9 |
+
prompt_template = prompt_templates["Math Teacher"]
|
| 10 |
+
|
| 11 |
+
system_prompt = []
|
| 12 |
+
if prompt_template:
|
| 13 |
+
system_prompt = [{ "role": "system", "content": prompt_template }]
|
| 14 |
+
|
| 15 |
+
prompt_msg = { "role": "user", "content": prompt }
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
completion = openai.Completion.create(
|
| 19 |
+
engine="text-davinci-002",
|
| 20 |
+
prompt=system_prompt + [prompt_msg],
|
| 21 |
+
max_tokens=1024,
|
| 22 |
+
n=1,
|
| 23 |
+
stop=None,
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
response = completion.choices[0].text
|
| 28 |
+
except Exception as e:
|
| 29 |
+
response = f"Sorry, an error occurred: {e}"
|
| 30 |
+
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
print(submit_message("I need help understanding how probability works."))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|