File size: 3,223 Bytes
f80f5d0
 
2124a36
 
 
 
 
f80f5d0
2124a36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f80f5d0
2124a36
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import AutoTokenizer, AutoModel
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.embeddings import HuggingFaceEmbeddings
import fitz  # PyMuPDF

# Function to get available models from Hugging Face
def get_hf_models():
    return ["Qwen/Qwen2.5-3B-Instruct", "HuggingFaceH4/zephyr-7b-beta", "mistralai/Mistral-7B-Instruct-v0.1"]

# Function to extract text from a PDF
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

# Function for manual RAG
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

# Function for classic RAG
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

# Function for response without RAG
def no_rag(query, client):
    response = client.text_generation(query, max_new_tokens=512)
    return response

# Gradio interface function
def process_query(query, pdf_path, llm_choice, embedder_choice):
    client = InferenceClient(llm_choice)
    full_text = extract_text_from_pdf(pdf_path)
    no_rag_response = no_rag(query, client)
    manual_rag_response = manual_rag(query, full_text, client)
    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

# Create Gradio interface
iface = gr.Interface(
    fn=process_query,
    inputs=[
        gr.Textbox(label="Votre question"),
        gr.File(label="Chargez votre PDF"),
        gr.Dropdown(choices=get_hf_models(), label="Choisissez le LLM", value="Qwen/Qwen2.5-3B-Instruct"),
        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")
    ],
    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"
)

# Launch the application
if __name__ == "__main__":
    iface.launch()