Skip to content

Commit

Permalink
Merge pull request #53 from ks6088ts-labs/feature/issue-52_dall-e-exa…
Browse files Browse the repository at this point in the history
…mple

add create image example
  • Loading branch information
ks6088ts authored Aug 12, 2024
2 parents 70e614b + 9fa69a6 commit 20db96b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ AZURE_OPENAI_API_VERSION="2024-06-01"
AZURE_OPENAI_GPT_MODEL="gpt-4o"
AZURE_OPENAI_STT_MODEL="whisper"
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-large"
AZURE_OPENAI_DALLE_MODEL="dall-e-3"

# Azure Cosmos DB
AZURE_COSMOS_DB_CONNECTION_STRING="AccountEndpoint=https://<YOUR_COSMOSDB_NAME>.documents.azure.com:443/;AccountKey=<ACCOUNT_KEY>;"
Expand Down
4 changes: 4 additions & 0 deletions apps/99_streamlit_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Access to http://localhost:8501 and select the sample you want to run from the s

![Speech to Text](../../docs/images/99_streamlit_examples.stt.png)

#### 7. Create image

![Create image](../../docs/images/99_streamlit_examples.createimage.png)

## References

- [🎈 Streamlit + LLM Examples App](https://github.com/streamlit/llm-examples)
Expand Down
72 changes: 72 additions & 0 deletions apps/99_streamlit_examples/pages/7_Create_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json
from os import getenv

import streamlit as st
from dotenv import load_dotenv
from openai import AzureOpenAI

load_dotenv()

with st.sidebar:
azure_openai_endpoint = st.text_input(
label="AZURE_OPENAI_ENDPOINT",
value=getenv("AZURE_OPENAI_ENDPOINT"),
key="AZURE_OPENAI_ENDPOINT",
type="default",
)
azure_openai_api_key = st.text_input(
label="AZURE_OPENAI_API_KEY",
key="AZURE_OPENAI_API_KEY",
type="password",
)
azure_openai_api_version = st.text_input(
label="AZURE_OPENAI_API_VERSION",
value=getenv("AZURE_OPENAI_API_VERSION"),
key="AZURE_OPENAI_API_VERSION",
type="default",
)
azure_openai_dalle_model = st.text_input(
label="AZURE_OPENAI_DALLE_MODEL",
value=getenv("AZURE_OPENAI_DALLE_MODEL"),
key="AZURE_OPENAI_DALLE_MODEL",
type="default",
)
"[Azure Portal](https://portal.azure.com/)"
"[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/7_Create_image.py)"

st.title("Create image")

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

st.info("Create an image from a text description.")

description = st.text_input(
"Describe the image",
placeholder="Please describe the content of the image",
)

if description:
client = AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
azure_endpoint=azure_openai_endpoint,
)

with st.spinner("Thinking..."):
result = client.images.generate(
model=azure_openai_dalle_model,
prompt=description,
n=1,
)
json_response = json.loads(result.model_dump_json())

st.link_button("Show image", json_response["data"][0]["url"])
st.write(json_response)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ param aiServicesDisableLocalAuth bool = false
])
param aiServicesPublicNetworkAccess string = 'Enabled'

@description('Specifies the location for the Azure AI Services resource.')
param aiServicesLocation string = 'eastus'

@description('Specifies the OpenAI deployments to create.')
param openAiDeployments array = []

Expand Down Expand Up @@ -81,7 +84,7 @@ module aiServices 'modules/aiServices.bicep' = {
params: {
// properties
name: empty(aiServicesName) ? toLower('${prefix}-ai-services') : aiServicesName
location: location
location: aiServicesLocation
tags: tags
sku: aiServicesSku
identity: aiServicesIdentity
Expand Down

0 comments on commit 20db96b

Please sign in to comment.