Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|