Spaces:
Sleeping
Sleeping
Commit ·
634253b
1
Parent(s): fc7720b
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,41 @@
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# Set up
|
| 5 |
openai.api_key = "API_KEY"
|
| 6 |
|
| 7 |
-
# Set up the GPT-3 model
|
| 8 |
-
model_engine = "gpt-3.5-turbo"
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Define the function that
|
| 11 |
-
def
|
| 12 |
-
#
|
| 13 |
-
temperature = 0.7
|
| 14 |
-
max_tokens = 1000
|
| 15 |
-
context_length = 2
|
| 16 |
-
|
| 17 |
-
# Use the OpenAI API to generate a response
|
| 18 |
response = openai.Completion.create(
|
| 19 |
engine=model_engine,
|
| 20 |
-
prompt=
|
| 21 |
-
temperature=temperature,
|
| 22 |
max_tokens=max_tokens,
|
| 23 |
-
|
| 24 |
-
stop=None,
|
| 25 |
-
frequency_penalty=0,
|
| 26 |
-
presence_penalty=0,
|
| 27 |
-
context=context_length * [prompt],
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
math_teacher = gr.Interface(
|
| 34 |
-
fn=generate_response,
|
| 35 |
inputs="text",
|
| 36 |
outputs="text",
|
| 37 |
-
title="Math Teacher",
|
| 38 |
-
description="
|
| 39 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Set up OpenAI API credentials
|
| 5 |
openai.api_key = "API_KEY"
|
| 6 |
|
| 7 |
+
# Set up the GPT-3.5-Turbo model
|
| 8 |
+
model_engine = "gpt-3.5-turbo-0301"
|
| 9 |
+
max_tokens = 1000
|
| 10 |
+
temperature = 0.7
|
| 11 |
|
| 12 |
+
# Define the function that will be called when the user interacts with the Gradio interface
|
| 13 |
+
def chatbot(text):
|
| 14 |
+
# Generate a response from the OpenAI model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
response = openai.Completion.create(
|
| 16 |
engine=model_engine,
|
| 17 |
+
prompt=text,
|
|
|
|
| 18 |
max_tokens=max_tokens,
|
| 19 |
+
temperature=temperature
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
+
# Extract the generated response from the API response
|
| 22 |
+
generated_text = response.choices[0].text
|
| 23 |
+
# Return the response, stripping any leading or trailing whitespace
|
| 24 |
+
return generated_text.strip()
|
| 25 |
|
| 26 |
+
# Define the Gradio interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=chatbot,
|
|
|
|
|
|
|
| 29 |
inputs="text",
|
| 30 |
outputs="text",
|
| 31 |
+
title="Math Teacher Chatbot",
|
| 32 |
+
description="Ask me any math questions you have and I'll do my best to help!",
|
| 33 |
+
examples=[
|
| 34 |
+
["What's the Pythagorean theorem?"],
|
| 35 |
+
["What's the area of a circle with radius 5?"],
|
| 36 |
+
["What's the derivative of x^2?"],
|
| 37 |
+
]
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Launch the interface
|
| 41 |
+
interface.launch()
|