File size: 1,753 Bytes
28be0a0
3bc9037
28be0a0
 
 
 
5257812
28be0a0
 
 
 
 
 
3bc9037
28be0a0
 
ae4c441
3bc9037
 
 
 
 
 
 
 
28be0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc85b01
 
28be0a0
 
 
cc85b01
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()