Skip to content

Commit 7c1f09c

Browse files
committed
refactor UI
1 parent c017cdf commit 7c1f09c

File tree

14 files changed

+155
-77
lines changed

14 files changed

+155
-77
lines changed

apps/2_streamlit_chat/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3535
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/2_streamlit_chat/main.py)"
3636

37+
38+
def is_configured():
39+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
40+
41+
3742
st.title("2_streamlit_chat")
3843

39-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
44+
if not is_configured():
4045
st.warning("Please fill in the required fields at the sidebar.")
41-
st.stop()
4246

4347
if "messages" not in st.session_state:
4448
st.session_state["messages"] = [
@@ -53,7 +57,7 @@
5357
st.chat_message(msg["role"]).write(msg["content"])
5458

5559
# Receive user input
56-
if prompt := st.chat_input():
60+
if prompt := st.chat_input(disabled=not is_configured()):
5761
client = AzureOpenAI(
5862
api_key=azure_openai_api_key,
5963
api_version=azure_openai_api_version,

apps/4_streamlit_chat_history/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ def store_chat_history(container: ContainerProxy):
6666
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
6767
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/4_streamlit_chat_history/main.py)"
6868

69+
70+
def is_configured():
71+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
72+
73+
6974
st.title("4_streamlit_chat_history")
70-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
75+
76+
if not is_configured():
7177
st.warning("Please fill in the required fields at the sidebar.")
72-
st.stop()
7378

7479
st.write(f"Session ID: {get_session_id()}")
7580

@@ -86,7 +91,7 @@ def store_chat_history(container: ContainerProxy):
8691
st.chat_message(msg["role"]).write(msg["content"])
8792

8893
# Receive user input
89-
if prompt := st.chat_input():
94+
if prompt := st.chat_input(disabled=not is_configured()):
9095
client = AzureOpenAI(
9196
api_key=azure_openai_api_key,
9297
api_version=azure_openai_api_version,

apps/7_streamlit_chat_rag/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@
5757
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
5858
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/7_streamlit_chat_rag/main.py)"
5959

60-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
60+
61+
def is_configured():
62+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
63+
64+
65+
if not is_configured():
6166
st.warning("Please fill in the required fields at the sidebar.")
62-
st.stop()
6367

6468

6569
def get_session_id():
@@ -121,7 +125,7 @@ def main():
121125
for msg in st.session_state["memory"].chat_memory.messages:
122126
st.chat_message(msg.type).write(msg.content)
123127

124-
if prompt := st.chat_input(placeholder="Type your message here..."):
128+
if prompt := st.chat_input(placeholder="Type your message here...", disabled=not is_configured()):
125129
st.chat_message("user").write(prompt)
126130

127131
with st.chat_message("assistant"):

apps/8_streamlit_azure_openai_batch/main.py

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,44 @@
3636
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3737
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/8_streamlit_azure_openai_batch/main.py)"
3838

39+
40+
def is_configured():
41+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
42+
43+
44+
def get_client():
45+
return AzureOpenAI(
46+
api_key=azure_openai_api_key,
47+
api_version=azure_openai_api_version,
48+
azure_endpoint=azure_openai_endpoint,
49+
)
50+
51+
3952
st.title("8_streamlit_azure_openai_batch")
4053

41-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
54+
if not is_configured():
4255
st.warning("Please fill in the required fields at the sidebar.")
43-
st.stop()
4456

