1
- import logging
2
1
import openai
3
-
2
+ import utils
4
3
import gradio as gr
5
4
6
5
from urllib .parse import urljoin
7
6
from langchain .schema import HumanMessage , AIMessage , SystemMessage
8
7
from langchain_openai import ChatOpenAI
9
- from typing import Dict , List
8
+ from typing import Dict
10
9
from pydantic import BaseModel , ConfigDict
11
- from utils import LLMParams , load_settings
12
10
13
- logging .basicConfig ()
14
- logger = logging .getLogger (__name__ )
15
- logger .setLevel (logging .INFO )
11
+ log = utils .get_logger ()
16
12
17
13
18
14
class AppSettings (BaseModel ):
@@ -22,7 +18,7 @@ class AppSettings(BaseModel):
22
18
model_name : str
23
19
model_instruction : str
24
20
page_title : str
25
- llm_params : LLMParams
21
+ llm_params : utils . LLMParams
26
22
# Theme customisation
27
23
theme_params : Dict [str , str | list ]
28
24
theme_params_extended : Dict [str , str ]
@@ -32,8 +28,8 @@ class AppSettings(BaseModel):
32
28
model_config = ConfigDict (protected_namespaces = (), extra = "forbid" )
33
29
34
30
35
- settings = AppSettings (** load_settings ())
36
- logger .info (settings )
31
+ settings = AppSettings (** utils . load_settings ())
32
+ log .info (settings )
37
33
38
34
backend_url = str (settings .backend_url )
39
35
backend_health_endpoint = urljoin (backend_url , "/health" )
@@ -82,7 +78,7 @@ def inference(latest_message, history):
82
78
context .append (HumanMessage (content = human ))
83
79
context .append (AIMessage (content = (ai or "" )))
84
80
context .append (HumanMessage (content = latest_message ))
85
- logger .debug ("Chat context: %s" , context )
81
+ log .debug ("Chat context: %s" , context )
86
82
87
83
response = ""
88
84
for chunk in llm .stream (context ):
@@ -104,7 +100,7 @@ def inference(latest_message, history):
104
100
# https://github.com/openai/openai-python/tree/e8e5a0dc7ccf2db19d7f81991ee0987f9c3ae375?tab=readme-ov-file#handling-errors
105
101
106
102
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 )
108
104
message = err .response .json ()["message" ]
109
105
if INCLUDE_SYSTEM_PROMPT :
110
106
raise PossibleSystemPromptException ()
@@ -115,12 +111,12 @@ def inference(latest_message, history):
115
111
116
112
except openai .APIConnectionError as err :
117
113
if not BACKEND_INITIALISED :
118
- logger .info ("Backend API not yet ready" )
114
+ log .info ("Backend API not yet ready" )
119
115
gr .Info (
120
116
"Backend not ready - model may still be initialising - please try again later."
121
117
)
122
118
else :
123
- logger .error ("Failed to connect to backend API: %s" , err )
119
+ log .error ("Failed to connect to backend API: %s" , err )
124
120
gr .Warning ("Failed to connect to backend API." )
125
121
126
122
except openai .InternalServerError as err :
@@ -130,7 +126,7 @@ def inference(latest_message, history):
130
126
131
127
# Catch-all for unexpected exceptions
132
128
except Exception as err :
133
- logger .error ("Unexpected error during inference: %s" , err )
129
+ log .error ("Unexpected error during inference: %s" , err )
134
130
raise gr .Error ("Unexpected error encountered - see logs for details." )
135
131
136
132
@@ -150,7 +146,7 @@ def inference_wrapper(*args):
150
146
for chunk in inference (* args ):
151
147
yield chunk
152
148
except PossibleSystemPromptException :
153
- logger .warning ("Disabling system prompt and retrying previous request" )
149
+ log .warning ("Disabling system prompt and retrying previous request" )
154
150
INCLUDE_SYSTEM_PROMPT = False
155
151
for chunk in inference (* args ):
156
152
yield chunk
@@ -179,7 +175,7 @@ def inference_wrapper(*args):
179
175
css = settings .css_overrides ,
180
176
js = settings .custom_javascript ,
181
177
)
182
- logger .debug ("Gradio chat interface config: %s" , app .config )
178
+ log .debug ("Gradio chat interface config: %s" , app .config )
183
179
app .queue (
184
180
default_concurrency_limit = 10 ,
185
181
).launch (server_name = settings .host_address )
0 commit comments