Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,120 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
)
|
| 3 |
|
| 4 |
# Launch the interface
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from typing import Dict, List, Optional
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
# Configuration
|
| 11 |
+
DASHBOARD_URL = "https://huggyguyjo01-testdashbord.static.hf.space"
|
| 12 |
+
CHAT_INTERFACE_URL = "https://huggyguyjo01-testchat.static.hf.space"
|
| 13 |
+
|
| 14 |
+
class ChatbotBackend:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.conversation_history: List[Dict] = []
|
| 17 |
+
self.user_sessions: Dict = {}
|
| 18 |
+
self.dashboard_settings = self.load_dashboard_settings()
|
| 19 |
+
|
| 20 |
+
def load_dashboard_settings(self) -> Dict:
|
| 21 |
+
"""Load settings from dashboard"""
|
| 22 |
+
try:
|
| 23 |
+
response = requests.get(f"{DASHBOARD_URL}/api/settings")
|
| 24 |
+
return response.json()
|
| 25 |
+
except:
|
| 26 |
+
return {
|
| 27 |
+
"chatbot_name": "AI Assistant",
|
| 28 |
+
"welcome_message": "Bonjour! Comment puis-je vous aider?",
|
| 29 |
+
"behavior": "friendly and helpful",
|
| 30 |
+
"llm_style": "professional"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
async def process_message(self,
|
| 34 |
+
message: str,
|
| 35 |
+
image: Optional[Image.Image] = None,
|
| 36 |
+
session_id: str = "default") -> str:
|
| 37 |
+
"""Process incoming messages and images"""
|
| 38 |
+
|
| 39 |
+
# Initialize user session if needed
|
| 40 |
+
if session_id not in self.user_sessions:
|
| 41 |
+
self.user_sessions[session_id] = {
|
| 42 |
+
"history": [],
|
| 43 |
+
"settings": self.dashboard_settings
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Store message in history
|
| 47 |
+
self.user_sessions[session_id]["history"].append({
|
| 48 |
+
"role": "user",
|
| 49 |
+
"content": message,
|
| 50 |
+
"has_image": image is not None
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
# Process image if present
|
| 54 |
+
image_data = None
|
| 55 |
+
if image:
|
| 56 |
+
# Convert image to base64
|
| 57 |
+
buffered = io.BytesIO()
|
| 58 |
+
image.save(buffered, format="PNG")
|
| 59 |
+
image_data = base64.b64encode(buffered.getvalue()).decode()
|
| 60 |
+
|
| 61 |
+
# Prepare API request
|
| 62 |
+
payload = {
|
| 63 |
+
"message": message,
|
| 64 |
+
"session_id": session_id,
|
| 65 |
+
"image": image_data,
|
| 66 |
+
"history": self.user_sessions[session_id]["history"],
|
| 67 |
+
"settings": self.user_sessions[session_id]["settings"]
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
# Send to processing endpoint
|
| 71 |
+
try:
|
| 72 |
+
response = requests.post(
|
| 73 |
+
f"{DASHBOARD_URL}/api/process",
|
| 74 |
+
json=payload
|
| 75 |
+
)
|
| 76 |
+
bot_response = response.json()["response"]
|
| 77 |
+
except:
|
| 78 |
+
bot_response = "Désolé, une erreur s'est produite."
|
| 79 |
+
|
| 80 |
+
# Store bot response
|
| 81 |
+
self.user_sessions[session_id]["history"].append({
|
| 82 |
+
"role": "assistant",
|
| 83 |
+
"content": bot_response
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
return bot_response
|
| 87 |
+
|
| 88 |
+
def handle_error(self, error: Exception) -> str:
|
| 89 |
+
"""Handle and log errors"""
|
| 90 |
+
error_msg = f"Error: {str(error)}"
|
| 91 |
+
print(error_msg) # Log error
|
| 92 |
+
return "Je suis désolé, une erreur s'est produite. Veuillez réessayer."
|
| 93 |
+
|
| 94 |
+
# Initialize Gradio interface
|
| 95 |
+
chatbot = ChatbotBackend()
|
| 96 |
+
|
| 97 |
+
def chat_interface(message: str,
|
| 98 |
+
image: Optional[Image.Image] = None) -> str:
|
| 99 |
+
"""Gradio interface function"""
|
| 100 |
+
try:
|
| 101 |
+
response = asyncio.run(
|
| 102 |
+
chatbot.process_message(message, image)
|
| 103 |
+
)
|
| 104 |
+
return response
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return chatbot.handle_error(e)
|
| 107 |
+
|
| 108 |
+
# Create Gradio interface
|
| 109 |
+
iface = gr.Interface(
|
| 110 |
+
fn=chat_interface,
|
| 111 |
+
inputs=[
|
| 112 |
+
gr.Textbox(label="Message"),
|
| 113 |
+
gr.Image(label="Upload Image", type="pil", optional=True)
|
| 114 |
+
],
|
| 115 |
+
outputs=gr.Textbox(label="Response"),
|
| 116 |
+
title="AI Chatbot Backend",
|
| 117 |
+
description="Backend service connecting dashboard and chat interface"
|
| 118 |
)
|
| 119 |
|
| 120 |
# Launch the interface
|