4557
# ---------------
4658
# Upload batch file
4759
# ---------------
4860
st.header("Upload batch file")
4961
st.info("Upload a file in JSON lines format (.jsonl)")
50-
client = AzureOpenAI(
51-
api_key=azure_openai_api_key,
52-
api_version=azure_openai_api_version,
53-
azure_endpoint=azure_openai_endpoint,
54-
)
5562
uploaded_file = st.file_uploader("Upload an input file in JSON lines format", type=("jsonl"))
5663
if uploaded_file:
5764
bytes_data = uploaded_file.read()
5865
st.write(bytes_data.decode().split("\n"))
59-
submit_button = st.button("Submit", key="submit")
60-
if submit_button:
66+
if st.button(
67+
"Submit",
68+
key="submit",
69+
disabled=not is_configured(),
70+
):
6171
temp_file_path = "tmp.jsonl"
6272
with open(temp_file_path, "wb") as f:
6373
f.write(bytes_data)
6474
with st.spinner("Uploading..."):
6575
try:
66-
response = client.files.create(
76+
response = get_client().files.create(
6777
# FIXME: hardcoded for now, use uploaded_file
6878
file=open(temp_file_path, "rb"),
6979
purpose="batch",
@@ -83,11 +93,14 @@
8393
key="track_file_id",
8494
help="Enter the file ID to track the file upload status",
8595
)
86-
track_button = st.button("Track")
87-
if track_file_id != "" and track_button:
96+
if st.button(
97+
"Track",
98+
key="track",
99+
disabled=not track_file_id or not is_configured(),
100+
):
88101
with st.spinner("Tracking..."):
89102
try:
90-
response = client.files.retrieve(track_file_id)
103+
response = get_client().files.retrieve(track_file_id)
91104
st.write(response.model_dump())
92105
st.write(f"status: {response.status}")
93106
except Exception as e:
@@ -104,11 +117,14 @@
104117
key="batch_file_id",
105118
help="Enter the file ID to track the file upload status",
106119
)
107-
batch_button = st.button("Create batch job")
108-
if batch_file_id != "" and batch_button:
120+
if st.button(
121+
"Create batch job",
122+
key="create",
123+
disabled=not batch_file_id or not is_configured(),
124+
):
109125
with st.spinner("Creating..."):
110126
try:
111-
response = client.batches.create(
127+
response = get_client().batches.create(
112128
input_file_id=batch_file_id,
113129
endpoint="/chat/completions",
114130
completion_window="24h",
@@ -128,11 +144,14 @@
128144
key="track_batch_job_id",
129145
help="Enter the batch job ID to track the job progress",
130146
)
131-
track_batch_job_button = st.button("Track batch job")
132-
if track_batch_job_id != "" and track_batch_job_button:
147+
if st.button(
148+
"Track batch job",
149+
key="track_batch_job",
150+
disabled=not track_batch_job_id or not is_configured(),
151+
):
133152
with st.spinner("Tracking..."):
134153
try:
135-
response = client.batches.retrieve(track_batch_job_id)
154+
response = get_client().batches.retrieve(track_batch_job_id)
136155
st.write(response.model_dump())
137156
st.write(f"status: {response.status}")
138157
st.write(f"output_file_id: {response.output_file_id}")
@@ -150,11 +169,14 @@
150169
key="retrieve_batch_job_id",
151170
help="Enter the batch job ID to retrieve the output file",
152171
)
153-
retrieve_batch_job_button = st.button("Retrieve batch job output file")
154-
if output_file_id != "" and retrieve_batch_job_button:
172+
if st.button(
173+
"Retrieve batch job output file",
174+
key="retrieve_batch_job",
175+
disabled=not output_file_id or not is_configured(),
176+
):
155177
with st.spinner("Retrieving..."):
156178
try:
157-
file_response = client.files.content(output_file_id)
179+
file_response = get_client().files.content(output_file_id)
158180
raw_responses = file_response.text.strip().split("\n")
159181

160182
for raw_response in raw_responses:

apps/99_streamlit_examples/pages/1_File_Q&A.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3535
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/1_File_Q&A.py)"
3636

37+
38+
def is_configured():
39+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
40+
41+
3742
st.title("File Q&A")
3843

39-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
44+
if not is_configured():
4045
st.warning("Please fill in the required fields at the sidebar.")
41-
st.stop()
4246

4347
st.info("Upload a file and ask a question. AI will answer the question.")
4448

@@ -49,7 +53,7 @@
4953
disabled=not uploaded_file,
5054
)
5155

52-
if uploaded_file and question:
56+
if uploaded_file and question and is_configured():
5357
article = uploaded_file.read().decode()
5458

5559
client = AzureOpenAI(

apps/99_streamlit_examples/pages/2_Image_Q&A.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3636
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/2_Image_Q&A.py)"
3737

38+
39+
def is_configured():
40+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
41+
42+
3843
st.title("Image Q&A")
3944

40-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
45+
if not is_configured():
4146
st.warning("Please fill in the required fields at the sidebar.")
42-
st.stop()
4347

4448
st.info("Upload an image and ask a question. AI will answer the question.")
4549

