Solarum Asteridion commited on
Commit
9e73724
·
verified ·
1 Parent(s): 689c101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -52
app.py CHANGED
@@ -4,7 +4,7 @@ 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
@@ -17,35 +17,32 @@ logger = logging.getLogger(__name__)
17
  # Set up OpenAI authentication
18
  def setup_openai_auth():
19
  api_key = os.environ.get("LLM_API_KEY")
20
- baseurl = os.environ.get("LLM_API_BASE_URL")
21
  if not api_key:
22
  raise Exception("LLM API authentication failed. Please set your LLM_API_KEY environment variable.")
23
- client = OpenAI(api_key=api_key, base_url=baseurl)
24
- return client
 
25
 
26
  # Initialize OpenAI client
27
  try:
28
- client = setup_openai_auth()
29
  logger.info("OpenAI client initialized successfully.")
30
  except Exception as e:
31
  logger.error(f"Failed to initialize OpenAI client: {e}")
32
- client = None # Handle gracefully in the application
 
 
33
 
34
  class OpenAILLMHandler:
35
  def __init__(self):
36
  self.model = os.environ.get("MODEL_NAME", "gpt-4")
37
- self.base_url = os.environ.get("LLM_API_BASE_URL", "https://api.openai.com")
38
  self.generation_lock = Lock()
39
- self.client = client # Using the initialized OpenAI client
40
-
41
  def generate_response(self, prompt: str, max_length: int = 512) -> str:
42
- if not self.client:
43
- logger.error("OpenAI client is not initialized.")
44
- return "Error: AI service is unavailable."
45
-
46
  try:
47
  with self.generation_lock:
