Skip to content

Commit

Permalink
refactor UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Aug 13, 2024
1 parent c017cdf commit 7c1f09c
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 77 deletions.
10 changes: 7 additions & 3 deletions apps/2_streamlit_chat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/2_streamlit_chat/main.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("2_streamlit_chat")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

# Receive user input
if prompt := st.chat_input():
if prompt := st.chat_input(disabled=not is_configured()):
client = AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
Expand Down
11 changes: 8 additions & 3 deletions apps/4_streamlit_chat_history/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ def store_chat_history(container: ContainerProxy):
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/4_streamlit_chat_history/main.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("4_streamlit_chat_history")
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:

if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

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

# Receive user input
if prompt := st.chat_input():
if prompt := st.chat_input(disabled=not is_configured()):
client = AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
Expand Down
10 changes: 7 additions & 3 deletions apps/7_streamlit_chat_rag/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/7_streamlit_chat_rag/main.py)"

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:

def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()


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

if prompt := st.chat_input(placeholder="Type your message here..."):
if prompt := st.chat_input(placeholder="Type your message here...", disabled=not is_configured()):
st.chat_message("user").write(prompt)

with st.chat_message("assistant"):
Expand Down
66 changes: 44 additions & 22 deletions apps/8_streamlit_azure_openai_batch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,44 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/8_streamlit_azure_openai_batch/main.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


def get_client():
return AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
azure_endpoint=azure_openai_endpoint,
)


st.title("8_streamlit_azure_openai_batch")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

# ---------------
# Upload batch file
# ---------------
st.header("Upload batch file")
st.info("Upload a file in JSON lines format (.jsonl)")
client = AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
azure_endpoint=azure_openai_endpoint,
)
uploaded_file = st.file_uploader("Upload an input file in JSON lines format", type=("jsonl"))
if uploaded_file:
bytes_data = uploaded_file.read()
st.write(bytes_data.decode().split("\n"))
submit_button = st.button("Submit", key="submit")
if submit_button:
if st.button(
"Submit",
key="submit",
disabled=not is_configured(),
):
temp_file_path = "tmp.jsonl"
with open(temp_file_path, "wb") as f:
f.write(bytes_data)
with st.spinner("Uploading..."):
try:
response = client.files.create(
response = get_client().files.create(
# FIXME: hardcoded for now, use uploaded_file
file=open(temp_file_path, "rb"),
purpose="batch",
Expand All @@ -83,11 +93,14 @@
key="track_file_id",
help="Enter the file ID to track the file upload status",
)
track_button = st.button("Track")
if track_file_id != "" and track_button:
if st.button(
"Track",
key="track",
disabled=not track_file_id or not is_configured(),
):
with st.spinner("Tracking..."):
try:
response = client.files.retrieve(track_file_id)
response = get_client().files.retrieve(track_file_id)
st.write(response.model_dump())
st.write(f"status: {response.status}")
except Exception as e:
Expand All @@ -104,11 +117,14 @@
key="batch_file_id",
help="Enter the file ID to track the file upload status",
)
batch_button = st.button("Create batch job")
if batch_file_id != "" and batch_button:
if st.button(
"Create batch job",
key="create",
disabled=not batch_file_id or not is_configured(),
):
with st.spinner("Creating..."):
try:
response = client.batches.create(
response = get_client().batches.create(
input_file_id=batch_file_id,
endpoint="/chat/completions",
completion_window="24h",
Expand All @@ -128,11 +144,14 @@
key="track_batch_job_id",
help="Enter the batch job ID to track the job progress",
)
track_batch_job_button = st.button("Track batch job")
if track_batch_job_id != "" and track_batch_job_button:
if st.button(
"Track batch job",
key="track_batch_job",
disabled=not track_batch_job_id or not is_configured(),
):
with st.spinner("Tracking..."):
try:
response = client.batches.retrieve(track_batch_job_id)
response = get_client().batches.retrieve(track_batch_job_id)
st.write(response.model_dump())
st.write(f"status: {response.status}")
st.write(f"output_file_id: {response.output_file_id}")
Expand All @@ -150,11 +169,14 @@
key="retrieve_batch_job_id",
help="Enter the batch job ID to retrieve the output file",
)
retrieve_batch_job_button = st.button("Retrieve batch job output file")
if output_file_id != "" and retrieve_batch_job_button:
if st.button(
"Retrieve batch job output file",
key="retrieve_batch_job",
disabled=not output_file_id or not is_configured(),
):
with st.spinner("Retrieving..."):
try:
file_response = client.files.content(output_file_id)
file_response = get_client().files.content(output_file_id)
raw_responses = file_response.text.strip().split("\n")