@@ -60,7 +64,7 @@
6064
disabled=not uploaded_file,
6165
)
6266

63-
if uploaded_file and question:
67+
if uploaded_file and question and is_configured():
6468
encoded_image = base64.b64encode(uploaded_file.read()).decode()
6569

6670
client = AzureOpenAI(

apps/99_streamlit_examples/pages/3_Camera_Q&A.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3636
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/3_Camera_Q&A.py)"
3737

38+
39+
def is_configured():
40+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
41+
42+
3843
st.title("Camera Q&A")
3944

40-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
45+
if not is_configured():
4146
st.warning("Please fill in the required fields at the sidebar.")
42-
st.stop()
4347

4448
st.info("Take a picture and ask a question. AI will answer the question.")
4549

@@ -50,7 +54,7 @@
5054
disabled=not img_file_buffer,
5155
)
5256

53-
if img_file_buffer and question:
57+
if img_file_buffer and question and is_configured():
5458
encoded_image = base64.b64encode(img_file_buffer.getvalue()).decode()
5559

5660
client = AzureOpenAI(

apps/99_streamlit_examples/pages/4_Translate_text.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3535
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/4_Translate_text.py)"
3636

37+
38+
def is_configured():
39+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
40+
41+
3742
st.title("Translate text")
3843

39-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
44+
if not is_configured():
4045
st.warning("Please fill in the required fields at the sidebar.")
41-
st.stop()
4246

4347
st.info("This is a sample to translate text.")
4448

@@ -89,7 +93,7 @@ def translate(target: str, input: str) -> str:
8993
)
9094

9195
with row1_right:
92-
translate_button = st.button("Translate")
96+
translate_button = st.button("Translate", disabled=not is_configured())
9397

9498
# 2nd row
9599
row2_left, row2_right = st.columns(2)

apps/99_streamlit_examples/pages/5_Explain_data.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@
3838
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3939
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/5_Explain_data.py)"
4040

41+
42+
def is_configured():
43+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
44+
45+
4146
st.title("Explain data")
4247

43-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
48+
if not is_configured():
4449
st.warning("Please fill in the required fields at the sidebar.")
45-
st.stop()
4650

4751
st.info("This is a sample to explain data.")
4852

@@ -104,7 +108,7 @@ def explain_data(input: str) -> str:
104108
use_container_width=True,
105109
)
106110

107-
explain_button = st.button("Explain data")
111+
explain_button = st.button("Explain data", disabled=not is_configured())
108112

109113
if explain_button:
110114
with st.spinner("Numerical data analysis..."):

apps/99_streamlit_examples/pages/6_Speech_to_text.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
3636
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/6_Speech_to_text.py)"
3737

38+
39+
def is_configured():
40+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_stt_model
41+
42+
3843
st.title("Speech to text")
3944

40-
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_stt_model:
45+
if not is_configured():
4146
st.warning("Please fill in the required fields at the sidebar.")
42-
st.stop()
4347

4448
st.info("This is a sample to convert speech to text.")
4549

@@ -64,17 +68,17 @@
6468
azure_endpoint=azure_openai_endpoint,
6569
)
6670

67-
if st.button("Convert"):
68-
with st.spinner("Converting..."):
69-
response = client.audio.transcriptions.create(
70-
model=azure_openai_stt_model,
71-
file=uploaded_file,
72-
response_format="text",
73-
)
74-
st.write(response)
75-
transcript_encoded = base64.b64encode(response.encode()).decode()
76-
# Generate a link to download the result
77-
st.markdown(
78-
f'<a href="data:file/txt;base64,{transcript_encoded}" download="transcript.txt">Download Result</a>',
79-
unsafe_allow_html=True,
71+
if st.button("Convert", disabled=not uploaded_file or not is_configured()):
72+
with st.spinner("Converting..."):
73+
response = client.audio.transcriptions.create(
74+
model=azure_openai_stt_model,
75+
file=uploaded_file,
76+
response_format="text",
8077
)
78+
st.write(response)
79+
transcript_encoded = base64.b64encode(response.encode()).decode()
80+
# Generate a link to download the result
81+
st.markdown(
82+
f'<a href="data:file/txt;base64,{transcript_encoded}" download="transcript.txt">Download Result</a>',
83+
unsafe_allow_html=True,
84+
)

0 commit comments

Comments
 (0)