Spaces:
Sleeping
Sleeping
Solarum Asteridion
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,58 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
-
from openai.error import OpenAIError
|
| 4 |
import gradio as gr
|
| 5 |
import datetime
|
| 6 |
import pytz
|
| 7 |
import logging
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
@staticmethod
|
| 11 |
-
def get_memory_usage():
|
| 12 |
-
# Placeholder for memory usage tracking
|
| 13 |
-
return "0.00 GB"
|
| 14 |
-
|
| 15 |
-
@staticmethod
|
| 16 |
-
def clear_memory():
|
| 17 |
-
# Placeholder for memory clearing
|
| 18 |
-
pass
|
| 19 |
-
|
| 20 |
logging.basicConfig(
|
| 21 |
level=logging.INFO,
|
| 22 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 23 |
)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
|
|
|
| 26 |
def setup_openai_auth():
|
| 27 |
-
|
| 28 |
-
if
|
| 29 |
-
raise Exception("
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
class OpenAILLMHandler:
|
| 33 |
def __init__(self):
|
| 34 |
-
self.model = "gpt-
|
| 35 |
-
self.
|
| 36 |
-
self.cache_manager =
|
| 37 |
self.generation_lock = Lock()
|
| 38 |
-
|
|
|
|
| 39 |
def generate_response(self, prompt: str, max_length: int = 512) -> str:
|
| 40 |
cache_key = f"{prompt[:100]}_{max_length}"
|
| 41 |
cached_response = self.cache_manager.get(cache_key)
|
| 42 |
if cached_response:
|
|
|
|
| 43 |
return cached_response
|
| 44 |
-
|
| 45 |
try:
|
| 46 |
with self.generation_lock:
|
| 47 |
-
response =
|
| 48 |
model=self.model,
|
| 49 |
messages=[{"role": "user", "content": prompt}],
|
| 50 |
max_tokens=max_length,
|
| 51 |
-
n=1,
|
| 52 |
-
stop=None,
|
| 53 |
temperature=0.7,
|
| 54 |
)
|
| 55 |
|
| 56 |
-
response_text = response.choices[0].message.content
|
| 57 |
|
| 58 |
-
self.cache_manager
|
|
|
|
| 59 |
return response_text
|
| 60 |
|
| 61 |
except OpenAIError as e:
|
|
@@ -94,7 +91,7 @@ Make it a challenge to guess if you are an AI.
|
|
| 94 |
* Be slightly curious, sometimes ask questions
|
| 95 |
* Be respectful and polite if the user is polite; be rough if the user is rough
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
llm_handler = OpenAILLMHandler()
|
| 99 |
|
| 100 |
def generate_response(user_message: str, conversation_history: list) -> str:
|
|
@@ -111,22 +108,26 @@ def generate_response(user_message: str, conversation_history: list) -> str:
|
|
| 111 |
|
| 112 |
return llm_handler.generate_response(prompt)
|
| 113 |
|
| 114 |
-
def chatbot_interface(user_message: str, history: list
|
| 115 |
-
if
|
| 116 |
-
history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
ai_response = generate_response(user_message, history)
|
| 119 |
history.append({"role": "user", "content": user_message})
|
| 120 |
history.append({"role": "assistant", "content": ai_response})
|
| 121 |
-
return history
|
| 122 |
|
| 123 |
-
# Gradio
|
| 124 |
custom_css = """
|
| 125 |
@import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;600&display=swap');
|
| 126 |
|
| 127 |
body, .gradio-container {
|
| 128 |
font-family: 'Raleway', sans-serif;
|
| 129 |
-
background-color: #
|
| 130 |
padding: 20px;
|
| 131 |
}
|
| 132 |
|
|
@@ -135,37 +136,67 @@ body, .gradio-container {
|
|
| 135 |
overflow-y: auto;
|
| 136 |
background-color: #ffffff;
|
| 137 |
border-radius: 10px;
|
| 138 |
-
padding:
|
| 139 |
font-size: 16px;
|
| 140 |
-
box-shadow: 0 4px
|
| 141 |
}
|
| 142 |
|
| 143 |
.message {
|
| 144 |
-
margin:
|
| 145 |
-
padding:
|
| 146 |
border-radius: 8px;
|
|
|
|
| 147 |
}
|
| 148 |
|
| 149 |
.user-message {
|
| 150 |
-
background-color: #
|
| 151 |
-
|
|
|
|
| 152 |
}
|
| 153 |
|
| 154 |
.bot-message {
|
| 155 |
-
background-color: #
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
}
|
| 158 |
"""
|
| 159 |
|
| 160 |
with gr.Blocks(css=custom_css) as demo:
|
| 161 |
-
gr.Markdown("<h1 style='text-align: center; color: #
|
| 162 |
|
| 163 |
with gr.Row():
|
| 164 |
-
load_button = gr.Button("Call Human", variant="primary")
|
| 165 |
model_status = gr.Textbox(
|
| 166 |
label="Human Arrival Status",
|
| 167 |
value="Human Not Listening.",
|
| 168 |
-
interactive=False
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
with gr.Row():
|
|
@@ -184,37 +215,47 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 184 |
elem_id="textbox"
|
| 185 |
)
|
| 186 |
send = gr.Button("➤", elem_id="send-button")
|
| 187 |
-
|
| 188 |
def load_model_click():
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
def update_chat(user_message, history):
|
| 193 |
if not user_message.strip():
|
| 194 |
-
return history,
|
| 195 |
-
if llm_handler.model
|
| 196 |
-
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
-
return
|
| 200 |
-
|
| 201 |
# Event handlers
|
| 202 |
load_button.click(
|
| 203 |
load_model_click,
|
| 204 |
outputs=[model_status]
|
| 205 |
)
|
| 206 |
|
| 207 |
-
|
| 208 |
update_chat,
|
| 209 |
inputs=[msg, chatbot],
|
| 210 |
-
outputs=[chatbot,
|
| 211 |
)
|
| 212 |
-
|
| 213 |
-
|
| 214 |
update_chat,
|
| 215 |
inputs=[msg, chatbot],
|
| 216 |
-
outputs=[chatbot,
|
| 217 |
)
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|
| 220 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import datetime
|
| 5 |
import pytz
|
| 6 |
import logging
|
| 7 |
+
from openai import OpenAI, OpenAIError
|
| 8 |
+
from threading import Lock
|
| 9 |
|
| 10 |
+
# Configure logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.INFO,
|
| 13 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 14 |
)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
+
# Set up OpenAI authentication
|
| 18 |
def setup_openai_auth():
|
| 19 |
+
api_key = os.environ.get("LLM_API_KEY")
|
| 20 |
+
if not api_key:
|
| 21 |
+
raise Exception("LLM API authentication failed. Please set your API key.")
|
| 22 |
+
client = OpenAI(api_key=api_key)
|
| 23 |
+
return client
|
| 24 |
+
|
| 25 |
+
# Initialize OpenAI client
|
| 26 |
+
client = setup_openai_auth()
|
| 27 |
|
| 28 |
class OpenAILLMHandler:
|
| 29 |
def __init__(self):
|
| 30 |
+
self.model = os.environ.get("MODEL_NAME", "gpt-4")
|
| 31 |
+
self.base_url = os.environ.get("LLM_API_BASE_URL", "https://api.openai.com")
|
| 32 |
+
self.cache_manager = {} # Simple in-memory cache; consider using Redis or similar for production
|
| 33 |
self.generation_lock = Lock()
|
| 34 |
+
self.client = client # Using the initialized OpenAI client
|
| 35 |
+
|
| 36 |
def generate_response(self, prompt: str, max_length: int = 512) -> str:
|
| 37 |
cache_key = f"{prompt[:100]}_{max_length}"
|
| 38 |
cached_response = self.cache_manager.get(cache_key)
|
| 39 |
if cached_response:
|
| 40 |
+
logger.info("Returning cached response.")
|
| 41 |
return cached_response
|
| 42 |
+
|
| 43 |
try:
|
| 44 |
with self.generation_lock:
|
| 45 |
+
response = self.client.chat.completions.create(
|
| 46 |
model=self.model,
|
| 47 |
messages=[{"role": "user", "content": prompt}],
|
| 48 |
max_tokens=max_length,
|
|
|
|
|
|
|
| 49 |
temperature=0.7,
|
| 50 |
)
|
| 51 |
|
| 52 |
+
response_text = response.choices[0].message.content.strip()
|
| 53 |
|
| 54 |
+
self.cache_manager[cache_key] = response_text
|
| 55 |
+
logger.info("Response generated and cached.")
|
| 56 |
return response_text
|
| 57 |
|
| 58 |
except OpenAIError as e:
|
|
|
|
| 91 |
* Be slightly curious, sometimes ask questions
|
| 92 |
* Be respectful and polite if the user is polite; be rough if the user is rough
|
| 93 |
"""
|
| 94 |
+
|
| 95 |
llm_handler = OpenAILLMHandler()
|
| 96 |
|
| 97 |
def generate_response(user_message: str, conversation_history: list) -> str:
|
|
|
|
| 108 |
|
| 109 |
return llm_handler.generate_response(prompt)
|
| 110 |
|
| 111 |
+
def chatbot_interface(user_message: str, history: list) -> list:
|
| 112 |
+
if not user_message.strip():
|
| 113 |
+
return history
|
| 114 |
+
|
| 115 |
+
if not llm_handler.model:
|
| 116 |
+
history.append({"role": "system", "content": "Error: Please call the Human first."})
|
| 117 |
+
return history
|
| 118 |
|
| 119 |
ai_response = generate_response(user_message, history)
|
| 120 |
history.append({"role": "user", "content": user_message})
|
| 121 |
history.append({"role": "assistant", "content": ai_response})
|
| 122 |
+
return history
|
| 123 |
|
| 124 |
+
# Enhanced Gradio UI with improved CSS and layout
|
| 125 |
custom_css = """
|
| 126 |
@import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;600&display=swap');
|
| 127 |
|
| 128 |
body, .gradio-container {
|
| 129 |
font-family: 'Raleway', sans-serif;
|
| 130 |
+
background-color: #f0f2f5;
|
| 131 |
padding: 20px;
|
| 132 |
}
|
| 133 |
|
|
|
|
| 136 |
overflow-y: auto;
|
| 137 |
background-color: #ffffff;
|
| 138 |
border-radius: 10px;
|
| 139 |
+
padding: 15px;
|
| 140 |
font-size: 16px;
|
| 141 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 142 |
}
|
| 143 |
|
| 144 |
.message {
|
| 145 |
+
margin: 10px 0;
|
| 146 |
+
padding: 10px;
|
| 147 |
border-radius: 8px;
|
| 148 |
+
max-width: 80%;
|
| 149 |
}
|
| 150 |
|
| 151 |
.user-message {
|
| 152 |
+
background-color: #d1e7dd;
|
| 153 |
+
align-self: flex-end;
|
| 154 |
+
margin-left: auto;
|
| 155 |
}
|
| 156 |
|
| 157 |
.bot-message {
|
| 158 |
+
background-color: #f8d7da;
|
| 159 |
+
align-self: flex-start;
|
| 160 |
+
margin-right: auto;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
#textbox {
|
| 164 |
+
width: 100%;
|
| 165 |
+
padding: 10px;
|
| 166 |
+
border: 1px solid #ced4da;
|
| 167 |
+
border-radius: 5px;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
#send-button, #load-button {
|
| 171 |
+
background-color: #0d6efd;
|
| 172 |
+
color: white;
|
| 173 |
+
border: none;
|
| 174 |
+
padding: 10px 20px;
|
| 175 |
+
border-radius: 5px;
|
| 176 |
+
cursor: pointer;
|
| 177 |
+
margin-left: 10px;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
#send-button:hover, #load-button:hover {
|
| 181 |
+
background-color: #0b5ed7;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
.gr-button:disabled {
|
| 185 |
+
background-color: #6c757d !important;
|
| 186 |
+
cursor: not-allowed;
|
| 187 |
}
|
| 188 |
"""
|
| 189 |
|
| 190 |
with gr.Blocks(css=custom_css) as demo:
|
| 191 |
+
gr.Markdown("<h1 style='text-align: center; color: #0d6efd;'>Human.</h1>")
|
| 192 |
|
| 193 |
with gr.Row():
|
| 194 |
+
load_button = gr.Button("Call Human", variant="primary", elem_id="load-button")
|
| 195 |
model_status = gr.Textbox(
|
| 196 |
label="Human Arrival Status",
|
| 197 |
value="Human Not Listening.",
|
| 198 |
+
interactive=False,
|
| 199 |
+
elem_id="model-status"
|
| 200 |
)
|
| 201 |
|
| 202 |
with gr.Row():
|
|
|
|
| 215 |
elem_id="textbox"
|
| 216 |
)
|
| 217 |
send = gr.Button("➤", elem_id="send-button")
|
| 218 |
+
|
| 219 |
def load_model_click():
|
| 220 |
+
if llm_handler.model:
|
| 221 |
+
return "Human Already Listening."
|
| 222 |
+
try:
|
| 223 |
+
# Reload the model name from environment if needed
|
| 224 |
+
llm_handler.model = os.environ.get("MODEL_NAME", "gpt-4")
|
| 225 |
+
if not llm_handler.model:
|
| 226 |
+
return "Failed to load model. Please set the MODEL_NAME environment variable."
|
| 227 |
+
return "Human Called Successfully."
|
| 228 |
+
except Exception as e:
|
| 229 |
+
logger.error(f"Error loading model: {e}")
|
| 230 |
+
return f"Error loading model: {str(e)}"
|
| 231 |
+
|
| 232 |
def update_chat(user_message, history):
|
| 233 |
if not user_message.strip():
|
| 234 |
+
return history, gr.update(value="")
|
| 235 |
+
if not llm_handler.model:
|
| 236 |
+
history.append({"role": "system", "content": "Error: Please call the Human first."})
|
| 237 |
+
return history, gr.update(value="")
|
| 238 |
|
| 239 |
+
updated_history = chatbot_interface(user_message, history)
|
| 240 |
+
return updated_history, gr.update(value="")
|
| 241 |
+
|
| 242 |
# Event handlers
|
| 243 |
load_button.click(
|
| 244 |
load_model_click,
|
| 245 |
outputs=[model_status]
|
| 246 |
)
|
| 247 |
|
| 248 |
+
send.click(
|
| 249 |
update_chat,
|
| 250 |
inputs=[msg, chatbot],
|
| 251 |
+
outputs=[chatbot, msg]
|
| 252 |
)
|
| 253 |
+
|
| 254 |
+
msg.submit(
|
| 255 |
update_chat,
|
| 256 |
inputs=[msg, chatbot],
|
| 257 |
+
outputs=[chatbot, msg]
|
| 258 |
)
|
| 259 |
|
| 260 |
if __name__ == "__main__":
|
| 261 |
+
demo.launch(share=True)
|