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