jonny commited on
Commit
9921a5e
Β·
1 Parent(s): eb45155

launch app

Browse files
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Chat Ai Safety
3
  emoji: πŸ“š
4
- colorFrom: gray
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.28.3
 
1
  ---
2
  title: Chat Ai Safety
3
  emoji: πŸ“š
4
+ colorFrom: grey
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.28.3
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # gradio imports
2
+ import gradio as gr
3
+ import os
4
+ import time
5
+
6
+ # Imports
7
+ import os
8
+
9
+ import openai
10
+ from langchain.chains import ConversationalRetrievalChain
11
+
12
+ from langchain.embeddings.openai import OpenAIEmbeddings
13
+ from langchain.chat_models import ChatOpenAI
14
+ from langchain.text_splitter import CharacterTextSplitter
15
+ from langchain.vectorstores import Chroma
16
+ from langchain.document_loaders import TextLoader
17
+
18
+ from langchain.memory import ConversationBufferMemory
19
+ from langchain.chat_models import ChatOpenAI
20
+
21
+ css="""
22
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
23
+ """
24
+
25
+ title = """
26
+ <div style="text-align: center;max-width: 700px;">
27
+ <h1>Chat with Jonny's views on AI β€’ AI Safety</h1>
28
+ <p style="text-align: left;">Chat is built from:<br />
29
+ This is a Dialogue (https://www.jonnyjohnson.com/this-is-a-dialogue)<br />
30
+ Game-Making articles (https://dialogues-ai.github.io/papers/docs/ai_regulation/gamemaking)<br />
31
+ As well as 25 blog posts contributed to BMC <br />
32
+ </div>
33
+ """
34
+
35
+
36
+ prompt_hints = """
37
+ <div style="text-align: center;max-width: 700px;">
38
+ <p style="text-align: left;">Some things you can ask:<br />
39
+ Should I be worried about AIs?<br />
40
+ How do we improve the games between AIs and humans?<br />
41
+ What are the risks associated with AI? <br />
42
+ Do you agree that everything is language? <br />
43
+ </div>
44
+ """
45
+
46
+ # from index import PERSIST_DIRECTORY, CalendarIndex
47
+ PERSIST_DIRECTORY = "chromadb"
48
+ # Create embeddings
49
+
50
+ # # create memory object
51
+ from langchain.memory import ConversationBufferMemory
52
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
53
+
54
+ def loading_pdf():
55
+ return "Loading..."
56
+
57
+ def loading_database(open_ai_key):
58
+ if open_ai_key is not None:
59
+ if os.path.exists(PERSIST_DIRECTORY):
60
+ os.environ['OPENAI_API_KEY'] = open_ai_key
61
+ print("Loading from persisted dir")
62
+ embeddings = OpenAIEmbeddings()
63
+ docs_retriever = Chroma(persist_directory=PERSIST_DIRECTORY, embedding_function=embeddings)
64
+
65
+ global qa_chain
66
+ qa_chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0),
67
+
68
+ retriever=docs_retriever.as_retriever(),
69
+ memory=memory,
70
+ return_source_documents=False
71
+ )
72
+ return "Ready"
73
+ else:
74
+ return "You forgot OpenAI API key"
75
+
76
+ def add_text(history, text):
77
+ history = history + [(text, None)]
78
+ return history, ""
79
+
80
+
81
+ def bot(history):
82
+ response = infer(history[-1][0], history)
83
+ history[-1][1] = ""
84
+ for character in response:
85
+ history[-1][1] += character
86
+ time.sleep(0.05)
87
+ yield history
88
+
89
+
90
+ def infer(question, history):
91
+ res = []
92
+ for human, ai in history[:-1]:
93
+ pair = (human, ai)
94
+ res.append(pair)
95
+
96
+ chat_history = res
97
+ query = question
98
+ result = qa_chain({"question": query, "chat_history": chat_history})
99
+ return result["answer"]
100
+
101
+ def update_message(question_component, chat_prompts):
102
+ question_component.value = chat_prompts.get_name()
103
+ return None
104
+
105
+ with gr.Blocks(css=css) as demo:
106
+ with gr.Column(elem_id="col-container"):
107
+ gr.HTML(title)
108
+ with gr.Column():
109
+ with gr.Row():
110
+ openai_key = gr.Textbox(label="OpenAI API key", type="password")
111
+ submit_api_key = gr.Button("Submit")
112
+ with gr.Row():
113
+ langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
114
+
115
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
116
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
117
+ submit_btn = gr.Button("Send Message")
118
+ gr.HTML(prompt_hints)
119
+
120
+ submit_api_key.click(loading_database, inputs=[openai_key], outputs=[langchain_status], queue=False)
121
+ # demo.load(loading_database, None, langchain_status)
122
+ question.submit(add_text, [chatbot, question], [chatbot, question]).then(
123
+ bot, chatbot, chatbot
124
+ )
125
+ submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
126
+ bot, chatbot, chatbot)
127
+
128
+ demo.queue(concurrency_count=2, max_size=20).launch()
chromadb/chroma-collections.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:958d13ad11160653c766cf8d6e76ad05ea4adc6fec22dc029165e9d1f2960e3e
3
+ size 557
chromadb/chroma-embeddings.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7eac70b81e2bfcc022ddd5942ae94edb8692f54a79a1fcde480a7f756d00e782
3
+ size 10769969
chromadb/index/id_to_uuid_56718c26-0f35-40cf-b992-9f14e3ab54f1.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:df90bd2803837a0127a510ce0b5d881b98b36bfc67c1f0107504e8efd73eada7
3
+ size 27751
chromadb/index/index_56718c26-0f35-40cf-b992-9f14e3ab54f1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d392de98b1d91f07d4bc0123d29dfd21c3c8c859823df110c95428bc0ab93199
3
+ size 5417600
chromadb/index/index_metadata_56718c26-0f35-40cf-b992-9f14e3ab54f1.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c400f5874bb840e55638e05ba049baee883731dcf4652e4fb6e7ba5c1f546849
3
+ size 74
chromadb/index/uuid_to_id_56718c26-0f35-40cf-b992-9f14e3ab54f1.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:42af20a7a3947ecabab29baae42936be1f77bda363699bc69d495f21685ddf04
3
+ size 32479
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai
2
+ tiktoken
3
+ chromadb
4
+ langchain