for raw_response in raw_responses:
Expand Down
10 changes: 7 additions & 3 deletions apps/99_streamlit_examples/pages/1_File_Q&A.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/1_File_Q&A.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("File Q&A")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand All @@ -49,7 +53,7 @@
disabled=not uploaded_file,
)

if uploaded_file and question:
if uploaded_file and question and is_configured():
article = uploaded_file.read().decode()

client = AzureOpenAI(
Expand Down
10 changes: 7 additions & 3 deletions apps/99_streamlit_examples/pages/2_Image_Q&A.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/2_Image_Q&A.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("Image Q&A")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand All @@ -60,7 +64,7 @@
disabled=not uploaded_file,
)

if uploaded_file and question:
if uploaded_file and question and is_configured():
encoded_image = base64.b64encode(uploaded_file.read()).decode()

client = AzureOpenAI(
Expand Down
10 changes: 7 additions & 3 deletions apps/99_streamlit_examples/pages/3_Camera_Q&A.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/3_Camera_Q&A.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("Camera Q&A")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand All @@ -50,7 +54,7 @@
disabled=not img_file_buffer,
)

if img_file_buffer and question:
if img_file_buffer and question and is_configured():
encoded_image = base64.b64encode(img_file_buffer.getvalue()).decode()

client = AzureOpenAI(
Expand Down
10 changes: 7 additions & 3 deletions apps/99_streamlit_examples/pages/4_Translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/4_Translate_text.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("Translate text")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand Down Expand Up @@ -89,7 +93,7 @@ def translate(target: str, input: str) -> str:
)

with row1_right:
translate_button = st.button("Translate")
translate_button = st.button("Translate", disabled=not is_configured())

# 2nd row
row2_left, row2_right = st.columns(2)
Expand Down
10 changes: 7 additions & 3 deletions apps/99_streamlit_examples/pages/5_Explain_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/5_Explain_data.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model


st.title("Explain data")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand Down Expand Up @@ -104,7 +108,7 @@ def explain_data(input: str) -> str:
use_container_width=True,
)

explain_button = st.button("Explain data")
explain_button = st.button("Explain data", disabled=not is_configured())

if explain_button:
with st.spinner("Numerical data analysis..."):
Expand Down
34 changes: 19 additions & 15 deletions apps/99_streamlit_examples/pages/6_Speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/6_Speech_to_text.py)"


def is_configured():
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_stt_model


st.title("Speech to text")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_stt_model:
if not is_configured():
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

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

Expand All @@ -64,17 +68,17 @@
azure_endpoint=azure_openai_endpoint,
)

if st.button("Convert"):
with st.spinner("Converting..."):
response = client.audio.transcriptions.create(
model=azure_openai_stt_model,
file=uploaded_file,
response_format="text",
)
st.write(response)
transcript_encoded = base64.b64encode(response.encode()).decode()
# Generate a link to download the result
st.markdown(
f'<a href="data:file/txt;base64,{transcript_encoded}" download="transcript.txt">Download Result</a>',
unsafe_allow_html=True,
if st.button("Convert", disabled=not uploaded_file or not is_configured()):
with st.spinner("Converting..."):
response = client.audio.transcriptions.create(
model=azure_openai_stt_model,
file=uploaded_file,
response_format="text",
)
st.write(response)
transcript_encoded = base64.b64encode(response.encode()).decode()
# Generate a link to download the result
st.markdown(
f'<a href="data:file/txt;base64,{transcript_encoded}" download="transcript.txt">Download Result</a>',
unsafe_allow_html=True,
)
Loading

0 comments on commit 7c1f09c

Please sign in to comment.