ChatRobot / app.py
RamV's picture
Update app.py
cc85b01
import os
from openai import Client
import gradio as gr
# Set up OpenAI API credentials
api_key = os.environ.get("OPENAI_API_KEY")
client = Client(api_key=api_key) # Update the initialization here
# 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!"
else:
# Generate a response using the OpenAI API
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": user_input}
]
)
chat_response = response.choices[0].message.content
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()