Skip to content

Commit 31f64ac

Browse files
author
sd109
committed
Enable gradio hot reload and standardise formatting
1 parent ec6f4d1 commit 31f64ac

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

chart/web-app/app.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from langchain.schema import HumanMessage, AIMessage, SystemMessage
1010
from langchain_openai import ChatOpenAI
1111

12-
1312
settings = AppSettings.load("./settings.yml")
1413
print("App settings:")
1514
rich.print(settings)
@@ -26,13 +25,15 @@
2625
# mistral model is specified using this regex and then handle it explicitly
2726
# when contructing the `context` list in the `inference` function below.
2827
MISTRAL_REGEX = re.compile(r".*mi(s|x)tral.*", re.IGNORECASE)
29-
IS_MISTRAL_MODEL = (MISTRAL_REGEX.match(settings.model_name) is not None)
28+
IS_MISTRAL_MODEL = MISTRAL_REGEX.match(settings.model_name) is not None
3029
if IS_MISTRAL_MODEL:
31-
print("Detected Mistral model - will alter LangChain conversation format appropriately.")
30+
print(
31+
"Detected Mistral model - will alter LangChain conversation format appropriately."
32+
)
3233

3334
llm = ChatOpenAI(
3435
base_url=urljoin(backend_url, "v1"),
35-
model = settings.model_name,
36+
model=settings.model_name,
3637
openai_api_key="required-but-not-used",
3738
temperature=settings.llm_temperature,
3839
max_tokens=settings.llm_max_tokens,
@@ -44,8 +45,8 @@
4445
streaming=True,
4546
)
4647

47-
def inference(latest_message, history):
4848

49+
def inference(latest_message, history):
4950
# Check backend health and warn the user on error
5051
try:
5152
response = requests.get(backend_health_endpoint, timeout=5)
@@ -68,7 +69,6 @@ def inference(latest_message, history):
6869
# In this case backend is probably still busy downloading model weights
6970
raise gr.Error("Backend not ready yet - please try again later")
7071

71-
7272
try:
7373
# To handle Mistral models we have to add the model instruction to
7474
# the first user message since Mistral requires user -> ai -> user
@@ -78,7 +78,9 @@ def inference(latest_message, history):
7878
context.append(SystemMessage(content=settings.model_instruction))
7979
for i, (human, ai) in enumerate(history):
8080
if IS_MISTRAL_MODEL and i == 0:
81-
context.append(HumanMessage(content=f"{settings.model_instruction}\n\n{human}"))
81+
context.append(
82+
HumanMessage(content=f"{settings.model_instruction}\n\n{human}")
83+
)
8284
else:
8385
context.append(HumanMessage(content=human))
8486
context.append(AIMessage(content=ai))
@@ -98,7 +100,9 @@ def inference(latest_message, history):
98100
# For all other errors notify user and log a more detailed warning
99101
except Exception as err:
100102
warnings.warn(f"Exception encountered while generating response: {err}")
101-
raise gr.Error("Unknown error encountered - see application logs for more information.")
103+
raise gr.Error(
104+
"Unknown error encountered - see application logs for more information."
105+
)
102106

103107

104108
# UI colour theming
@@ -119,7 +123,7 @@ def inference(latest_message, history):
119123

120124

121125
# Build main chat interface
122-
gr.ChatInterface(
126+
with gr.ChatInterface(
123127
inference,
124128
chatbot=gr.Chatbot(
125129
# Height of conversation window in CSS units (string) or pixels (int)
@@ -139,4 +143,6 @@ def inference(latest_message, history):
139143
analytics_enabled=False,
140144
theme=theme,
141145
css=css_overrides,
142-
).queue().launch(server_name="0.0.0.0")
146+
) as app:
147+
# app.launch(server_name="0.0.0.0")
148+
app.launch()

chart/web-app/config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class AppSettings(BaseSettings):
3333
default_factory=lambda: f"http://llm-backend.{get_k8s_namespace()}.svc"
3434
)
3535
page_title: str = Field(default="Large Language Model")
36-
model_instruction: str = Field(default="You are a helpful and cheerful AI assistant. Please respond appropriately.")
36+
model_instruction: str = Field(
37+
default="You are a helpful and cheerful AI assistant. Please respond appropriately."
38+
)
3739

3840
# Model settings
3941
# See https://platform.openai.com/docs/api-reference/chat/create for available parameters
@@ -43,9 +45,8 @@ class AppSettings(BaseSettings):
4345
llm_presence_penalty: float = Field(default=0, ge=-2, le=2)
4446
llm_frequency_penalty: float = Field(default=0, ge=-2, le=2)
4547

46-
4748
# UI theming
48-
49+
4950
# Variables explicitly passed to gradio.theme.Default()
5051
# For example:
5152
# {"primary_hue": "red"}

0 commit comments

Comments
 (0)