1- import logging
21import openai
3-
2+ import utils
43import gradio as gr
54
65from urllib .parse import urljoin
76from langchain .schema import HumanMessage , AIMessage , SystemMessage
87from langchain_openai import ChatOpenAI
9- from typing import Dict , List
8+ from typing import Dict
109from pydantic import BaseModel , ConfigDict
11- from utils import LLMParams , load_settings
1210
13- logging .basicConfig ()
14- logger = logging .getLogger (__name__ )
15- logger .setLevel (logging .INFO )
11+ log = utils .get_logger ()
1612
1713
1814class AppSettings (BaseModel ):
@@ -22,7 +18,7 @@ class AppSettings(BaseModel):
2218 model_name : str
2319 model_instruction : str
2420 page_title : str
25- llm_params : LLMParams
21+ llm_params : utils . LLMParams
2622 # Theme customisation
2723 theme_params : Dict [str , str | list ]
2824 theme_params_extended : Dict [str , str ]
@@ -32,8 +28,8 @@ class AppSettings(BaseModel):
3228 model_config = ConfigDict (protected_namespaces = (), extra = "forbid" )
3329
3430
35- settings = AppSettings (** load_settings ())
36- logger .info (settings )
31+ settings = AppSettings (** utils . load_settings ())
32+ log .info (settings )
3733
3834backend_url = str (settings .backend_url )
3935backend_health_endpoint = urljoin (backend_url , "/health" )
@@ -82,7 +78,7 @@ def inference(latest_message, history):
8278 context .append (HumanMessage (content = human ))
8379 context .append (AIMessage (content = (ai or "" )))
8480 context .append (HumanMessage (content = latest_message ))
85- logger .debug ("Chat context: %s" , context )
81+ log .debug ("Chat context: %s" , context )
8682
8783 response = ""
8884 for chunk in llm .stream (context ):
@@ -104,7 +100,7 @@ def inference(latest_message, history):
104100 # https://github.com/openai/openai-python/tree/e8e5a0dc7ccf2db19d7f81991ee0987f9c3ae375?tab=readme-ov-file#handling-errors
105101
106102 except openai .BadRequestError as err :
107- logger .error ("Received BadRequestError from backend API: %s" , err )
103+ log .error ("Received BadRequestError from backend API: %s" , err )
108104 message = err .response .json ()["message" ]
109105 if INCLUDE_SYSTEM_PROMPT :
110106 raise PossibleSystemPromptException ()
@@ -115,12 +111,12 @@ def inference(latest_message, history):
115111
116112 except openai .APIConnectionError as err :
117113 if not BACKEND_INITIALISED :
118- logger .info ("Backend API not yet ready" )
114+ log .info ("Backend API not yet ready" )
119115 gr .Info (
120116 "Backend not ready - model may still be initialising - please try again later."
121117 )
122118 else :
123- logger .error ("Failed to connect to backend API: %s" , err )
119+ log .error ("Failed to connect to backend API: %s" , err )
124120 gr .Warning ("Failed to connect to backend API." )
125121
126122 except openai .InternalServerError as err :
@@ -130,7 +126,7 @@ def inference(latest_message, history):
130126
131127 # Catch-all for unexpected exceptions
132128 except Exception as err :
133- logger .error ("Unexpected error during inference: %s" , err )
129+ log .error ("Unexpected error during inference: %s" , err )
134130 raise gr .Error ("Unexpected error encountered - see logs for details." )
135131
136132
@@ -150,7 +146,7 @@ def inference_wrapper(*args):
150146 for chunk in inference (* args ):
151147 yield chunk
152148 except PossibleSystemPromptException :
153- logger .warning ("Disabling system prompt and retrying previous request" )
149+ log .warning ("Disabling system prompt and retrying previous request" )
154150 INCLUDE_SYSTEM_PROMPT = False
155151 for chunk in inference (* args ):
156152 yield chunk
@@ -179,7 +175,7 @@ def inference_wrapper(*args):
179175 css = settings .css_overrides ,
180176 js = settings .custom_javascript ,
181177)
182- logger .debug ("Gradio chat interface config: %s" , app .config )
178+ log .debug ("Gradio chat interface config: %s" , app .config )
183179app .queue (
184180 default_concurrency_limit = 10 ,
185181).launch (server_name = settings .host_address )
0 commit comments