Spaces:
Runtime error
Runtime error
Commit
·
66586fe
1
Parent(s):
0ceae6b
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.document_loaders import TextLoader, DirectoryLoader
|
| 5 |
+
from langchain.embeddings import CohereEmbeddings
|
| 6 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
+
from langchain.vectorstores import Chroma
|
| 8 |
+
from langchain.llms import OpenAI
|
| 9 |
+
from langchain.llms import Cohere
|
| 10 |
+
from langchain.chains import RetrievalQA
|
| 11 |
+
from langchain import PromptTemplate
|
| 12 |
+
|
| 13 |
+
import streamlit as st
|
| 14 |
+
|
| 15 |
+
def ingest(file_path,embeddings):
|
| 16 |
+
loader = TextLoader(file_path)
|
| 17 |
+
documents = loader.load()
|
| 18 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000) #Splitting the text and creating chunks
|
| 19 |
+
docs = text_splitter.split_documents(documents)
|
| 20 |
+
|
| 21 |
+
persist_directory = file_path[:-4]
|
| 22 |
+
print('persist dict: ')
|
| 23 |
+
print(persist_directory)
|
| 24 |
+
|
| 25 |
+
vectordb = Chroma.from_documents(documents=docs,
|
| 26 |
+
embedding=embeddings,
|
| 27 |
+
persist_directory=persist_directory)
|
| 28 |
+
# persiste the db to disk
|
| 29 |
+
vectordb.persist()
|
| 30 |
+
vectordb = None
|
| 31 |
+
|
| 32 |
+
with st.sidebar:
|
| 33 |
+
with st.form('Cohere/OpenAI'):
|
| 34 |
+
mod = st.radio('Choose OpenAI/Cohere', ('OpenAI', 'Cohere'))
|
| 35 |
+
api_key = st.text_input('Enter API key', type="password")
|
| 36 |
+
# model = st.radio('Choose Company', ('ArtisanAppetite foods', 'BMW','Titan Watches'))
|
| 37 |
+
submitted = st.form_submit_button("Submit")
|
| 38 |
+
|
| 39 |
+
if api_key:
|
| 40 |
+
if(mod=='OpenAI'):
|
| 41 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 42 |
+
llm = OpenAI(temperature=0.7, verbose=True)
|
| 43 |
+
embeddings = OpenAIEmbeddings()
|
| 44 |
+
elif(mod=='Cohere'):
|
| 45 |
+
os.environ["COHERE_API_KEY"] = api_key
|
| 46 |
+
llm = Cohere(temperature=0.7, verbose=True)
|
| 47 |
+
embeddings = CohereEmbeddings()
|
| 48 |
+
|
| 49 |
+
uploaded_file = st.file_uploader("Upload a file to ingest", type=["txt"])
|
| 50 |
+
|
| 51 |
+
if uploaded_file is not None:
|
| 52 |
+
file_contents = uploaded_file.read()
|
| 53 |
+
file_path = uploaded_file.name
|
| 54 |
+
# with open(save_path, "wb") as f:
|
| 55 |
+
# f.write(file_contents)
|
| 56 |
+
print(file_path)
|
| 57 |
+
ingest(file_path,embeddings)
|