kadsai commited on
Commit
bf02447
·
verified ·
1 Parent(s): af8716b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -67
app.py CHANGED
@@ -158,6 +158,7 @@ def respond(message, history, system_message, domain, model_name, max_tokens, te
158
 
159
  # --- Interface utilisateur Gradio
160
  with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color: #b8c2d3;}") as app:
 
161
  with gr.Column(visible=True, elem_id="login-block") as login_block:
162
  gr.Markdown("""
163
  <div style="text-align: center; font-size: 24px; font-weight: bold; margin-bottom: 10px;">
@@ -175,29 +176,72 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
175
  <div style="text-align:center; font-size: 22px; font-weight: bold; margin-bottom: 10px;">
176
  🤖 Assistant IA Excelis
177
  </div>
178
- """
179
- )
180
- with gr.Row():
181
- domain = gr.Dropdown(
182
- choices=["corismoney", "ptibank", "cmf", "cma-bf", "assurance", "expertise"],
183
- label="🌐 Domaine d'application", value="corismoney"
184
- )
185
- model_name = gr.Dropdown(
186
- choices=["gpt-3.5-turbo", "gpt-4", "gpt-4o-mini"],
187
- label="🧠 Modèle OpenAI", value="gpt-3.5-turbo"
188
- )
189
 
190
- file_upload = gr.File(label="📄 Upload de document (optionnel)")
 
 
 
 
191
 
192
- with gr.Row():
193
- max_tokens = gr.Slider(1, 2048, value=800, label="🧠 Max tokens")
194
- temperature = gr.Slider(0.1, 2.0, value=0.7, label="🌡️ Température")
195
- top_p = gr.Slider(0.1, 1.0, value=0.95, label="🎯 Top-p")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
- chatbot = gr.Chatbot(label="💬 Chat", type="messages", elem_id="chat-display")
198
- message_input = gr.Textbox(label="✉️ Votre message", placeholder="Tapez votre question ici...")
199
- send_button = gr.Button("🚀 Envoyer", variant="primary")
200
 
 
201
  def chat_submit(msg, history, domain, model_name, max_tokens, temperature, top_p):
202
  if not msg or not msg.strip():
203
  return history, gr.update(value=msg)
@@ -220,54 +264,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
220
  inputs=[user_input, pass_input],
221
  outputs=[login_block, chatbot_block, error_box]
222
  )
223
-
224
- # --- FastAPI app
225
- app_fastapi = FastAPI()
226
-
227
- app_fastapi.add_middleware(
228
- CORSMiddleware,
229
- allow_origins=["*"],
230
- allow_credentials=True,
231
- allow_methods=["*"],
232
- allow_headers=["*"],
233
- )
234
-
235
- class RequestData(BaseModel):
236
- message: str
237
- history: list = []
238
- system_message: str = ""
239
- domain: str
240
- model_name: str = "gpt-3.5-turbo"
241
- max_tokens: int = 800
242
- temperature: float = 0.7
243
- top_p: float = 0.95
244
-
245
- @app_fastapi.post("/ask")
246
- async def api_respond(data: RequestData):
247
- if not data.message.strip():
248
- raise HTTPException(status_code=400, detail="Message vide")
249
- history = data.history or []
250
- response = respond(
251
- message=data.message,
252
- history=history,
253
- system_message=data.system_message,
254
- domain=data.domain,
255
- model_name=data.model_name,
256
- max_tokens=data.max_tokens,
257
- temperature=data.temperature,
258
- top_p=data.top_p
259
- )
260
- return {"history": response}
261
- # -- Start FastApi
262
- def start_api():
263
- uvicorn.run(app_fastapi)
264
-
265
- # -- Start Gradio
266
- #def start_gradio():
267
- # app.launch(server_name="0.0.0.0", server_port=7860, share=False)
268
-
269
  if __name__ == "__main__":
270
- # --Lancer FastAPI avec uvicorn
271
- # threading.Thread(target=start_api).start()
272
  # --Lancer Gradio
273
  app.launch()
 
158
 
159
  # --- Interface utilisateur Gradio
160
  with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color: #b8c2d3;}") as app:
161
+
162
  with gr.Column(visible=True, elem_id="login-block") as login_block:
