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