amqdn commited on
Commit
4b5c989
1 Parent(s): 2cf4056

add janky api key flow

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -5,19 +5,30 @@ from langchain.chains.question_answering import load_qa_chain
5
  from langchain.llms import OpenAI
6
  import os
7
 
 
 
8
  with open("guide1.txt") as f:
9
  hitchhikersguide = f.read()
10
 
11
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
  texts = text_splitter.split_text(hitchhikersguide)
13
 
14
- embeddings = OpenAIEmbeddings()
15
-
16
- docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
 
 
 
17
 
18
- chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
 
 
 
 
19
 
20
  def make_inference(query):
 
 
21
  docs = docsearch.get_relevant_documents(query)
22
  return(chain.run(input_documents=docs, question=query))
23
 
@@ -33,4 +44,4 @@ if __name__ == "__main__":
33
  gr.outputs.Textbox(label="Response"),
34
  title="Query Hitchhiker's Guide",
35
  description="See title. What would Douglas Adams say if he saw you query The Hitchhiker's Guide to the Galaxy with AI?",
36
- ).launch()
 
5
  from langchain.llms import OpenAI
6
  import os
7
 
8
+ setup_complete = False
9
+
10
  with open("guide1.txt") as f:
11
  hitchhikersguide = f.read()
12
 
13
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
14
  texts = text_splitter.split_text(hitchhikersguide)
15
 
16
+ def get_api_key(input_1, input_2):
17
+ if len(input_1) > 0:
18
+ os.environ['OPENAI_API_KEY'] = input_1
19
+ elif len(input_2) > 0:
20
+ os.environ['OPENAI_API_KEY'] = input_2
21
+ return True
22
 
23
+ def setup_chain():
24
+ global embeddings, docsearch, chain
25
+ embeddings = OpenAIEmbeddings()
26
+ docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
27
+ chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
28
 
29
  def make_inference(query):
30
+ if not setup_complete:
31
+ setup_chain()
32
  docs = docsearch.get_relevant_documents(query)
33
  return(chain.run(input_documents=docs, question=query))
34
 
 
44
  gr.outputs.Textbox(label="Response"),
45
  title="Query Hitchhiker's Guide",
46
  description="See title. What would Douglas Adams say if he saw you query The Hitchhiker's Guide to the Galaxy with AI?",
47
+ ).launch(auth=get_api_key)