Ankitnau25 commited on
Commit
6b06f9a
1 Parent(s): 6f5c8da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ with open("guide1.txt") as f:
9
+ hitchhikersguide = f.read()
10
+
11
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
+ texts = text_splitter.split_text(hitchhikersguide)
13
+
14
+ embeddings = OpenAIEmbeddings()
15
+
16
+ docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
17
+
18
+ chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
19
+
20
+ def make_inference(query):
21
+ docs = docsearch.get_relevant_documents(query)
22
+ return(chain.run(input_documents=docs, question=query))
23
+
24
+ if __name__ == "__main__":
25
+ # make a gradio interface
26
+ import gradio as gr
27
+
28
+ gr.Interface(
29
+ make_inference,
30
+ [
31
+ gr.inputs.Textbox(lines=2, label="Query"),
32
+ ],
33
+ gr.outputs.Textbox(label="Response"),
34
+ title="hitchhikebot",
35
+ description="hitchhikebot is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
36
+ ).launch()