review css and fix open ai key
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from chromadb import PersistentClient
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
from fastapi import FastAPI
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
|
@@ -11,25 +11,25 @@ from langchain.chat_models import ChatOpenAI
|
|
| 11 |
from langchain.schema import HumanMessage, SystemMessage
|
| 12 |
from langchain.embeddings import OpenAIEmbeddings
|
| 13 |
|
| 14 |
-
|
| 15 |
# Configuration
|
| 16 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 17 |
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 18 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
| 19 |
VECTORSTORE_BASE_DIR = "vectorstores_chroma"
|
| 20 |
-
#embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 21 |
-
embedding_model = OpenAIEmbeddings(model="text-embedding-ada-002", openai_api_key=OPENAI_API_KEY)
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
chat_model = ChatOpenAI(
|
| 25 |
temperature=0.7,
|
| 26 |
max_tokens=800,
|
| 27 |
-
|
| 28 |
-
model_name = "gpt-4",
|
| 29 |
openai_api_key=OPENAI_API_KEY
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# FastAPI
|
| 33 |
api = FastAPI()
|
| 34 |
api.add_middleware(
|
| 35 |
CORSMiddleware,
|
|
@@ -55,7 +55,6 @@ def load_collection(domain, subdomain):
|
|
| 55 |
|
| 56 |
def respond(message, history, system_message, domain, subdomain, max_tokens, temperature, top_p):
|
| 57 |
try:
|
| 58 |
-
# Message système selon sous-domaine
|
| 59 |
if subdomain == "incident":
|
| 60 |
system_message = (
|
| 61 |
"Tu es CoMoSolution, un assistant support multilingue de CorisMoney. "
|
|
@@ -77,9 +76,9 @@ def respond(message, history, system_message, domain, subdomain, max_tokens, tem
|
|
| 77 |
)
|
| 78 |
|
| 79 |
collection = load_collection(domain, subdomain)
|
| 80 |
-
#query_embedding = embedding_model.encode(message).tolist()
|
| 81 |
query_embedding = embedding_model.embed_query(message)
|
| 82 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
|
|
|
| 83 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
| 84 |
|
| 85 |
messages = [
|
|
@@ -119,78 +118,8 @@ def login(username, password):
|
|
| 119 |
else:
|
| 120 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
with gr.Blocks(css=""
|
| 124 |
-
body {
|
| 125 |
-
background: linear-gradient(135deg, #0f172a, #1e293b);
|
| 126 |
-
color: #f1f5f9;
|
| 127 |
-
font-family: 'Segoe UI', sans-serif;
|
| 128 |
-
margin: 0;
|
| 129 |
-
padding: 0;
|
| 130 |
-
}
|
| 131 |
-
#login-container {
|
| 132 |
-
max-width: 400px;
|
| 133 |
-
margin: 8% auto;
|
| 134 |
-
padding: 30px;
|
| 135 |
-
background: #334155;
|
| 136 |
-
border-radius: 12px;
|
| 137 |
-
box-shadow: 0px 5px 15px rgba(0,0,0,0.4);
|
| 138 |
-
}
|
| 139 |
-
.gr-box {
|
| 140 |
-
background: transparent !important;
|
| 141 |
-
border: none !important;
|
| 142 |
-
}
|
| 143 |
-
input, textarea, select {
|
| 144 |
-
background-color: #f8fafc !important;
|
| 145 |
-
color: #1e293b !important;
|
| 146 |
-
border-radius: 8px !important;
|
| 147 |
-
border: none;
|
| 148 |
-
padding: 12px !important;
|
| 149 |
-
font-size: 1rem !important;
|
| 150 |
-
}
|
| 151 |
-
button {
|
| 152 |
-
background-color: #38bdf8 !important;
|
| 153 |
-
color: white !important;
|
| 154 |
-
border-radius: 8px !important;
|
| 155 |
-
padding: 10px 16px !important;
|
| 156 |
-
border: none;
|
| 157 |
-
font-weight: bold;
|
| 158 |
-
}
|
| 159 |
-
button:hover {
|
| 160 |
-
background-color: #0ea5e9 !important;
|
| 161 |
-
}
|
| 162 |
-
.gr-chat-interface {
|
| 163 |
-
width: 100% !important;
|
| 164 |
-
height: 100vh;
|
| 165 |
-
background-color: #1e293b;
|
| 166 |
-
border-radius: 16px;
|
| 167 |
-
box-shadow: 0 0 15px rgba(0,0,0,0.4);
|
| 168 |
-
padding: 20px;
|
| 169 |
-
}
|
| 170 |
-
textarea.svelte-1ipelgc {
|
| 171 |
-
height: 140px !important;
|
| 172 |
-
font-size: 1.05rem !important;
|
| 173 |
-
padding: 16px !important;
|
| 174 |
-
border-radius: 10px !important;
|
| 175 |
-
}
|
| 176 |
-
.gr-chat-message {
|
| 177 |
-
font-size: 1.05rem;
|
| 178 |
-
line-height: 1.6;
|
| 179 |
-
padding: 12px 16px;
|
| 180 |
-
background-color: #334155;
|
| 181 |
-
border-radius: 10px;
|
| 182 |
-
margin: 6px 0;
|
| 183 |
-
white-space: pre-wrap;
|
| 184 |
-
word-break: break-word;
|
| 185 |
-
}
|
| 186 |
-
#main-chat-column {
|
| 187 |
-
max-width: 100% !important;
|
| 188 |
-
height : 100vh !important;
|
| 189 |
-
width: 100% !important;
|
| 190 |
-
flex-grow: 1 !important;
|
| 191 |
-
padding: 0 20px;
|
| 192 |
-
}
|
| 193 |
-
""") as app:
|
| 194 |
with gr.Column(visible=True, elem_id="login-container") as login_block:
|
| 195 |
gr.Markdown("## 🔐 Connexion à l'assistant IA")
|
| 196 |
username = gr.Textbox(label="Nom d'utilisateur")
|
|
@@ -224,4 +153,4 @@ textarea.svelte-1ipelgc {
|
|
| 224 |
|
| 225 |
if __name__ == "__main__":
|
| 226 |
threading.Thread(target=start_api).start()
|
| 227 |
-
app.launch()
|
|
|
|
| 1 |
+
# Import section
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from chromadb import PersistentClient
|
|
|
|
| 5 |
from fastapi import FastAPI
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
|
|
|
| 11 |
from langchain.schema import HumanMessage, SystemMessage
|
| 12 |
from langchain.embeddings import OpenAIEmbeddings
|
| 13 |
|
|
|
|
| 14 |
# Configuration
|
| 15 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 16 |
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 17 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
| 18 |
VECTORSTORE_BASE_DIR = "vectorstores_chroma"
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
embedding_model = OpenAIEmbeddings(
|
| 21 |
+
model="text-embedding-ada-002",
|
| 22 |
+
openai_api_key=OPENAI_API_KEY
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
chat_model = ChatOpenAI(
|
| 26 |
temperature=0.7,
|
| 27 |
max_tokens=800,
|
| 28 |
+
model_name="gpt-4",
|
|
|
|
| 29 |
openai_api_key=OPENAI_API_KEY
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# FastAPI setup
|
| 33 |
api = FastAPI()
|
| 34 |
api.add_middleware(
|
| 35 |
CORSMiddleware,
|
|
|
|
| 55 |
|
| 56 |
def respond(message, history, system_message, domain, subdomain, max_tokens, temperature, top_p):
|
| 57 |
try:
|
|
|
|
| 58 |
if subdomain == "incident":
|
| 59 |
system_message = (
|
| 60 |
"Tu es CoMoSolution, un assistant support multilingue de CorisMoney. "
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
collection = load_collection(domain, subdomain)
|
|
|
|
| 79 |
query_embedding = embedding_model.embed_query(message)
|
| 80 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
| 81 |
+
|
| 82 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
| 83 |
|
| 84 |
messages = [
|
|
|
|
| 118 |
else:
|
| 119 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 120 |
|
| 121 |
+
# Gradio Interface
|
| 122 |
+
with gr.Blocks(css="style.css") as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Column(visible=True, elem_id="login-container") as login_block:
|
| 124 |
gr.Markdown("## 🔐 Connexion à l'assistant IA")
|
| 125 |
username = gr.Textbox(label="Nom d'utilisateur")
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
threading.Thread(target=start_api).start()
|
| 156 |
+
app.launch()
|