thebendu commited on
Commit
be09774
·
1 Parent(s): 2dc92ec
Dockerfile CHANGED
@@ -30,7 +30,10 @@ COPY ./commons commons
30
  COPY ./media media
31
  COPY ./scripts scripts
32
  COPY ./system_prompts system_prompts
 
33
 
34
  COPY ./bot.py bot.py
35
 
36
  EXPOSE 8080
 
 
 
30
  COPY ./media media
31
  COPY ./scripts scripts
32
  COPY ./system_prompts system_prompts
33
+ COPY ./resources resources
34
 
35
  COPY ./bot.py bot.py
36
 
37
  EXPOSE 8080
38
+
39
+ CMD ["python3", "bot.py"]
bot.py CHANGED
@@ -1,6 +1,11 @@
 
 
 
 
 
1
 
2
  if __name__ == '__main__':
3
- print("Starting gradio ui application ...")
4
  from scripts.bots.gradio_bot import GradioBot
5
  gradio_bot = GradioBot()
6
  gradio_bot.start()
 
1
+ from commons.loggerfactory import LoggerFactory
2
+ from commons.utils import resource_path
3
+
4
+ LoggerFactory(resource_path("logging.ini"))
5
+ logger = LoggerFactory.getLogger("main")
6
 
7
  if __name__ == '__main__':
8
+ logger.info("Starting gradio ui application ...")
9
  from scripts.bots.gradio_bot import GradioBot
10
  gradio_bot = GradioBot()
11
  gradio_bot.start()
commons/loggerfactory/__init__.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import logging.config
3
+ from logging import Logger
4
+ from pathlib import Path
5
+
6
+
7
+ class LoggerFactory:
8
+ def __init__(self, file: str | Path):
9
+ """
10
+ Logger factory to simplify
11
+ :param file: Logger config file
12
+ """
13
+ logging.config.fileConfig(file)
14
+
15
+ @staticmethod
16
+ def getLogger(name: str) -> Logger:
17
+ """
18
+ A static method to get logger
19
+ :param name: Name of the logger
20
+ @return: Logger
21
+ """
22
+ return logging.getLogger(name)
commons/utils.py CHANGED
@@ -13,3 +13,8 @@ def prompt_path(file_name: str):
13
 
14
  def media_path(file_name: str):
15
  return DEFAULT_DIR_PATH / "media" / file_name
 
 
 
 
 
 
13
 
14
  def media_path(file_name: str):
15
  return DEFAULT_DIR_PATH / "media" / file_name
16
+
17
+
18
+ def resource_path(file_name: str):
19
+ return DEFAULT_DIR_PATH / "resources" / file_name
20
+
compose.yaml CHANGED
@@ -3,6 +3,5 @@ services:
3
  build: .
4
  ports:
5
  - "8080:8080"
6
- entrypoint: ["python3", "bot.py"]
7
  environment:
8
  - OPENAI_API_KEY
 
3
  build: .
4
  ports:
5
  - "8080:8080"
 
6
  environment:
7
  - OPENAI_API_KEY
resources/logging.ini ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [loggers]
2
+ keys=root,simpleExample,httpx
3
+
4
+ [handlers]
5
+ keys=consoleHandler
6
+
7
+ [formatters]
8
+ keys=simpleFormatter
9
+
10
+ [logger_root]
11
+ level=INFO
12
+ handlers=consoleHandler
13
+
14
+ [logger_simpleExample]
15
+ level=INFO
16
+ handlers=consoleHandler
17
+ qualname=simpleExample
18
+ propagate=0
19
+
20
+ [logger_httpx]
21
+ level=ERROR
22
+ handlers=consoleHandler
23
+ qualname=httpx
24
+ propagate=0
25
+
26
+ [handler_consoleHandler]
27
+ class=StreamHandler
28
+ level=INFO
29
+ formatter=simpleFormatter
30
+ args=(sys.stdout,)
31
+
32
+ [formatter_simpleFormatter]
33
+ format=%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)s
scripts/bots/gradio_bot.py CHANGED
@@ -1,5 +1,6 @@
1
  from openai import OpenAI
2
 
 
3
  from commons.utils import prompt_path, media_path
4
  from scripts.components import OpenAIBot
5
  from scripts.gradio_ui import GradioBotUI
@@ -17,6 +18,9 @@ class GradioBot:
17
 
18
  gradio_bot = GradioBotUI(openai_bot, user_avatar=media_path("user.png"), agent_avatar=media_path("agent.png"))
19
 
 
 
 
20
  def start(self):
21
- print("Starting gradio bot ...")
22
  self.gradio_bot.launch()
 
1
  from openai import OpenAI
2
 
3
+ from commons.loggerfactory import LoggerFactory
4
  from commons.utils import prompt_path, media_path
5
  from scripts.components import OpenAIBot
6
  from scripts.gradio_ui import GradioBotUI
 
18
 
