Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,51 @@
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
# Set up OpenAI API credentials
|
| 5 |
-
openai.api_key = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
def generate_response(user_input):
|
| 9 |
# Check if user input contains the phrase "who created you"
|
| 10 |
-
if "who created you" in
|
| 11 |
# Generate a response that includes your name
|
| 12 |
-
chat_response =
|
| 13 |
else:
|
| 14 |
-
# Construct the prompt with the user input
|
| 15 |
-
prompt = f"You said: {user_input}"
|
| 16 |
-
|
| 17 |
-
# Generate a response using the OpenAI API
|
| 18 |
response = openai.Completion.create(
|
| 19 |
-
|
| 20 |
prompt=prompt,
|
| 21 |
-
temperature=0.
|
| 22 |
-
max_tokens=
|
| 23 |
-
|
| 24 |
-
stop=None,
|
| 25 |
frequency_penalty=0,
|
| 26 |
-
presence_penalty=0
|
|
|
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
chat_response = response.choices[0].text.strip()
|
| 31 |
-
|
| 32 |
return chat_response
|
| 33 |
|
| 34 |
-
# Define a Gradio interface for the chatbot
|
| 35 |
-
conversation_prompt = "Type here to chat with the bot!"
|
| 36 |
block = gr.Interface(
|
| 37 |
-
fn=
|
| 38 |
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
|
| 39 |
-
outputs=[gr.outputs.Textbox(label="
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
# Launch the interface
|
| 43 |
block.launch()
|
| 44 |
|
| 45 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Set up OpenAI API credentials
|
| 6 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Define the conversation prompt
|
| 9 |
+
conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
|
| 10 |
+
|
| 11 |
+
# Define the function to generate a response from the GPT model
|
| 12 |
+
def openai_chat_history(input, history):
|
| 13 |
+
history = history or []
|
| 14 |
+
if input.strip() != "":
|
| 15 |
+
s = list(sum(history, ()))
|
| 16 |
+
s.append(input)
|
| 17 |
+
inp = ' '.join(s)
|
| 18 |
+
output = openai_create(inp)
|
| 19 |
+
history.append((input, output))
|
| 20 |
+
return history[-1][1]
|
| 21 |
+
else:
|
| 22 |
+
return ""
|
| 23 |
|
| 24 |
+
def openai_create(prompt):
|
|
|
|
| 25 |
# Check if user input contains the phrase "who created you"
|
| 26 |
+
if "who created you" in prompt.lower():
|
| 27 |
# Generate a response that includes your name
|
| 28 |
+
chat_response = "I was created by [YOUR NAME HERE]!"
|
| 29 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
response = openai.Completion.create(
|
| 31 |
+
model="text-davinci-003",
|
| 32 |
prompt=prompt,
|
| 33 |
+
temperature=0.9,
|
| 34 |
+
max_tokens=150,
|
| 35 |
+
top_p=1,
|
|
|
|
| 36 |
frequency_penalty=0,
|
| 37 |
+
presence_penalty=0.6,
|
| 38 |
+
stop=["Human:", "AI:"]
|
| 39 |
)
|
| 40 |
+
chat_response = response.choices[0].text
|
| 41 |
+
|
|
|
|
|
|
|
| 42 |
return chat_response
|
| 43 |
|
|
|
|
|
|
|
| 44 |
block = gr.Interface(
|
| 45 |
+
fn=openai_chat_history,
|
| 46 |
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
|
| 47 |
+
outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
|
| 48 |
)
|
|
|
|
|
|
|
| 49 |
block.launch()
|
| 50 |
|
| 51 |
|