Skip to content

Commit 20db96b

Browse files
authored
Merge pull request #53 from ks6088ts-labs/feature/issue-52_dall-e-example
add create image example
2 parents 70e614b + 9fa69a6 commit 20db96b

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ AZURE_OPENAI_API_VERSION="2024-06-01"
55
AZURE_OPENAI_GPT_MODEL="gpt-4o"
66
AZURE_OPENAI_STT_MODEL="whisper"
77
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-large"
8+
AZURE_OPENAI_DALLE_MODEL="dall-e-3"
89

910
# Azure Cosmos DB
1011
AZURE_COSMOS_DB_CONNECTION_STRING="AccountEndpoint=https://<YOUR_COSMOSDB_NAME>.documents.azure.com:443/;AccountKey=<ACCOUNT_KEY>;"

apps/99_streamlit_examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Access to http://localhost:8501 and select the sample you want to run from the s
5757

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

60+
#### 7. Create image
61+
62+
![Create image](../../docs/images/99_streamlit_examples.createimage.png)
63+
6064
## References
6165

6266
- [🎈 Streamlit + LLM Examples App](https://github.com/streamlit/llm-examples)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import json
2+
from os import getenv
3+
4+
import streamlit as st
5+
from dotenv import load_dotenv
6+
from openai import AzureOpenAI
7+
8+
load_dotenv()
9+
10+
with st.sidebar:
11+
azure_openai_endpoint = st.text_input(
12+
label="AZURE_OPENAI_ENDPOINT",
13+
value=getenv("AZURE_OPENAI_ENDPOINT"),
14+
key="AZURE_OPENAI_ENDPOINT",
15+
type="default",
16+
)
17+
azure_openai_api_key = st.text_input(
18+
label="AZURE_OPENAI_API_KEY",
19+
key="AZURE_OPENAI_API_KEY",
20+
type="password",
21+
)
22+
azure_openai_api_version = st.text_input(
23+
label="AZURE_OPENAI_API_VERSION",
24+
value=getenv("AZURE_OPENAI_API_VERSION"),
25+
key="AZURE_OPENAI_API_VERSION",
26+
type="default",
27+
)
28+
azure_openai_dalle_model = st.text_input(
29+
label="AZURE_OPENAI_DALLE_MODEL",
30+
value=getenv("AZURE_OPENAI_DALLE_MODEL"),
31+
key="AZURE_OPENAI_DALLE_MODEL",
32+
type="default",
33+
)
34+
"[Azure Portal](https://portal.azure.com/)"
35+
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
36+
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/7_Create_image.py)"
37+
38+
st.title("Create image")
39+
40+
if (
41+
not azure_openai_api_key
42+
or not azure_openai_endpoint
43+
or not azure_openai_api_version
44+
or not azure_openai_dalle_model
45+
):
46+
st.warning("Please fill in the required fields at the sidebar.")
47+
st.stop()
48+
49+
st.info("Create an image from a text description.")
50+
51+
description = st.text_input(
52+
"Describe the image",
53+
placeholder="Please describe the content of the image",
54+
)
55+
56+
if description:
57+
client = AzureOpenAI(
58+
api_key=azure_openai_api_key,
59+
api_version=azure_openai_api_version,
60+
azure_endpoint=azure_openai_endpoint,
61+
)
62+
63+
with st.spinner("Thinking..."):
64+
result = client.images.generate(
65+
model=azure_openai_dalle_model,
66+
prompt=description,
67+
n=1,
68+
)
69+
json_response = json.loads(result.model_dump_json())
70+
71+
st.link_button("Show image", json_response["data"][0]["url"])
72+
st.write(json_response)
266 KB
Loading

infra/main.bicep

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ param aiServicesDisableLocalAuth bool = false
4848
])
4949
param aiServicesPublicNetworkAccess string = 'Enabled'
5050

51+
@description('Specifies the location for the Azure AI Services resource.')
52+
param aiServicesLocation string = 'eastus'
53+
5154
@description('Specifies the OpenAI deployments to create.')
5255
param openAiDeployments array = []
5356

@@ -81,7 +84,7 @@ module aiServices 'modules/aiServices.bicep' = {
8184
params: {
8285
// properties
8386
name: empty(aiServicesName) ? toLower('${prefix}-ai-services') : aiServicesName
84-
location: location
87+
location: aiServicesLocation
8588
tags: tags
8689
sku: aiServicesSku
8790
identity: aiServicesIdentity

0 commit comments

Comments
 (0)