File size: 5,612 Bytes
23bdbfd
 
 
 
 
 
 
 
7fd9767
23bdbfd
3b3e30d
 
23bdbfd
3b3e30d
23bdbfd
 
7fd9767
 
 
 
23bdbfd
 
 
 
 
7fd9767
23bdbfd
 
 
 
 
7fd9767
23bdbfd
 
7fd9767
23bdbfd
 
 
 
 
 
 
 
 
 
242a8d9
 
 
7fd9767
 
23bdbfd
 
 
7fd9767
 
23bdbfd
 
7fd9767
 
 
23bdbfd
 
7fd9767
 
 
 
 
 
 
 
 
 
 
 
23bdbfd
 
 
 
 
 
7fd9767
 
23bdbfd
 
 
 
 
7fd9767
 
 
23bdbfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fd9767
 
23bdbfd
 
7fd9767
23bdbfd
 
 
 
 
 
7fd9767
 
 
 
23bdbfd
 
7fd9767
23bdbfd
 
 
 
 
 
 
7fd9767
23bdbfd
7fd9767
23bdbfd
 
 
 
 
 
 
7fd9767
23bdbfd
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
from huggingface_hub import login
from datasets import load_dataset
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import chromadb
from sentence_transformers import SentenceTransformer
import time

# Charger le token depuis les secrets
hf_token = os.getenv("HF_TOKEN")  # Assurez-vous que 'HF_TOKEN' est bien le nom du secret Hugging Face

# Connecte-toi à Hugging Face
login(hf_token)

# Charger le dataset une seule fois
dataset = load_dataset("Maryem2025/dataset-test")  # Chargez le dataset une fois

# Initialisation du modèle Llama avec une taille de contexte réduite
llm = Llama(
    model_path=hf_hub_download(
        repo_id="TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF",
        filename="capybarahermes-2.5-mistral-7b.Q2_K.gguf",
    ),
    n_ctx=1024,  # Réduire la taille du contexte
    n_gpu_layers=50,  # Ajustez selon votre VRAM
)

# Initialisation de ChromaDB Vector Store
class VectorStore:
    def __init__(self, collection_name, batch_size=10):
        self.embedding_model = SentenceTransformer('sentence-transformers/multi-qa-MiniLM-L6-cos-v1')
        self.chroma_client = chromadb.Client()
        self.batch_size = batch_size

        # Supprimer la collection existante si elle existe
        if collection_name in self.chroma_client.list_collections():
            self.chroma_client.delete_collection(collection_name)
        
        # Créer une nouvelle collection
        self.collection = self.chroma_client.create_collection(name=collection_name)

    def populate_vectors(self, dataset):
        # Sélectionner les colonnes pertinentes à concaténer
        names = dataset['train']['name'][:200]
        ingredients = dataset['train']['ingredients'][:200]
        instructions = dataset['train']['instructions'][:200]
        cuisine = dataset['train']['cuisine'][:200]
        total_time = dataset['train']['total_time'][:200]

        # Concaténer les textes à partir des colonnes sélectionnées
        texts = [
            f"Name: {name}. Ingredients: {ingr}. Instructions: {instr}. Cuisine: {cui}. Total time: {total} minutes."
            for name, ingr, instr, cui, total in zip(names, ingredients, instructions, cuisine, total_time)
        ]

        embeddings_batch = []
        documents_batch = []

        for i, item in enumerate(texts):
            embeddings = self.embedding_model.encode(item).tolist()
            embeddings_batch.append(embeddings)
            documents_batch.append(item)

            # Quand le batch est plein, on ajoute les embeddings
            if len(embeddings_batch) >= self.batch_size:
                self.collection.add(embeddings=embeddings_batch, documents=documents_batch, ids=[str(i) for i in range(i - self.batch_size + 1, i + 1)])
                embeddings_batch = []
                documents_batch = []

        # Ajouter les derniers items restants s'il y en a
        if embeddings_batch:
            self.collection.add(embeddings=embeddings_batch, documents=documents_batch, ids=[str(i) for i in range(len(texts) - len(embeddings_batch), len(texts))])

    def search_context(self, query, n_results=1):
        query_embedding = self.embedding_model.encode([query]).tolist()
        results = self.collection.query(query_embeddings=query_embedding, n_results=n_results)
        return results['documents']


# Initialisation du store de vecteurs et peuplement du dataset
vector_store = VectorStore("embedding_vector")
vector_store.populate_vectors(dataset)

# Fonction pour générer du texte
def generate_text(message, max_tokens, temperature, top_p):
    # Profiler le temps d'exécution de la génération de texte
    start_time = time.time()

    # Récupérer le contexte depuis le store de vecteurs
    context_results = vector_store.search_context(message, n_results=1)
    context = context_results[0] if context_results else ""

    # Créer le modèle de prompt
    prompt_template = (
        f"SYSTEM: You are a recipe generating bot.\n"
        f"SYSTEM: {context}\n"
        f"USER: {message}\n"
        f"ASSISTANT:\n"
    )

    # Générer le texte avec le modèle de langue
    output = llm(
        prompt_template,
        temperature=temperature,
        top_p=top_p,
        top_k=40,
        repeat_penalty=1.1,
        max_tokens=max_tokens,
    )

    # Traiter la sortie
    input_string = output['choices'][0]['text'].strip()
    cleaned_text = input_string.strip("[]'").replace('\\n', '\n')
    continuous_text = '\n'.join(cleaned_text.split('\n'))

    # Afficher le temps d'exécution
    print(f"Temps d'exécution pour générer du texte : {time.time() - start_time} secondes")

    return continuous_text


# Définir l'interface Gradio
demo = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter your message here...", label="Message"),
    ],
    outputs=gr.Textbox(label="Generated Text"),
    title="Chatbot - Your Personal Culinary Advisor",
    description="Running LLM with context retrieval from ChromaDB",
    cache_examples=False,  # Désactivez le cache
    examples=[
        ["I have leftover rice, what can I make out of it?"],
        ["I just have some milk and chocolate, what dessert can I make?"],
        ["I am allergic to coconut milk, what can I use instead in a Thai curry?"],
        ["Can you suggest a vegan breakfast recipe?"],
        ["How do I make a perfect scrambled egg?"],
        ["Can you guide me through making a soufflé?"],
    ],  # Réduire le nombre d'exemples pour accélérer
)

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