ChatRobo_II / app.py
RamV's picture
Update app.py
d9cc1af
import os
import openai
import gradio as gr
# Set up OpenAI API credentials
api_key = os.environ.get("OPENAI_API_KEY")
openai.api_key = api_key
# Define a function to generate a response to user input
def generate_response(user_input):
if "who created you" in user_input.lower():
chat_response = "I was created by Ram.V"
elif "who is superstar" in user_input.lower():
chat_response = "The one and only * Superstar Rajinikanth * Thalaiva!"
elif "what's on 10th aug" in user_input.lower():
chat_response = "Jailer Movie releasing! Alappara Kelappurom Thalaivaru Nerandharam!"
elif "what is the weather like" in user_input.lower():
chat_response = "I'm sorry, but I don't have the ability to check the weather. Is there something else I can help you with?"
elif "how are you" in user_input.lower():
chat_response = "I'm a chatbot created by Ram.V. I don't have feelings, but thanks for asking. Hope you're well! :-)"
elif "what's your name" in user_input.lower():
chat_response = "My name is ChatRobo :-)"
else:
prompt = f"You said: {user_input}"
# Generate a response using the OpenAI API
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=["Human:", "AI:"]
)
# Extract the response from the API output
chat_response = response.choices[0].text.strip()
return chat_response
# Define the function to handle the chat history
def openai_chat_history(input, history):
history = history or []
if input.strip() != "":
s = list(sum(history, ()))
s.append(input)
inp = ' '.join(s)
output = generate_response(inp)
history.append((input, output))
return history[-1][1]
else:
return ""
# Define the conversation prompt
conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
# Set up the Gradio interface
block = gr.Interface(
fn=openai_chat_history,
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
)
# Launch the Gradio interface
block.launch()