Soguy commited on
Commit
3b4ac2c
·
1 Parent(s): 2a4c4e7

Changement de modele : Mistral 7B

Browse files
Files changed (7) hide show
  1. .gitignore +1 -0
  2. app backup.py +91 -0
  3. app.py +18 -3
  4. logo.png +0 -0
  5. poetry.lock +0 -0
  6. pyproject.toml +22 -0
  7. test.py +26 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app backup.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ # Charger les variables d'environnement
7
+ load_dotenv()
8
+
9
+ # Vérifie si la clé API est définie dans l'environnement
10
+ api_token = os.getenv("HF_API_TOKEN")
11
+ model_name = "mistralai/Mistral-7B-Instruct-v0.3"
12
+
13
+ if not api_token:
14
+ raise ValueError("API token is required. Please set it in your .env file.")
15
+
16
+ # Initialiser le client d'inférence
17
+ client = InferenceClient(token=api_token, model=model_name)
18
+
19
+ # Fonction de réponse
20
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
21
+ messages = [{"role": "system", "content": system_message}]
22
+ for user, assistant in history:
23
+ if user:
24
+ messages.append({"role": "user", "content": user})
25
+ if assistant:
26
+ messages.append({"role": "assistant", "content": assistant})
27
+
28
+ messages.append({"role": "user", "content": message})
29
+
30
+ response = ""
31
+ try:
32
+ for message in client.chat_completion(
33
+ messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
34
+ ):
35
+ token = message.choices[0].delta.content
36
+ response += token
37
+ yield response
38
+ except Exception as e:
39
+ yield f"Error: {str(e)}"
40
+
41
+ # Interface utilisateur avec Gradio
42
+ demo = gr.Blocks()
43
+
44
+ with demo:
45
+ # Header avec le logo et le slogan
46
+ with gr.Row():
47
+ gr.Image("logo.png", show_label=False, interactive=False, elem_id="logo", scale=1)
48
+ gr.Markdown(
49
+ """
50
+ <div style="text-align: center;">
51
+ <h1 style="color: #0fa86b;">SHAURI</h1>
52
+ <p style="font-size: 16px; color: #000;">Un test avec le modèle Mistral via l'API Hugging Face</p>
53
+ </div>
54
+ """
55
+ )
56
+
57
+ # Composant Chatbot
58
+ chatbot = gr.Chatbot(label="Chat History")
59
+
60
+ # Zone de saisie utilisateur
61
+ with gr.Row():
62
+ user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=1)
63
+
64
+ # Réglages du système et paramètres
65
+ with gr.Row():
66
+ system_message = gr.Textbox(
67
+ value="You are a friendly Chatbot.",
68
+ label="System Message",
69
+ lines=2,
70
+ )
71
+ with gr.Row():
72
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
73
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
74
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
75
+
76
+ # Interaction entre l'utilisateur et le chatbot
77
+ def handle_message(message, history, system_msg, max_toks, temp, top_p_val):
78
+ if history is None:
79
+ history = []
80
+ for response in respond(message, history, system_msg, max_toks, temp, top_p_val):
81
+ history.append((message, response))
82
+ yield history, ""
83
+
84
+ user_input.submit(
85
+ fn=handle_message,
86
+ inputs=[user_input, chatbot, system_message, max_tokens, temperature, top_p],
87
+ outputs=[chatbot, user_input],
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()
app.py CHANGED
@@ -1,11 +1,26 @@
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,
@@ -61,4 +76,4 @@ demo = gr.ChatInterface(
61
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import os
4
+
5
+ from dotenv import load_dotenv
6
+ load_dotenv() # Charge les variables depuis .env
7
 
8
  """
9
+ 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
10
  """
 
11
 
12
+ # Vérifie si la clé API est définie dans l'environnement (pour local)
13
+ api_token = os.getenv("HF_API_TOKEN")
14
+
15
+ if api_token:
16
+ # Local : Utilise la clé API
17
+ print("Local key found in .env")
18
+ client = InferenceClient(token=api_token, model="HuggingFaceH4/zephyr-7b-beta")
19
+ else:
20
+ # Sur Hugging Face Spaces : Pas besoin de clé
21
+ print("We are on HF, no need API key")
22
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
23
+
24
 
25
  def respond(
26
  message,
 
76
 
77
 
78
  if __name__ == "__main__":
79
+ demo.launch()
logo.png ADDED
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "gradio-chatbot-test"
3
+ version = "0.1.0"
4
+ description = "Test huggingface"
5
+ authors = ["Guillaume Soto"]
6
+ license = "Open"
7
+ readme = "README.md"
8
+ package-mode = false
9
+
10
+ [tool.poetry.dependencies]
11
+ python = "^3.12"
12
+ gradio = "^5.5.0"
13
+ huggingface-hub = "^0.26.2"
14
+ python-dotenv = "^1.0.1"
15
+ langchain = "^0.3.7"
16
+ requests = "^2.32.3"
17
+
18
+
19
+
20
+ [build-system]
21
+ requires = ["poetry-core"]
22
+ build-backend = "poetry.core.masonry.api"
test.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ # Charger les variables d'environnement depuis .env
6
+ load_dotenv()
7
+
8
+ # Récupérer la clé API Hugging Face depuis .env
9
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
10
+
11
+ # URL de l'API d'inférence
12
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B"
13
+ headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
14
+
15
+ # Fonction pour envoyer une requête
16
+ def query(payload):
17
+ response = requests.post(API_URL, headers=headers, json=payload)
18
+ return response.json()
19
+
20
+ # Test du modèle avec un exemple simple
21
+ output = query({
22
+ "inputs": "What are the benefits of artificial intelligence in education?",
23
+ })
24
+
25
+ # Afficher le résultat
26
+ print(output)