format: reformat gradio ui
Browse files- gradio_chatbot.py +61 -8
gradio_chatbot.py
CHANGED
|
@@ -6,6 +6,7 @@ from typing import List, Tuple, Dict, Optional
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
from enum import Enum
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
# Environment configuration
|
| 11 |
env = environs.Env()
|
|
@@ -57,6 +58,47 @@ CUSTOM_CSS = """
|
|
| 57 |
.gradio-container {
|
| 58 |
font-family: 'UI Sans Serif', 'System UI', sans-serif;
|
| 59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
"""
|
| 61 |
|
| 62 |
class MessageStatus(Enum):
|
|
@@ -89,7 +131,7 @@ class ChatAPI:
|
|
| 89 |
Returns:
|
| 90 |
MessageResponse: The response from the API
|
| 91 |
"""
|
| 92 |
-
|
| 93 |
try:
|
| 94 |
async with httpx.AsyncClient() as client:
|
| 95 |
response = await client.post(
|
|
@@ -100,11 +142,12 @@ class ChatAPI:
|
|
| 100 |
"model": "gpt-3.5-turbo",
|
| 101 |
"completion_id": "new_chat",
|
| 102 |
"stream": True
|
| 103 |
-
}
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
if response.status_code != 200:
|
| 107 |
-
|
| 108 |
return MessageResponse(
|
| 109 |
status=MessageStatus.ERROR,
|
| 110 |
content="",
|
|
@@ -112,25 +155,33 @@ class ChatAPI:
|
|
| 112 |
)
|
| 113 |
|
| 114 |
result = response.json()
|
| 115 |
-
|
| 116 |
|
| 117 |
if "choices" in result and len(result["choices"]) > 0:
|
| 118 |
message = result["choices"][0].get("message", {})
|
| 119 |
content = message.get("content", "Content not found")
|
| 120 |
-
|
| 121 |
return MessageResponse(
|
| 122 |
status=MessageStatus.SUCCESS,
|
| 123 |
content=content
|
| 124 |
)
|
| 125 |
else:
|
|
|
|
| 126 |
return MessageResponse(
|
| 127 |
status=MessageStatus.ERROR,
|
| 128 |
content="",
|
| 129 |
error="Invalid API response"
|
| 130 |
)
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
-
|
| 134 |
return MessageResponse(
|
| 135 |
status=MessageStatus.ERROR,
|
| 136 |
content="",
|
|
@@ -170,7 +221,8 @@ class ChatInterface:
|
|
| 170 |
label="Chat History",
|
| 171 |
height=400,
|
| 172 |
show_copy_button=True,
|
| 173 |
-
avatar_images=(USER_AVATAR, BOT_AVATAR)
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
# Message input area
|
|
@@ -179,7 +231,8 @@ class ChatInterface:
|
|
| 179 |
label="Your Message",
|
| 180 |
placeholder="Enter your question here...",
|
| 181 |
lines=3,
|
| 182 |
-
scale=4
|
|
|
|
| 183 |
)
|
| 184 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 185 |
|
|
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
from enum import Enum
|
| 8 |
import os
|
| 9 |
+
from loguru import logger
|
| 10 |
|
| 11 |
# Environment configuration
|
| 12 |
env = environs.Env()
|
|
|
|
| 58 |
.gradio-container {
|
| 59 |
font-family: 'UI Sans Serif', 'System UI', sans-serif;
|
| 60 |
}
|
| 61 |
+
|
| 62 |
+
/* Improve chat interface */
|
| 63 |
+
.chat-message {
|
| 64 |
+
padding: 1rem;
|
| 65 |
+
border-radius: 0.5rem;
|
| 66 |
+
margin-bottom: 1rem;
|
| 67 |
+
display: flex;
|
| 68 |
+
align-items: flex-start;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
.chat-message.user {
|
| 72 |
+
background-color: #f3f4f6;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.chat-message.bot {
|
| 76 |
+
background-color: #eef2ff;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* Improve button styles */
|
| 80 |
+
button {
|
| 81 |
+
transition: all 0.2s ease-in-out;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
button:hover {
|
| 85 |
+
transform: translateY(-1px);
|
| 86 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/* Improve input area */
|
| 90 |
+
textarea {
|
| 91 |
+
border-radius: 0.5rem;
|
| 92 |
+
padding: 0.75rem;
|
| 93 |
+
border: 1px solid #e5e7eb;
|
| 94 |
+
transition: border-color 0.2s ease-in-out;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
textarea:focus {
|
| 98 |
+
border-color: #4f46e5;
|
| 99 |
+
outline: none;
|
| 100 |
+
box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.1);
|
| 101 |
+
}
|
| 102 |
"""
|
| 103 |
|
| 104 |
class MessageStatus(Enum):
|
|
|
|
| 131 |
Returns:
|
| 132 |
MessageResponse: The response from the API
|
| 133 |
"""
|
| 134 |
+
logger.debug(f"Calling chat API with prompt: {prompt}")
|
| 135 |
try:
|
| 136 |
async with httpx.AsyncClient() as client:
|
| 137 |
response = await client.post(
|
|
|
|
| 142 |
"model": "gpt-3.5-turbo",
|
| 143 |
"completion_id": "new_chat",
|
| 144 |
"stream": True
|
| 145 |
+
},
|
| 146 |
+
timeout=30.0 # Add timeout
|
| 147 |
)
|
| 148 |
|
| 149 |
if response.status_code != 200:
|
| 150 |
+
logger.error(f"API Error: {response.text}")
|
| 151 |
return MessageResponse(
|
| 152 |
status=MessageStatus.ERROR,
|
| 153 |
content="",
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
result = response.json()
|
| 158 |
+
logger.debug(f"API response: {result}")
|
| 159 |
|
| 160 |
if "choices" in result and len(result["choices"]) > 0:
|
| 161 |
message = result["choices"][0].get("message", {})
|
| 162 |
content = message.get("content", "Content not found")
|
| 163 |
+
logger.info(f"Last message: {content}")
|
| 164 |
return MessageResponse(
|
| 165 |
status=MessageStatus.SUCCESS,
|
| 166 |
content=content
|
| 167 |
)
|
| 168 |
else:
|
| 169 |
+
logger.error("Invalid API response")
|
| 170 |
return MessageResponse(
|
| 171 |
status=MessageStatus.ERROR,
|
| 172 |
content="",
|
| 173 |
error="Invalid API response"
|
| 174 |
)
|
| 175 |
|
| 176 |
+
except httpx.TimeoutException:
|
| 177 |
+
logger.error("API request timed out")
|
| 178 |
+
return MessageResponse(
|
| 179 |
+
status=MessageStatus.ERROR,
|
| 180 |
+
content="",
|
| 181 |
+
error="Request timed out. Please try again."
|
| 182 |
+
)
|
| 183 |
except Exception as e:
|
| 184 |
+
logger.error(f"Error: {str(e)}")
|
| 185 |
return MessageResponse(
|
| 186 |
status=MessageStatus.ERROR,
|
| 187 |
content="",
|
|
|
|
| 221 |
label="Chat History",
|
| 222 |
height=400,
|
| 223 |
show_copy_button=True,
|
| 224 |
+
avatar_images=(USER_AVATAR, BOT_AVATAR),
|
| 225 |
+
elem_classes=["chat-message"]
|
| 226 |
)
|
| 227 |
|
| 228 |
# Message input area
|
|
|
|
| 231 |
label="Your Message",
|
| 232 |
placeholder="Enter your question here...",
|
| 233 |
lines=3,
|
| 234 |
+
scale=4,
|
| 235 |
+
elem_classes=["message-input"]
|
| 236 |
)
|
| 237 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 238 |
|