-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistant.py
82 lines (64 loc) · 3.18 KB
/
assistant.py
1
2
3
4
5
6
7
8
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
from PIL import Image
import openai
import gradio as gr
from dotenv import load_dotenv
import os
load_dotenv()
# Votre clé API OpenAI (remplacez-la par la vôtre)
openai.api_key = os.getenv("OPENAI_API_KEY")
# ID des assistants (Remplacez par vos vrais IDs)
assistant_ids = {
"GPTBay": "asst_jkf2zv9jlzYj4prJ7dHECOWL",
"specialiste_du_vrac": "asst_HSUZYtNegNGOfXRC3p7NqInX",
"Scraping and Crawling Expert Code (RAG)": "asst_Lt1AWleH509FJYTVu0sy9AUE",
"Hybrid Designer": "asst_5l3HvMS8AoKRahvrhph1Z5L0",
"Math tuthor": "asst_9vmMiolEozJTNjQN4LBBM3AX"
}
# Fonction pour interroger l'API OpenAI en utilisant l'ID de l'assistant
def chat_with_assistant(input_text, assistant, temp, contrast, image):
assistant_id = assistant_ids[assistant] # Récupérer l'ID de l'assistant sélectionné
# Créer le prompt en fonction de l'assistant sélectionné
prompt = f"Utilisateur: {input_text}\nAssistant ({assistant_id}):"
# Appel à l'API OpenAI
try:
response = openai.Completion.create(
engine="GPT-4o-mini", # Ou GPT-4 si vous avez accès
prompt=prompt,
temperature=temp,
max_tokens=1500,
n=1,
stop=["Utilisateur:", "Assistant:"]
)
response_text = response.choices[0].text.strip()
# Réponse personnalisée avec icône
response_text = f"{'🤖' if assistant == 'OBD2 Diagnostic' else '👨💻'} {response_text}"
if image:
response_text += f"\nVous avez téléchargé une image : {image.name}."
return response_text
except Exception as e:
return f"Erreur avec l'API OpenAI: {str(e)}"
# Liste des assistants disponibles
assistants = ["GPTBay", "specialiste_du_vrac"]
# Interface utilisateur avec Gradio
with gr.Blocks() as interface:
# Titre, sous-titre et image
with gr.Row():
gr.Markdown("# 🧰 Mes assistants Mécaniciens 🔧\n### Des assistants spécialisés dans plusieurs facettes du domaine de la Mécanique.")
# Layout de la barre latérale et du chat
with gr.Row():
with gr.Column():
assistant_choice = gr.Radio(label="Sélectionnez un Assistant:", choices=assistants, value=assistants[0])
file_upload = gr.File(label="Télécharger une image ou fichier:")
temperature = gr.Slider(label="Température du modèle", minimum=0, maximum=1, value=0.7, step=0.1)
contrast_mode = gr.Radio(label="Mode Contraste de l'image:", choices=["Clair", "Sombre"], value="Clair")
with gr.Column():
chat_window = gr.Chatbot(label="Chat", height=400)
user_input = gr.Textbox(label="Votre message ici", placeholder="Tapez votre question...")
# Bouton d'envoi
send_button = gr.Button("Envoyer")
# Annonce sous le chat
gr.Markdown("Visitez [🤖GPTsIndex🤖](http://www.gpts-index.com) pour d’autres applications IA!!")
# Action du bouton envoyer
send_button.click(chat_with_assistant, inputs=[user_input, assistant_choice, temperature, contrast_mode, file_upload], outputs=chat_window)
# Lancer l'interface Gradio
interface.launch()