Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from pydantic import BaseModel
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
-
from
|
| 10 |
from langchain.schema import HumanMessage, SystemMessage
|
| 11 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 12 |
|
| 13 |
-
#
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 15 |
-
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 16 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
|
|
|
| 17 |
VECTORSTORE_BASE_DIR = "vectorstores"
|
| 18 |
|
|
|
|
| 19 |
embedding_model = OpenAIEmbeddings(
|
| 20 |
model="text-embedding-ada-002",
|
| 21 |
openai_api_key=OPENAI_API_KEY
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
temperature=0.7,
|
| 26 |
-
max_tokens=800,
|
| 27 |
-
model_name="gpt-4",
|
| 28 |
-
openai_api_key=OPENAI_API_KEY
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# FastAPI setup
|
| 32 |
api = FastAPI()
|
| 33 |
api.add_middleware(
|
| 34 |
CORSMiddleware,
|
|
@@ -57,7 +52,6 @@ def load_collection(domain):
|
|
| 57 |
|
| 58 |
def respond(message, history, system_message, domain, max_tokens, temperature, top_p):
|
| 59 |
try:
|
| 60 |
-
# Personnalisation du message système selon domaine
|
| 61 |
if domain.lower() == "corismoney":
|
| 62 |
system_message = (
|
| 63 |
"Tu es CoMoSolution, un assistant support multilingue de CorisMoney. "
|
|
@@ -69,7 +63,6 @@ def respond(message, history, system_message, domain, max_tokens, temperature, t
|
|
| 69 |
collection = load_collection(domain)
|
| 70 |
query_embedding = embedding_model.embed_query(message)
|
| 71 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
| 72 |
-
|
| 73 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
| 74 |
|
| 75 |
messages = [
|
|
@@ -87,61 +80,61 @@ def respond(message, history, system_message, domain, max_tokens, temperature, t
|
|
| 87 |
|
| 88 |
result = chat.invoke(messages)
|
| 89 |
content = result.content
|
| 90 |
-
history.append(
|
| 91 |
-
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
-
return f"❌ Erreur : {e}"
|
| 94 |
|
| 95 |
@api.post("/ask")
|
| 96 |
def ask_api(query: Query):
|
| 97 |
try:
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
return {"error": str(e)}
|
| 102 |
|
| 103 |
def start_api():
|
| 104 |
uvicorn.run(api, host="0.0.0.0", port=7880)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
if username == EXCELIS_USER and password == EXCELIS_MDP:
|
| 108 |
-
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 109 |
-
else:
|
| 110 |
-
return gr.update(visible=False), gr.update(visible=True), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 111 |
-
|
| 112 |
-
# Gradio Interface
|
| 113 |
with gr.Blocks() as app:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import threading
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
|
| 10 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
| 11 |
+
from langchain_community.vectorstores import Chroma
|
| 12 |
from langchain.schema import HumanMessage, SystemMessage
|
|
|
|
| 13 |
|
| 14 |
+
# --- Configuration d'environnement
|
| 15 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 16 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
| 17 |
+
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 18 |
VECTORSTORE_BASE_DIR = "vectorstores"
|
| 19 |
|
| 20 |
+
# --- Initialisation modèles
|
| 21 |
embedding_model = OpenAIEmbeddings(
|
| 22 |
model="text-embedding-ada-002",
|
| 23 |
openai_api_key=OPENAI_API_KEY
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# --- FastAPI pour API externe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
api = FastAPI()
|
| 28 |
api.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 52 |
|
| 53 |
def respond(message, history, system_message, domain, max_tokens, temperature, top_p):
|
| 54 |
try:
|
|
|
|
| 55 |
if domain.lower() == "corismoney":
|
| 56 |
system_message = (
|
| 57 |
"Tu es CoMoSolution, un assistant support multilingue de CorisMoney. "
|
|
|
|
| 63 |
collection = load_collection(domain)
|
| 64 |
query_embedding = embedding_model.embed_query(message)
|
| 65 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
|
|
|
| 66 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
| 67 |
|
| 68 |
messages = [
|
|
|
|
| 80 |
|
| 81 |
result = chat.invoke(messages)
|
| 82 |
content = result.content
|
| 83 |
+
history.append({"role": "user", "content": message})
|
| 84 |
+
history.append({"role": "assistant", "content": content})
|
| 85 |
+
return history
|
| 86 |
except Exception as e:
|
| 87 |
+
return [{"role": "assistant", "content": f"❌ Erreur : {e}"}]
|
| 88 |
|
| 89 |
@api.post("/ask")
|
| 90 |
def ask_api(query: Query):
|
| 91 |
try:
|
| 92 |
+
history = []
|
| 93 |
+
response = respond(query.message, history, query.system_message, query.domain, query.max_tokens, query.temperature, query.top_p)
|
| 94 |
+
return {"response": response[-1]["content"]}
|
| 95 |
except Exception as e:
|
| 96 |
return {"error": str(e)}
|
| 97 |
|
| 98 |
def start_api():
|
| 99 |
uvicorn.run(api, host="0.0.0.0", port=7880)
|
| 100 |
|
| 101 |
+
# --- Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Blocks() as app:
|
| 103 |
+
# --- Bloc Connexion
|
| 104 |
+
with gr.Column(visible=True) as login_block:
|
| 105 |
+
gr.Markdown("## 🔐 Connexion")
|
| 106 |
+
user_input = gr.Textbox(label="Nom d'utilisateur")
|
| 107 |
+
pass_input = gr.Textbox(label="Mot de passe", type="password")
|
| 108 |
+
login_button = gr.Button("Se connecter")
|
| 109 |
+
error_box = gr.Textbox(visible=False, interactive=False)
|
| 110 |
+
|
| 111 |
+
# --- Bloc Chatbot (caché avant connexion)
|
| 112 |
+
with gr.Column(visible=False) as chatbot_block:
|
| 113 |
+
domain = gr.Dropdown(choices=["corismoney", "ptibank", "cmf", "cma-bf", "assurance", "expertise"], label="Application", value="corismoney")
|
| 114 |
+
max_tokens = gr.Slider(1, 2048, value=800, label="Max tokens")
|
| 115 |
+
temperature = gr.Slider(0.1, 2.0, value=0.7, label="Température")
|
| 116 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, label="Top-p")
|
| 117 |
+
|
| 118 |
+
chatbot = gr.Chatbot(label="Assistant IA", value=[], type="messages")
|
| 119 |
+
message_input = gr.Textbox(label="Votre message")
|
| 120 |
+
send_button = gr.Button("Envoyer")
|
| 121 |
+
|
| 122 |
+
def chat_submit(msg, history, domain, max_tokens, temperature, top_p):
|
| 123 |
+
return respond(msg, history, "", domain, max_tokens, temperature, top_p)
|
| 124 |
+
|
| 125 |
+
send_button.click(
|
| 126 |
+
fn=chat_submit,
|
| 127 |
+
inputs=[message_input, chatbot, domain, max_tokens, temperature, top_p],
|
| 128 |
+
outputs=[chatbot]
|
| 129 |
+
)
|
| 130 |
|
| 131 |
+
# --- Fonction Connexion
|
| 132 |
+
def login(username, password):
|
| 133 |
+
if username == EXCELIS_USER and password == EXCELIS_MDP:
|
| 134 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
| 135 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 136 |
|
| 137 |
+
login_button.click(
|
| 138 |
+
fn=login,
|
| 139 |
+
inputs=[user_input, pass_input],
|
| 140 |
+
outputs=[login_block, chatbot_block,]()
|