Maryem2025 commited on
Commit
23bdbfd
·
verified ·
1 Parent(s): d6260c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -120
app.py CHANGED
@@ -1,120 +1,129 @@
1
- import os
2
- from huggingface_hub import login
3
- from datasets import load_dataset
4
- import gradio as gr
5
- from llama_cpp import Llama
6
- from huggingface_hub import hf_hub_download
7
- import chromadb
8
- from sentence_transformers import SentenceTransformer
9
-
10
- # Authentification via un secret
11
- hf_token = os.getenv("HF_TOKEN") # Récupérer le token depuis les secrets
12
- login(hf_token)
13
-
14
- # Charger le dataset
15
- dataset = load_dataset("Maryem2025/dataset-train") # Changez le nom si nécessaire
16
-
17
- # Initialisation du modèle Llama
18
- llm = Llama(
19
- model_path=hf_hub_download(
20
- repo_id="TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF",
21
- filename="capybarahermes-2.5-mistral-7b.Q2_K.gguf",
22
- ),
23
- n_ctx=2048,
24
- n_gpu_layers=50, # Ajustez selon votre VRAM
25
- )
26
-
27
- # Initialisation de ChromaDB Vector Store
28
- class VectorStore:
29
- def __init__(self, collection_name):
30
- self.embedding_model = SentenceTransformer('sentence-transformers/multi-qa-MiniLM-L6-cos-v1')
31
- self.chroma_client = chromadb.Client()
32
-
33
- # Supprimer la collection existante si elle existe
34
- if collection_name in self.chroma_client.list_collections():
35
- self.chroma_client.delete_collection(collection_name)
36
-
37
- # Créer une nouvelle collection
38
- self.collection = self.chroma_client.create_collection(name=collection_name)
39
-
40
- def populate_vectors(self, dataset):
41
- # Sélectionner les colonnes pertinentes à concaténer
42
- names = dataset['train']['name'][:20]
43
- ingredients = dataset['train']['ingredients'][:20]
44
- instructions = dataset['train']['instructions'][:20]
45
- cuisine = dataset['train']['cuisine'][:20]
46
- total_time = dataset['train']['total_time'][:20]
47
-
48
- # Concaténer les textes à partir des colonnes sélectionnées
49
- texts = [
50
- f"Name: {name}. Ingredients: {ingr}. Instructions: {instr}. Cuisine: {cui}. Total time: {total} minutes."
51
- for name, ingr, instr, cui, total in zip(names, ingredients, instructions, cuisine, total_time)
52
- ]
53
-
54
- # Ajouter les embeddings au store de vecteurs
55
- for i, item in enumerate(texts):
56
- embeddings = self.embedding_model.encode(item).tolist()
57
- self.collection.add(embeddings=[embeddings], documents=[item], ids=[str(i)])
58
-
59
- def search_context(self, query, n_results=1):
60
- query_embedding = self.embedding_model.encode([query]).tolist()
61
- results = self.collection.query(query_embeddings=query_embedding, n_results=n_results)
62
- return results['documents']
63
-
64
- # Initialisation du store de vecteurs et peuplement
65
- dataset = load_dataset('Maryem2025/dataset-test')
66
- vector_store = VectorStore("embedding_vector")
67
- vector_store.populate_vectors(dataset)
68
-
69
- # Fonction pour générer du texte
70
- def generate_text(message, max_tokens, temperature, top_p):
71
- # Récupérer le contexte depuis le store de vecteurs
72
- context_results = vector_store.search_context(message, n_results=1)
73
- context = context_results[0] if context_results else ""
74
-
75
- # Créer le modèle de prompt
76
- prompt_template = (
77
- f"SYSTEM: You are a recipe generating bot.\n"
78
- f"SYSTEM: {context}\n"
79
- f"USER: {message}\n"
80
- f"ASSISTANT:\n"
81
- )
82
-
83
- # Générer le texte avec le modèle de langue
84
- output = llm(
85
- prompt_template,
86
- temperature=0.3,
87
- top_p=0.95,
88
- top_k=40,
89
- repeat_penalty=1.1,
90
- max_tokens=600,
91
- )
92
-
93
- # Traiter la sortie
94
- input_string = output['choices'][0]['text'].strip()
95
- cleaned_text = input_string.strip("[]'").replace('\\n', '\n')
96
- continuous_text = '\n'.join(cleaned_text.split('\n'))
97
- return continuous_text
98
-
99
- # Définir l'interface Gradio
100
- demo = gr.Interface(
101
- fn=generate_text,
102
- inputs=[
103
- gr.Textbox(lines=2, placeholder="Enter your message here...", label="Message"),
104
- ],
105
- outputs=gr.Textbox(label="Generated Text"),
106
- title="Chatbot - Your Personal Culinary Advisor: Discover What to Cook Next!",
107
- description="Running LLM with context retrieval from ChromaDB",
108
- examples=[
109
- ["I have leftover rice, what can I make out of it?"],
110
- ["I just have some milk and chocolate, what dessert can I make?"],
111
- ["I am allergic to coconut milk, what can I use instead in a Thai curry?"],
112
- ["Can you suggest a vegan breakfast recipe?"],
113
- ["How do I make a perfect scrambled egg?"],
114
- ["Can you guide me through making a soufflé?"],
115
- ],
116
- cache_examples=False,
117
- )
118
-
119
- if __name__ == "__main__":
120
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import login
3
+ from datasets import load_dataset
4
+ import gradio as gr
5
+ from llama_cpp import Llama
6
+ from huggingface_hub import hf_hub_download
7
+ import chromadb
8
+ from sentence_transformers import SentenceTransformer
9
+
10
+ from dotenv import load_dotenv
11
+
12
+
13
+ # Charger le fichier .env
14
+ load_dotenv()
15
+
16
+ # Lire le token depuis l'environnement
17
+ hf_token = os.getenv("HF_TOKEN")
18
+
19
+ # Authentification via un secret
20
+ hf_token = os.getenv("HF_TOKEN") # Récupérer le token depuis les secrets
21
+ login(hf_token)
22
+
23
+ # Charger le dataset
24
+ dataset = load_dataset("Maryem2025/dataset-train") # Changez le nom si nécessaire
25
+
26
+ # Initialisation du modèle Llama
27
+ llm = Llama(
28
+ model_path=hf_hub_download(
29
+ repo_id="TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF",
30
+ filename="capybarahermes-2.5-mistral-7b.Q2_K.gguf",
31
+ ),
32
+ n_ctx=2048,
33
+ n_gpu_layers=50, # Ajustez selon votre VRAM
34
+ )
35
+
36
+ # Initialisation de ChromaDB Vector Store
37
+ class VectorStore:
38
+ def __init__(self, collection_name):
39
+ self.embedding_model = SentenceTransformer('sentence-transformers/multi-qa-MiniLM-L6-cos-v1')
40
+ self.chroma_client = chromadb.Client()
41
+
42
+ # Supprimer la collection existante si elle existe
43
+ if collection_name in self.chroma_client.list_collections():
44
+ self.chroma_client.delete_collection(collection_name)
45
+
46
+ # Créer une nouvelle collection
47
+ self.collection = self.chroma_client.create_collection(name=collection_name)
48
+
49
+ def populate_vectors(self, dataset):
50
+ # Sélectionner les colonnes pertinentes à concaténer
51
+ names = dataset['train']['name'][:20]
52
+ ingredients = dataset['train']['ingredients'][:20]
53
+ instructions = dataset['train']['instructions'][:20]
54
+ cuisine = dataset['train']['cuisine'][:20]
55
+ total_time = dataset['train']['total_time'][:20]
56
+
57
+ # Concaténer les textes à partir des colonnes sélectionnées
58
+ texts = [
59
+ f"Name: {name}. Ingredients: {ingr}. Instructions: {instr}. Cuisine: {cui}. Total time: {total} minutes."
60
+ for name, ingr, instr, cui, total in zip(names, ingredients, instructions, cuisine, total_time)
61
+ ]
62
+
63
+ # Ajouter les embeddings au store de vecteurs
64
+ for i, item in enumerate(texts):
65
+ embeddings = self.embedding_model.encode(item).tolist()
66
+ self.collection.add(embeddings=[embeddings], documents=[item], ids=[str(i)])
67
+
68
+ def search_context(self, query, n_results=1):
69
+ query_embedding = self.embedding_model.encode([query]).tolist()
70
+ results = self.collection.query(query_embeddings=query_embedding, n_results=n_results)
71
+ return results['documents']
72
+
73
+ # Initialisation du store de vecteurs et peuplement
74
+ dataset = load_dataset('Maryem2025/dataset-test')
75
+ vector_store = VectorStore("embedding_vector")
76
+ vector_store.populate_vectors(dataset)
77
+
78
+ # Fonction pour générer du texte
79
+ def generate_text(message, max_tokens, temperature, top_p):
80
+ # Récupérer le contexte depuis le store de vecteurs
81
+ context_results = vector_store.search_context(message, n_results=1)
82
+ context = context_results[0] if context_results else ""
83
+
84
+ # Créer le modèle de prompt
85
+ prompt_template = (
86
+ f"SYSTEM: You are a recipe generating bot.\n"
87
+ f"SYSTEM: {context}\n"
88
+ f"USER: {message}\n"
89
+ f"ASSISTANT:\n"
90
+ )
91
+
92
+ # Générer le texte avec le modèle de langue
93
+ output = llm(
94
+ prompt_template,
95
+ temperature=0.3,
96
+ top_p=0.95,
97
+ top_k=40,
98
+ repeat_penalty=1.1,
99
+ max_tokens=600,
100
+ )
101
+
102
+ # Traiter la sortie
103
+ input_string = output['choices'][0]['text'].strip()
104
+ cleaned_text = input_string.strip("[]'").replace('\\n', '\n')
105
+ continuous_text = '\n'.join(cleaned_text.split('\n'))
106
+ return continuous_text
107
+
108
+ # Définir l'interface Gradio
109
+ demo = gr.Interface(
110
+ fn=generate_text,
111
+ inputs=[
112
+ gr.Textbox(lines=2, placeholder="Enter your message here...", label="Message"),
113
+ ],
114
+ outputs=gr.Textbox(label="Generated Text"),
115
+ title="Chatbot - Your Personal Culinary Advisor: Discover What to Cook Next!",
116
+ description="Running LLM with context retrieval from ChromaDB",
117
+ examples=[
118
+ ["I have leftover rice, what can I make out of it?"],
119
+ ["I just have some milk and chocolate, what dessert can I make?"],
120
+ ["I am allergic to coconut milk, what can I use instead in a Thai curry?"],
121
+ ["Can you suggest a vegan breakfast recipe?"],
122
+ ["How do I make a perfect scrambled egg?"],
123
+ ["Can you guide me through making a soufflé?"],
124
+ ],
125
+ cache_examples=False,
126
+ )
127
+
128
+ if __name__ == "__main__":
129
+ demo.launch()