File size: 1,361 Bytes
bb26e07
 
 
 
 
 
fd6114e
bb26e07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73f064
 
 
 
 
5308ba8
b73f064
 
f83a076
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import os
import streamlit as st

with open("guide1.txt") as f:
    hitchhikersguide = f.read()

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
texts = text_splitter.split_text(hitchhikersguide)

embeddings = OpenAIEmbeddings()

docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()

chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")

def make_inference(query):
    docs = docsearch.get_relevant_documents(query)
    return(chain.run(input_documents=docs, question=query))

if __name__ == "__main__":
    # Title of the web application
    st.title('🗣️TalkToMyDoc📄')
    
    # Text input widget
    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..')
    
    # Displaying output directly below the input field
    if user_input:
        st.write('Answer:', make_inference(user_input))