Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 2 |
+
from langchain.vectorstores import Chroma
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 5 |
+
from langchain.llms import OpenAI
|
| 6 |
+
import os
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
with open("guide1.txt") as f:
|
| 10 |
+
hitchhikersguide = f.read()
|
| 11 |
+
|
| 12 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
|
| 13 |
+
texts = text_splitter.split_text(hitchhikersguide)
|
| 14 |
+
|
| 15 |
+
embeddings = OpenAIEmbeddings()
|
| 16 |
+
|
| 17 |
+
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
|
| 18 |
+
|
| 19 |
+
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
|
| 20 |
+
|
| 21 |
+
def make_inference(query):
|
| 22 |
+
docs = docsearch.get_relevant_documents(query)
|
| 23 |
+
return(chain.run(input_documents=docs, question=query))
|
| 24 |
+
|
| 25 |
+
if __name__ == "__main__":
|
| 26 |
+
# Title of the web application
|
| 27 |
+
st.title('🗣️TalkToMyDoc📄')
|
| 28 |
+
|
| 29 |
+
# Text input widget
|
| 30 |
+
user_input = st.text_input('Enter a question about Hitchhiker\'s Galaxy Guide book:', '', help='🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker\'s Guide to the Galaxy..')
|
| 31 |
+
|
| 32 |
+
# Displaying output directly below the input field
|
| 33 |
+
if user_input:
|
| 34 |
+
st.write('Answer:', make_inference(user_input))
|