File size: 1,780 Bytes
9a514d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import openai
import os
import gradio as gr
from langchain.document_loaders import DirectoryLoader, TextLoader, UnstructuredFileLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI

os.environ["OPENAI_API_KEY"] = "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"

def question_document(Document, Question):
    # Load documents with DirectoryLoader 

    if not Document.name.endswith('.txt'):
        return ("Le document doit être un fichier texte (.txt)")
    
    loader = TextLoader(Document.name, encoding = "ISO-8859-1")

    #loader = DirectoryLoader("", glob="*.txt", loader_kwargs = {"encoding" : "ISO-8859-1"})
    txt_docs = loader.load_and_split()

    # Create embeddings
    embeddings = OpenAIEmbeddings()
    # Write in DB
    txt_docsearch = Chroma.from_documents(txt_docs, embeddings)

    # Define LLM
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)

    # Create Retriever
    qa_txt = RetrievalQA.from_chain_type(llm=llm, 
                                        chain_type="map_reduce",
                                        retriever=txt_docsearch.as_retriever()
                                        )

    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." )
    return answer 

#Définition de l'interface

iface = gr.Interface(
    fn = question_document, 
    inputs= ["file","text"],
    outputs = gr.outputs.Textbox(label="Réponse"), 
    title="Long Text Questioner",
    description="par Nicolas \nPermet d'interroger un document texte",
    allow_flagging = "never")

iface.launch()