Spaces:
Running
Running
File size: 3,984 Bytes
f80f5d0 2124a36 83a9d0d f80f5d0 2124a36 83a9d0d 2124a36 83a9d0d 2124a36 83a9d0d 2124a36 83a9d0d 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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
import os
import hashlib
# Directory to store cached files
CACHE_DIR = "pdf_cache"
os.makedirs(CACHE_DIR, exist_ok=True)
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 cache_file(file):
if file is None:
return None
file_hash = hashlib.md5(file.read()).hexdigest()
cached_path = os.path.join(CACHE_DIR, f"{file_hash}.pdf")
if not os.path.exists(cached_path):
with open(cached_path, "wb") as f:
file.seek(0)
f.write(file.read())
return cached_path
def get_cached_files():
return [f for f in os.listdir(CACHE_DIR) if f.endswith('.pdf')]
def process_query(query, pdf_file, cached_file, llm_choice, embedder_choice):
client = InferenceClient(llm_choice)
no_rag_response = no_rag(query, client)
if pdf_file is not None:
pdf_path = cache_file(pdf_file)
elif cached_file:
pdf_path = os.path.join(CACHE_DIR, cached_file)
else:
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)
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
iface = gr.Interface(
fn=process_query,
inputs=[
gr.Textbox(label="Votre question"),
gr.File(label="Chargez un nouveau PDF"),
gr.Dropdown(choices=get_cached_files, label="Ou choisissez un PDF déjà téléversé", interactive=True),
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"
)
if __name__ == "__main__":
iface.launch()
|