NeuralJunkie commited on
Commit
2bdde4b
·
1 Parent(s): 91df1b6

Update app.py

Browse files

complete rework

Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -1,32 +1,47 @@
1
- # Define the conversation history variable
2
- conversation_history = ""
3
 
4
- # Define the OpenAI chat function
5
- def chat(text):
6
- # Access the global conversation history variable
7
- global conversation_history
8
-
9
- # Update the conversation history with the latest input
10
- conversation_history += f"User: {text}\nAI: "
11
-
12
- # Set up the OpenAI prompt with the full conversation history
13
- prompt = conversation_history
14
-
15
- # Call the OpenAI API to generate a response
 
 
 
 
 
 
16
  response = openai.Completion.create(
17
- engine="gpt-3.5-turbo-0301",
18
  prompt=prompt,
19
- max_tokens=1024,
20
- n=1,
21
- stop=["\n", ".", "?", "!"],
22
- temperature=0.7,
 
 
 
23
  )
24
-
25
- # Extract the response text from the OpenAI API result
26
- message = response.choices[0].text.strip()
27
-
28
- # Update the conversation history with the latest response
29
- conversation_history += f"{message}\n"
30
-
31
- # Return the response to the user
32
- return message
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
 
4
+ # Set up OpenAI API credentials
5
+ openai.api_key = OPENAPI_KEY
6
+
7
+ # Set up the GPT-3 model
8
+ model_engine = "gpt-3.5-turbo-0301"
9
+ prompt = (
10
+ "I want you to act as a math teacher. I will provide some mathematical equations or concepts, "
11
+ "and it will be your job to explain them in easy-to-understand terms. This could include providing "
12
+ "step-by-step instructions for solving a problem, demonstrating various techniques with visuals or "
13
+ "suggesting online resources for further study. My first request is \"I need help understanding how "
14
+ "probability works.\""
15
+ )
16
+ temperature = 0.7
17
+ max_tokens = 1000
18
+ context_length = 2
19
+
20
+ # Define the function that generates the response
21
+ def generate_response(prompt):
22
  response = openai.Completion.create(
23
+ engine=model_engine,
24
  prompt=prompt,
25
+ temperature=temperature,
26
+ max_tokens=max_tokens,
27
+ n = 1,
28
+ stop=None,
29
+ frequency_penalty=0,
30
+ presence_penalty=0,
31
+ context=context_length * [prompt],
32
  )
33
+
34
+ return response.choices[0].text.strip()
35
+
36
+ # Set up the Gradio interface
37
+ io = gr.Interface(
38
+ generate_response,
39
+ "textbox",
40
+ "textbox",
41
+ title="Chat with a Math Teacher",
42
+ description="Type a math question or concept that you need help understanding, and the math teacher will explain it to you in easy-to-understand terms!",
43
+ article="This app uses the GPT-3 language model to generate responses. The math teacher is programmed to provide step-by-step instructions for solving a problem, demonstrate various techniques with visuals, or suggest online resources for further study. Please keep in mind that this is an AI language model and may not always provide accurate or complete information.",
44
+ )
45
+
46
+ # Launch the interface
47
+ io.launch()