Spaces:
Runtime error
Runtime error
| 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() |