Chris Alexiuk commited on
Commit
90aeb4d
1 Parent(s): e635da9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings.openai import OpenAIEmbeddings
2
+ from langchain.text_splitter import NLTKTextSplitter
3
+ from langchain.vectorstores import Chroma
4
+ from langchain.docstore.document import Document
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.indexes.vectorstore import VectorstoreIndexCreator
7
+ from langchain.chains.question_answering import load_qa_chain
8
+ from langchain.llms import OpenAI
9
+ import nltk
10
+ import os
11
+
12
+ nltk.download("punkt")
13
+
14
+ with open("guide1.txt") as f:
15
+ hitchhikersguide = f.read()
16
+
17
+ text_splitter = NLTKTextSplitter()
18
+ texts = text_splitter.split_text(hitchhikersguide)
19
+
20
+
21
+ def make_inference(query, openai_key=""):
22
+ os.environ["OPENAI_API_KEY"] = openai_key
23
+ embeddings = OpenAIEmbeddings()
24
+ docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
25
+ chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine")
26
+ docs = docsearch.get_relevant_documents(query)
27
+ chain({"input_documents": docs, "question": query}, return_only_outputs=True)
28
+
29
+ if __name__ == "__main__":
30
+ # make a gradio interface
31
+ import gradio as gr
32
+
33
+ gr.Interface(
34
+ make_inference,
35
+ [
36
+ gr.inputs.Textbox(lines=2, label="Query"),
37
+ gr.inputs.Textbox(lines=2, label="OpenAI Key"),
38
+ ],
39
+ gr.outputs.Textbox(label="Response"),
40
+ title="🗣️TalkToMyDoc📄",
41
+ description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
42
+ ).launch()