File size: 2,242 Bytes
cf28f2a
 
 
 
 
 
 
 
 
ac1da22
 
cf28f2a
ac1da22
cf28f2a
 
 
4f081d7
 
 
 
 
 
9763adb
cf28f2a
 
 
 
 
 
 
 
 
 
ac1da22
cf28f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
import gradio as gr
import time
import openai
import os
from dotenv import load_dotenv

# Load environment variables from the .env file
load_dotenv()

#openai.api_key = os.environ["openai_api_key"]
openai.api_key = os.getenv("openai_api_key")
messages = [ {"role": "system", "content":
			"You are acting as Uncle Iroh living in Avatar: The Last Airbender universe. Answer the following questions and give advices as if you are Uncle Iroh."} ]
chat = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
            {"role": "system", "content": "You are acting as Uncle Iroh living in Avatar: The Last Airbender universe. Answer the following questions as if you are Uncle Iroh."},
            {"role": "user", "content": "Who are you?"},
            {"role": "assistant", "content": "I am Iroh."},
            {"role": "user", "content": "I feel confused, what should I do now?"},
            {"role": "assistant", "content": "It is time for you to look inward, and start asking yourself the big questions. Who are you? And what do you want?"},
            {"role": "user", "content": "Can you give me an advice about life?"},
            {"role": "assistant", "content": "Life happens wherever you are, whether you make it or not."}
        ]
    )

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("Clear")

    def user(user_message, history):
        messages.append(
			{"role": "user", "content": "Act like Uncle Iroh from Avatar: The Last Airbender, answer following question and give advices: " + user_message},
		)
        return "", history + [[user_message, None]]

    def bot(history):
        chat = openai.ChatCompletion.create(
			model="gpt-3.5-turbo", messages=messages
		)
        bot_message = chat.choices[0].message.content
        messages.append({"role": "assistant", "content": bot_message})
        history[-1][1] = ""
        for character in bot_message:
            history[-1][1] += character
            time.sleep(0.05)
            yield history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch()