File size: 2,994 Bytes
38bf5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2845ab6
38bf5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2845ab6
38bf5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f226a
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import datetime
import os
import gradio as gr
import langchain
import weaviate
from langchain.vectorstores import Weaviate

from chain import get_new_chain1

WEAVIATE_URL = "https://tro.weaviate.network/"



def get_weaviate_store():
    client = weaviate.Client(
        url=WEAVIATE_URL,
        additional_headers={"X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"]},
    )
    return Weaviate(client, "Paragraphs", "content")


def set_openai_api_key(api_key, agent):
    if api_key:
        os.environ["OPENAI_API_KEY"] = api_key
        vectorstore = get_weaviate_store()  # Calls Vector DB 
        
        qa_chain = get_new_chain1(vectorstore) # inputs Charmed Game DB into new Quest chain

        os.environ["OPENAI_API_KEY"] = ""
        return qa_chain


def chat(inp, history, agent): # inp: User Input
    history = history or []
    if agent is None:
        history.append((inp, "Please paste your OpenAI key to use"))
        return history, history
    print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
    print("inp: " + inp)
    history = history or []
    output = agent({"game_description": inp, "chat_history": history}) # agent takes in input and history if any
    game_environment = output["game_environment"] # answer is appended to chat history
    history.append((inp, game_environment))
    # print(history)
    return history, history


block = gr.Blocks(css=".gradio-container {background-color: darkpurple}")

with block:
    with gr.Row():
        gr.Markdown("<h3><center>Charmed AI</center></h3>")

        openai_api_key_textbox = gr.Textbox(
            placeholder="Paste your OpenAI API key (sk-...)",
            show_label=False,
            lines=1,
            type="password",
        )

    chatbot = gr.Chatbot() # gradio Chatbot

    with gr.Row():
        message = gr.Textbox(
            label="Describe your game",
            placeholder="A game about turtles and ninjas who are flying in space in order to explore new planets",
            lines=1,
        )
        submit = gr.Button(value="Dream", variant="secondary").style(full_width=False)

    gr.Examples(
        examples=[
            "rougue-like",
            "sci-fi",
            "underwater sharks",
            "haunted house and ghosts",
            "cute animals",
            "80s theme",
        ],
        inputs=message,
    )

    gr.HTML(
        """
    """
    )

    gr.HTML(
        "<center>Powered by <a href='https://github.com/charmed-ai/'>Charmed-ai</a></center>"
    )
    # Gradio Chatbots are stateful
    state = gr.State()
    agent_state = gr.State() # agent

    submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
    message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])

    openai_api_key_textbox.change(
        set_openai_api_key,
        inputs=[openai_api_key_textbox, agent_state],
        outputs=[agent_state],
    )

block.launch(debug=True)#, share=True)