Eddyhzd commited on
Commit
2cbcc71
·
1 Parent(s): 690576f

First commit

Browse files
Files changed (2) hide show
  1. app.py +38 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ cle_api = os.environ.get("CLE_API_MISTRAL")
6
+
7
+ # Initialisation du client Mistral (API compatible OpenAI)
8
+ client = OpenAI(api_key=cle_api, base_url="https://api.mistral.ai/v1")
9
+
10
+ # Chatbot : simple écho Fonction chatbot reliée à Mistral
11
+ def chatbot(message, history):
12
+ # Préparer l’historique dans le format de Mistral
13
+ messages = []
14
+ for user_msg, bot_msg in history:
15
+ messages.append({"role": "user", "content": user_msg})
16
+ messages.append({"role": "assistant", "content": bot_msg})
17
+
18
+ messages.append({"role": "user", "content": message})
19
+
20
+ # Appel API Mistral
21
+ response = client.chat.completions.create(
22
+ model="mistral-small-latest", # tu peux changer le modèle (mistral-medium, mistral-large, etc.)
23
+ messages=messages
24
+ )
25
+
26
+ bot_reply = response.choices[0].message.content.strip()
27
+ history.append(("Vous: " + message, "Bot: " + bot_reply))
28
+ return history, history
29
+
30
+ with gr.Blocks() as demo:
31
+
32
+
33
+ chatbot_ui = gr.Chatbot(label="ChatBot")
34
+ msg = gr.Textbox(placeholder="Écrivez un message...")
35
+
36
+ msg.submit(chatbot, [msg, chatbot_ui], [chatbot_ui, chatbot_ui])
37
+
38
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ os
3
+ openai