Spaces:
Runtime error
Runtime error
Khachatur Mirijanyan
commited on
Commit
•
da55f4e
1
Parent(s):
9005141
All files redy
Browse files- app.py +34 -5
- guide1.txt +0 -0
- requirements.txt +4 -0
app.py
CHANGED
@@ -1,7 +1,36 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="🗣️TalkToMyDoc📄",
|
35 |
+
description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
|
36 |
+
).launch()
|
guide1.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
tiktoken
|
4 |
+
chromadb
|