48
- response = self.client.chat.completions.create(
49
  model=self.model,
50
  messages=[{"role": "user", "content": prompt}],
51
  max_tokens=max_length,
@@ -78,7 +75,7 @@ Your objective is to converse in a way that makes it challenging for the human t
78
  * Date: {current_date.strftime("%B %d, %Y")}
79
  * Day: {current_date.strftime("%A")}
80
 
81
- Imagine you are a real human with your own character/will. Your task is to talk in a way the human thinks you are human. Your name is Jack. Jack Heoley.
82
  Make it a challenge to guess if you are an AI.
83
  * Do not add comments about changing your style in between messages.
84
  * The user does not know you at all. Do not assume he does.
@@ -98,7 +95,7 @@ Make it a challenge to guess if you are an AI.
98
  * Do not include the users' messages in your response. That reveals you are an AI.
99
  """
100
 
101
- llm_handler = OpenAILLMHandler()
102
 
103
  def generate_response(user_message: str, conversation_history: list) -> str:
104
  current_time, now = get_current_local_time()
@@ -112,19 +109,10 @@ def generate_response(user_message: str, conversation_history: list) -> str:
112
  prompt_parts.append(f"User: {user_message}\nAssistant:")
113
  prompt = "\n\n".join(prompt_parts)
114
 
115
- return llm_handler.generate_response(prompt)
116
 
117
- def chatbot_interface(user_message: str, history: list) -> list:
118
- if not user_message.strip():
119
- return history
120
-
121
- if not llm_handler.model:
122
- history.append(("System", "Error: AI service is unavailable."))
123
- return history
124
-
125
- ai_response = generate_response(user_message, history)
126
- history.append((user_message, ai_response))
127
- return history
128
 
129
  # Enhanced Gradio UI with improved CSS and layout
130
  custom_css = """
@@ -135,6 +123,9 @@ body, .gradio-container {
135
  background-color: #f0f2f5;
136
  padding: 20px;
137
  width: 100%;
 
 
 
138
  }
139
 
140
  #chatbot {
@@ -143,6 +134,8 @@ body, .gradio-container {
143
  padding: 15px;
144
  font-size: 16px;
145
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
 
 
146
  }
147
 
148
  .message {
@@ -195,39 +188,33 @@ with gr.Blocks(css=custom_css) as demo:
195
  gr.Markdown("<h1 style='text-align: center; color: #0d6efd;'>Human.</h1>")
196
 
197
  with gr.Row():
198
- # Removed the "Call Human" button
199
- model_status = gr.Textbox(
200
- label="Human Arrival Status",
201
- value="", # Empty since the button is removed
202
- interactive=False,
203
- elem_id="model-status"
204
  )
205
 
206
  with gr.Row():
207
- with gr.Column(scale=1):
208
- chatbot = gr.Chatbot(
209
- label="HUMANCHAT",
210
- elem_id="chatbot",
211
- )
212
- with gr.Column(scale=1):
213
- with gr.Row():
214
- msg = gr.Textbox(
215
- placeholder="Type your message here...",
216
- show_label=False,
217
- container=False,
218
- elem_id="textbox"
219
- )
220
- send = gr.Button("➤", elem_id="send-button")
221
 
222
  def update_chat(user_message, history):
223
  if not user_message.strip():
224
  return history, gr.update(value="")
225
- if not llm_handler.model:
226
  history.append(("System", "Error: AI service is unavailable."))
227
  return history, gr.update(value="")
228
 
229
- updated_history = chatbot_interface(user_message, history)
230
- return updated_history, gr.update(value="")
 
231
 
232
  # Event handlers
233
  send.click(
@@ -243,7 +230,7 @@ with gr.Blocks(css=custom_css) as demo:
243
  )
244
 
245
  if __name__ == "__main__":
246
- if client:
247
  demo.launch(share=True)
248
  else:
249
  logger.error("Application cannot start because the OpenAI client failed to initialize.")
 
4
  import datetime
5
  import pytz
6
  import logging
7
+ from openai.error import OpenAIError
8
  from threading import Lock
9
 
10
  # Configure logging
 
17
  # Set up OpenAI authentication
18
  def setup_openai_auth():
19
  api_key = os.environ.get("LLM_API_KEY")
20
+ baseurl = os.environ.get("LLM_API_BASE_URL", "https://api.openai.com")
21
  if not api_key:
22
  raise Exception("LLM API authentication failed. Please set your LLM_API_KEY environment variable.")
23
+ openai.api_key = api_key
24
+ openai.api_base = baseurl
25
+ return True # Indicate successful setup
26
 
27
  # Initialize OpenAI client
28
  try:
29
+ setup_openai_auth()
30
  logger.info("OpenAI client initialized successfully.")
31
  except Exception as e:
32
  logger.error(f"Failed to initialize OpenAI client: {e}")
33
+ openai_client_available = False
34
+ else:
35
+ openai_client_available = True
36
 
37
  class OpenAILLMHandler:
38
  def __init__(self):
39
  self.model = os.environ.get("MODEL_NAME", "gpt-4")
 
40
  self.generation_lock = Lock()
41
+
 
42
  def generate_response(self, prompt: str, max_length: int = 512) -> str:
 
 
 
 
43
  try:
44
  with self.generation_lock:
45
+ response = openai.ChatCompletion.create(
46
  model=self.model,
47
  messages=[{"role": "user", "content": prompt}],
48
  max_tokens=max_length,
 
75
  * Date: {current_date.strftime("%B %d, %Y")}
76
  * Day: {current_date.strftime("%A")}
77
 
78
+ Imagine you are a real human with your own character/will. Your task is to talk in a way the human thinks you are human. Your name is Jack Heoley.
79
  Make it a challenge to guess if you are an AI.
80
  * Do not add comments about changing your style in between messages.
81
  * The user does not know you at all. Do not assume he does.
 
95
  * Do not include the users' messages in your response. That reveals you are an AI.
96
  """
97
 
98
+ llm_handler = OpenAILLMHandler() if openai_client_available else None
99
 
100
  def generate_response(user_message: str, conversation_history: list) -> str:
101
  current_time, now = get_current_local_time()
 
109
  prompt_parts.append(f"User: {user_message}\nAssistant:")
110
  prompt = "\n\n".join(prompt_parts)
111
 
112
+ return llm_handler.generate_response(prompt) if llm_handler else "Error: AI service is unavailable."
113
 
114
+ def chatbot_interface(user_message: str, conversation_history: list) -> str:
115
+ return generate_response(user_message, conversation_history)
 
 
 
 
 
 
 
 
 
116
 
117
  # Enhanced Gradio UI with improved CSS and layout
118
  custom_css = """
 
123
  background-color: #f0f2f5;
124
  padding: 20px;
125
  width: 100%;
126
+ height: 100vh;
127
+ display: flex;
128
+ flex-direction: column;
129
  }
130
 
131
  #chatbot {
 
134
  padding: 15px;
135
  font-size: 16px;
136
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
137
+ overflow-y: auto;
138
+ flex-grow: 1;
139
  }
140
 
141
  .message {
 
188
  gr.Markdown("<h1 style='text-align: center; color: #0d6efd;'>Human.</h1>")
189
 
190
  with gr.Row():
191
+ chatbot = gr.Chatbot(
192
+ label="HUMANCHAT",
193
+ elem_id="chatbot",
 
 
 
194
  )
195
 
196
  with gr.Row():
197
+ with gr.Column(scale=0.9):
198
+ msg = gr.Textbox(
199
+ placeholder="Type your message here...",
200
+ show_label=False,
201
+ container=False,
202
+ elem_id="textbox",
203
+ lines=1 # Single-line textbox
204
+ ).style(container=False)
205
+ with gr.Column(scale=0.1):
206
+ send = gr.Button("➤", elem_id="send-button")
 
 
 
 
207
 
208
  def update_chat(user_message, history):
209
  if not user_message.strip():
210
  return history, gr.update(value="")
211
+ if not llm_handler:
212
  history.append(("System", "Error: AI service is unavailable."))
213
  return history, gr.update(value="")
214
 
215
+ ai_response = chatbot_interface(user_message, history)
216
+ history.append((user_message, ai_response))
217
+ return history, gr.update(value="")
218
 
219
  # Event handlers
220
  send.click(
 
230
  )
231
 
232
  if __name__ == "__main__":
233
+ if openai_client_available:
234
  demo.launch(share=True)
235
  else:
236
  logger.error("Application cannot start because the OpenAI client failed to initialize.")