MJ106 commited on
Commit
fd9e507
β€’
1 Parent(s): c4f204b

Upload 4 files

Browse files
Files changed (4) hide show
  1. app-v1.py +46 -0
  2. config.toml +6 -0
  3. requirements (2).txt +5 -0
  4. streamlit_app.py +47 -0
app-v1.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.llms import OpenAI
3
+ from langchain.text_splitter import CharacterTextSplitter
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores import Chroma
6
+ from langchain.chains import RetrievalQA
7
+
8
+ def generate_response(uploaded_file, openai_api_key, query_text):
9
+ # Load document if file is uploaded
10
+ if uploaded_file is not None:
11
+ documents = [uploaded_file.read().decode()]
12
+ # Split documents into chunks
13
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
14
+ texts = text_splitter.create_documents(documents)
15
+ # Select embeddings
16
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
17
+ # Create a vectorstore from documents
18
+ db = Chroma.from_documents(texts, embeddings)
19
+ # Create retriever interface
20
+ retriever = db.as_retriever()
21
+ # Create QA chain
22
+ qa = RetrievalQA.from_chain_type(llm=OpenAI(openai_api_key=openai_api_key), chain_type='stuff', retriever=retriever)
23
+ return qa.run(query_text)
24
+
25
+ # Page title
26
+ st.set_page_config(page_title='πŸ¦œπŸ”— Ask the Doc App')
27
+ st.title('πŸ¦œπŸ”— Ask the Doc App')
28
+
29
+ # File upload
30
+ uploaded_file = st.file_uploader('Upload an article', type='txt')
31
+ # Query text
32
+ query_text = st.text_input('Enter your question:', placeholder = 'Please provide a short summary.', disabled=not uploaded_file)
33
+
34
+ # Form input and query
35
+ result = []
36
+ with st.form('myform', clear_on_submit=True):
37
+ openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (uploaded_file and query_text))
38
+ submitted = st.form_submit_button('Submit', disabled=not(uploaded_file and query_text))
39
+ if submitted and openai_api_key.startswith('sk-'):
40
+ with st.spinner('Calculating...'):
41
+ response = generate_response(uploaded_file, openai_api_key, query_text)
42
+ result.append(response)
43
+ del openai_api_key
44
+
45
+ if len(result):
46
+ st.info(response)
config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#F63366"
3
+ backgroundColor="#FFFFFF"
4
+ secondaryBackgroundColor="#F0F2F6"
5
+ textColor="#262730"
6
+ font="sans serif"
requirements (2).txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ langchain
3
+ openai
4
+ chromadb
5
+ tiktoken
streamlit_app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.llms import OpenAI
3
+ from langchain.text_splitter import CharacterTextSplitter
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores import Chroma
6
+ from langchain.chains import RetrievalQA
7
+
8
+ def generate_response(uploaded_file, openai_api_key, query_text):
9
+ # Load document if file is uploaded
10
+ if uploaded_file is not None:
11
+ documents = [uploaded_file.read().decode()]
12
+ # Split documents into chunks
13
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
14
+ texts = text_splitter.create_documents(documents)
15
+ # Select embeddings
16
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
17
+ # Create a vectorstore from documents
18
+ db = Chroma.from_documents(texts, embeddings)
19
+ # Create retriever interface
20
+ retriever = db.as_retriever()
21
+ # Create QA chain
22
+ qa = RetrievalQA.from_chain_type(llm=OpenAI(openai_api_key=openai_api_key), chain_type='stuff', retriever=retriever)
23
+ return qa.run(query_text)
24
+
25
+
26
+ # Page title
27
+ st.set_page_config(page_title='πŸ¦œπŸ”— Ask the Doc App')
28
+ st.title('πŸ¦œπŸ”— Ask the Doc App')
29
+
30
+ # File upload
31
+ uploaded_file = st.file_uploader('Upload an article', type='txt')
32
+ # Query text
33
+ query_text = st.text_input('Enter your question:', placeholder = 'Please provide a short summary.', disabled=not uploaded_file)
34
+
35
+ # Form input and query
36
+ result = []
37
+ with st.form('myform', clear_on_submit=True):
38
+ openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (uploaded_file and query_text))
39
+ submitted = st.form_submit_button('Submit', disabled=not(uploaded_file and query_text))
40
+ if submitted and openai_api_key.startswith('sk-'):
41
+ with st.spinner('Calculating...'):
42
+ response = generate_response(uploaded_file, openai_api_key, query_text)
43
+ result.append(response)
44
+ del openai_api_key
45
+
46
+ if len(result):
47
+ st.info(response)