File size: 3,453 Bytes
f80f5d0
 
2124a36
 
d8bdf2c
 
2124a36
83a9d0d
f80f5d0
2124a36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8bdf2c
2124a36
 
83a9d0d
d8bdf2c
83a9d0d
 
 
d8bdf2c
 
 
 
 
 
 
2124a36
83a9d0d
2124a36
 
d8bdf2c
2124a36
 
 
 
83a9d0d
d8bdf2c
2124a36
d8bdf2c
 
f80f5d0
2124a36
 
 
 
 
 
 
 
 
 
f80f5d0
 
 
2124a36
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import AutoTokenizer, AutoModel
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
import fitz  # PyMuPDF


def get_hf_models():
    return ["Qwen/Qwen2.5-3B-Instruct", "HuggingFaceH4/zephyr-7b-beta", "mistralai/Mistral-7B-Instruct-v0.1"]

def extract_text_from_pdf(pdf_path):
    text = ""
    with fitz.open(pdf_path) as doc:
        for page in doc:
            text += page.get_text()
    return text

def manual_rag(query, context, client):
    prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
    response = client.text_generation(prompt, max_new_tokens=512)
    return response

def classic_rag(query, pdf_path, client, embedder):
    text = extract_text_from_pdf(pdf_path)
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    chunks = text_splitter.split_text(text)
    embeddings = HuggingFaceEmbeddings(model_name=embedder)
    db = FAISS.from_texts(chunks, embeddings)
    docs = db.similarity_search(query, k=3)
    context = " ".join([doc.page_content for doc in docs])
    response = manual_rag(query, context, client)
    return response, context

def no_rag(query, client):
    response = client.text_generation(query, max_new_tokens=512)
    return response

def process_query(query, pdf_path, llm_choice, embedder_choice, use_manual_rag):
    client = InferenceClient(llm_choice)
    no_rag_response = no_rag(query, client)

    if pdf_path is None:
        return no_rag_response, "RAG non utilisé (pas de fichier PDF)", "RAG non utilisé (pas de fichier PDF)", "Pas de fichier PDF fourni", "Pas de contexte extrait"

    full_text = extract_text_from_pdf(pdf_path)

    # RAG manuel seulement si choisi
    if use_manual_rag == "Oui":
        manual_rag_response = manual_rag(query, full_text, client)
    else:
        manual_rag_response = "RAG manuel non utilisé"

    classic_rag_response, classic_rag_context = classic_rag(query, pdf_path, client, embedder_choice)

    return no_rag_response, manual_rag_response, classic_rag_response, full_text, classic_rag_context


iface = gr.Interface(
    fn=process_query,
    inputs=[
        gr.Textbox(label="Votre question"),
        gr.File(label="Chargez un nouveau PDF"),
        gr.Dropdown(choices=get_hf_models(), label="Choisissez le LLM", value="HuggingFaceH4/zephyr-7b-beta"),
        gr.Dropdown(choices=["sentence-transformers/all-MiniLM-L6-v2", "nomic-ai/nomic-embed-text-v1.5"],
                    label="Choisissez l'Embedder", value="sentence-transformers/all-MiniLM-L6-v2"),
        gr.Dropdown(choices=["Oui", "Non"], label="Utiliser RAG manuel ?", value="Non")  # Ajout de la combobox pour choisir RAG manuel
    ],
    outputs=[
        gr.Textbox(label="Réponse sans RAG"),
        gr.Textbox(label="Réponse avec RAG manuel"),
        gr.Textbox(label="Réponse avec RAG classique"),
        gr.Textbox(label="Texte complet du PDF (pour RAG manuel)", lines=10),
        gr.Textbox(label="Contexte extrait (pour RAG classique)", lines=10)
    ],
    title="Tutoriel RAG - Comparaison des méthodes",
    description="Posez une question sur le contenu d'un PDF et comparez les réponses obtenues avec différentes méthodes de RAG.",
    theme="default"
)

if __name__ == "__main__":
    iface.launch()