NeuralJunkie commited on
Commit
4ec1a37
·
1 Parent(s): 5e8d0e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -53
app.py CHANGED
@@ -1,54 +1,33 @@
1
- import os
2
  import openai
3
- import gradio as gr
4
-
5
- # Set up OpenAI API credentials
6
- api_key = os.environ.get("API_KEY", "API_KEY")
7
- openai.api_key = api_key
8
-
9
- # Set up the GPT-3.5-Turbo model
10
- model_engine = "gpt-3.5-turbo" # engine parameter used to specify the GPT-3.5-Turbo model
11
- max_tokens = 1000
12
- temperature = 0.7
13
-
14
- # Define the function that will be called when the user interacts with the Gradio interface
15
- def chatbot(text):
16
- # Generate a response from the OpenAI model
17
- response = openai.Completion.create(
18
- engine=model_engine,
19
- prompt=text,
20
- max_tokens=max_tokens,
21
- temperature=temperature,
22
- stop=None,
23
- n=None,
24
- logprobs=None,
25
- echo=False,
26
- presence_penalty=0.5,
27
- frequency_penalty=None,
28
- best_of=None,
29
- response_format="text",
30
- v="2022-04-28", # specify the OpenAI API version explicitly
31
- model=None, # remove the 'model' parameter
32
- )
33
- # Extract the generated response from the API response
34
- generated_text = response.choices[0].text
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."))