19
  gradio_bot = GradioBotUI(openai_bot, user_avatar=media_path("user.png"), agent_avatar=media_path("agent.png"))
20
 
21
+ def __init__(self):
22
+ self.logger = LoggerFactory.getLogger(self.__class__.__name__)
23
+
24
  def start(self):
25
+ self.logger.info("Starting gradio bot ...")
26
  self.gradio_bot.launch()
scripts/components.py CHANGED
@@ -4,6 +4,7 @@ from os import PathLike
4
 
5
  from openai import OpenAI
6
 
 
7
  from commons.utils import getdefault
8
  from scripts.io import InputOutput
9
 
@@ -57,6 +58,7 @@ class ChatHistory:
57
 
58
  class OpenAIBot:
59
  def __init__(self, bot: OpenAI, model: str, prompt: PathLike | str, context_span: int, **args):
 
60
  self.__bot = bot
61
  self.__model = model
62
  final_prompt = prompt
@@ -83,8 +85,8 @@ class OpenAIBot:
83
  messages = history.add_message(Role.USER, user_input).get_whole_context()
84
 
85
  chat = self.__bot.chat.completions.create(model=self.__model, messages=messages)
86
- # print(f"Tokens count, prompts: {chat.usage.prompt_tokens}, completion: {chat.usage.completion_tokens}, "
87
- # f"total: {chat.usage.total_tokens}")
88
  # del messages
89
 
90
  reply = chat.choices[0].message.content
 
4
 
5
  from openai import OpenAI
6
 
7
+ from commons.loggerfactory import LoggerFactory
8
  from commons.utils import getdefault
9
  from scripts.io import InputOutput
10
 
 
58
 
59
  class OpenAIBot:
60
  def __init__(self, bot: OpenAI, model: str, prompt: PathLike | str, context_span: int, **args):
61
+ self.logger = LoggerFactory.getLogger(self.__class__.__name__)
62
  self.__bot = bot
63
  self.__model = model
64
  final_prompt = prompt
 
85
  messages = history.add_message(Role.USER, user_input).get_whole_context()
86
 
87
  chat = self.__bot.chat.completions.create(model=self.__model, messages=messages)
88
+ self.logger.info("Tokens count, prompts: %s, completion: %s, total: %s",
89
+ chat.usage.prompt_tokens, chat.usage.completion_tokens, chat.usage.total_tokens)
90
  # del messages
91
 
92
  reply = chat.choices[0].message.content
scripts/gradio_ui.py CHANGED
@@ -2,17 +2,19 @@ from pathlib import Path
2
 
3
  import gradio as gr
4
 
 
5
  from scripts.components import OpenAIBot, DEFAULT_WELCOME_MESSAGE, DEFAULT_EXIT_MESSAGE, ChatHistory
6
 
7
 
8
  class GradioBotUI:
9
  def __init__(self, bot: OpenAIBot, user_avatar: str | Path, agent_avatar: str | Path):
 
10
  self.bot = bot
11
  self.user_avatar = user_avatar
12
  self.agent_avatar = agent_avatar
13
 
14
  def launch(self):
15
- print("Launching gradio ui ...")
16
  with gr.Blocks() as demo:
17
  with gr.Row():
18
  gr.Markdown(
@@ -37,7 +39,7 @@ class GradioBotUI:
37
  user_step.then(self.agent_message, inputs=[state, chatbot], outputs=[prompt, state, chatbot])
38
 
39
  demo.launch(server_port=8080, server_name="0.0.0.0")
40
- print("Done launching gradio ui!")
41
 
42
  def user_message(self, user_text, history):
43
  return gr.Textbox(label="Prompt"), history + [[user_text, None]]
 
2
 
3
  import gradio as gr
4
 
5
+ from commons.loggerfactory import LoggerFactory
6
  from scripts.components import OpenAIBot, DEFAULT_WELCOME_MESSAGE, DEFAULT_EXIT_MESSAGE, ChatHistory
7
 
8
 
9
  class GradioBotUI:
10
  def __init__(self, bot: OpenAIBot, user_avatar: str | Path, agent_avatar: str | Path):
11
+ self.logger = LoggerFactory.getLogger(self.__class__.__name__)
12
  self.bot = bot
13
  self.user_avatar = user_avatar
14
  self.agent_avatar = agent_avatar
15
 
16
  def launch(self):
17
+ self.logger.info("Launching gradio ui ...")
18
  with gr.Blocks() as demo:
19
  with gr.Row():
20
  gr.Markdown(
 
39
  user_step.then(self.agent_message, inputs=[state, chatbot], outputs=[prompt, state, chatbot])
40
 
41
  demo.launch(server_port=8080, server_name="0.0.0.0")
42
+ self.logger.info("Done launching gradio ui!")
43
 
44
  def user_message(self, user_text, history):
45
  return gr.Textbox(label="Prompt"), history + [[user_text, None]]