Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,47 +0,0 @@
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|