163
  gr.Markdown("""
164
  <div style="text-align: center; font-size: 24px; font-weight: bold; margin-bottom: 10px;">
 
176
  <div style="text-align:center; font-size: 22px; font-weight: bold; margin-bottom: 10px;">
177
  🤖 Assistant IA Excelis
178
  </div>
179
+ """)
 
 
 
 
 
 
 
 
 
 
180
 
181
+ # 🔽 Mode selector
182
+ mode_selector = gr.Dropdown(
183
+ choices=["chatbotAssistance", "ID Extraction", "Car Detection"],
184
+ label="🎛️ Choisissez un mode", value="chatbotAssistance"
185
+ )
186
 
187
+ # Bloc 1 - Chatbot
188
+ with gr.Column(visible=True) as chatbot_mode_block:
189
+ with gr.Row():
190
+ domain = gr.Dropdown(
191
+ choices=["corismoney", "ptibank", "cmf", "cma-bf", "assurance", "expertise"],
192
+ label="🌐 Domaine d'application", value="corismoney"
193
+ )
194
+ model_name = gr.Dropdown(
195
+ choices=["gpt-3.5-turbo", "gpt-4", "gpt-4o-mini"],
196
+ label="🧠 Modèle OpenAI", value="gpt-3.5-turbo"
197
+ )
198
+
199
+ file_upload = gr.File(label="📄 Upload de document (optionnel)")
200
+
201
+ with gr.Row():
202
+ max_tokens = gr.Slider(1, 2048, value=800, label="🧠 Max tokens")
203
+ temperature = gr.Slider(0.1, 2.0, value=0.7, label="🌡️ Température")
204
+ top_p = gr.Slider(0.1, 1.0, value=0.95, label="🎯 Top-p")
205
+
206
+ chatbot = gr.Chatbot(label="💬 Chat", type="messages", elem_id="chat-display")
207
+ message_input = gr.Textbox(label="✉️ Votre message", placeholder="Tapez votre question ici...")
208
+ send_button = gr.Button("🚀 Envoyer", variant="primary")
209
+
210
+ # Bloc 2 - ID Extraction
211
+ with gr.Column(visible=False) as id_extraction_block:
212
+ gr.Markdown("🪪 **Extraction d'informations d'identité à partir d'image**")
213
+ id_image_input = gr.File(label="Uploader image de pièce d'identité", file_types=["image"], file_count="multiple")
214
+ id_output = gr.Textbox(label="Résultat OCR", lines=6)
215
+ id_button = gr.Button("📤 Extraire les informations")
216
+
217
+ def extract_id_fn(files):
218
+ return f"[Simulé] Infos extraites de {len(files)} image(s)."
219
+
220
+ id_button.click(fn=extract_id_fn, inputs=[id_image_input], outputs=[id_output])
221
+
222
+ # Bloc 3 - Car Detection
223
+ with gr.Column(visible=False) as car_detection_block:
224
+ gr.Markdown("🚗 **Détection de véhicules à partir d'image**")
225
+ car_image_input = gr.File(label="Uploader image de voiture", file_types=["image"], file_count="multiple")
226
+ car_output = gr.Textbox(label="Résultat détection", lines=6)
227
+ car_button = gr.Button("🔍 Détecter")
228
+
229
+ def car_detect_fn(files):
230
+ return f"[Simulé] {len(files)} voiture(s) détectée(s)."
231
+
232
+ car_button.click(fn=car_detect_fn, inputs=[car_image_input], outputs=[car_output])
233
+
234
+ # 🔁 Fonction pour afficher le bon bloc
235
+ def toggle_mode(mode):
236
+ return (
237
+ gr.update(visible=mode == "chatbotAssistance"),
238
+ gr.update(visible=mode == "ID Extraction"),
239
+ gr.update(visible=mode == "Car Detection")
240
+ )
241
 
242
+ mode_selector.change(fn=toggle_mode, inputs=[mode_selector], outputs=[chatbot_mode_block, id_extraction_block, car_detection_block])
 
 
243
 
244
+ # 📨 Envoi pour le chatbot seulement
245
  def chat_submit(msg, history, domain, model_name, max_tokens, temperature, top_p):
246
  if not msg or not msg.strip():
247
  return history, gr.update(value=msg)
 
264
  inputs=[user_input, pass_input],
265
  outputs=[login_block, chatbot_block, error_box]
266
  )
267
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  if __name__ == "__main__":
 
 
269
  # --Lancer Gradio
270
  app.launch()