NicolasGaudemet commited on
Commit
9a514d8
0 Parent(s):

Duplicate from NicolasGaudemet/LongTextQuestioner

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. document_questioner_app.py +50 -0
  4. requirements.txt +5 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: LongTextQuestioner
3
+ emoji: 🌖
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.28.0
8
+ app_file: document_questioner_app.py
9
+ pinned: false
10
+ duplicated_from: NicolasGaudemet/LongTextQuestioner
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
document_questioner_app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ import gradio as gr
4
+ from langchain.document_loaders import DirectoryLoader, TextLoader, UnstructuredFileLoader
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.vectorstores import Chroma
7
+ from langchain.chains import RetrievalQA
8
+ from langchain.chat_models import ChatOpenAI
9
+
10
+ os.environ["OPENAI_API_KEY"] = "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"
11
+
12
+ def question_document(Document, Question):
13
+ # Load documents with DirectoryLoader
14
+
15
+ if not Document.name.endswith('.txt'):
16
+ return ("Le document doit être un fichier texte (.txt)")
17
+
18
+ loader = TextLoader(Document.name, encoding = "ISO-8859-1")
19
+
20
+ #loader = DirectoryLoader("", glob="*.txt", loader_kwargs = {"encoding" : "ISO-8859-1"})
21
+ txt_docs = loader.load_and_split()
22
+
23
+ # Create embeddings
24
+ embeddings = OpenAIEmbeddings()
25
+ # Write in DB
26
+ txt_docsearch = Chroma.from_documents(txt_docs, embeddings)
27
+
28
+ # Define LLM
29
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)
30
+
31
+ # Create Retriever
32
+ qa_txt = RetrievalQA.from_chain_type(llm=llm,
33
+ chain_type="map_reduce",
34
+ retriever=txt_docsearch.as_retriever()
35
+ )
36
+
37
+ answer = qa_txt.run(Question) #+ "If you don't find the answer in the document, don't answer, say you don't know, in the language of the question." )
38
+ return answer
39
+
40
+ #Définition de l'interface
41
+
42
+ iface = gr.Interface(
43
+ fn = question_document,
44
+ inputs= ["file","text"],
45
+ outputs = gr.outputs.Textbox(label="Réponse"),
46
+ title="Long Text Questioner",
47
+ description="par Nicolas \nPermet d'interroger un document texte",
48
+ allow_flagging = "never")
49
+
50
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai
2
+ langchain
3
+ unstructured
4
+ chromadb
5
+ tiktoken