hitchhikebot / app.py
Ankitnau25's picture
Update app.py
99a28eb
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
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)
def make_inference(query,OPENAI_API_KEY):
os.environ["OPENAI_API_KEY"]=OPENAI_API_KEY
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")
docs = docsearch.get_relevant_documents(query)
return(chain.run(input_documents=docs, question=query))
if __name__ == "__main__":
# make a gradio interface
import gradio as gr
gr.Interface(
make_inference,
[
gr.inputs.Textbox(lines=2, label="Query"),
gr.inputs.Textbox(lines=1, label="OPENAI_API_KEY"),
],
gr.outputs.Textbox(label="Response"),
debug=True,
title="hitchhikebot",
description="hitchhikebot is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
).launch()