Maryem2025 commited on
Commit
2b15466
·
verified ·
1 Parent(s): 0dde60e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -64
app.py CHANGED
@@ -1,64 +1,120 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- 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
+ # 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()