nehatarey commited on
Commit
32f1ee7
1 Parent(s): 92a13c1

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("devil_wears_prada.txt") as f:
9
+ devil_wears_prada = f.read()
10
+
11
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
+ texts = text_splitter.split_text(devil_wears_prada)
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="🗣️TalkToMyDoc📄",
35
+ description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Devil Wears Prada script",
36
+ ).launch()