Skip to content

Commit 0b95894

Browse files
authored
Merge pull request #51 from ks6088ts-labs/feature/issue-50_camera-input
add camera Q&A example
2 parents 544cfef + ff0bc82 commit 0b95894

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

apps/99_streamlit_examples/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ Access to http://localhost:8501 and select the sample you want to run from the s
3737

3838
![File Q&A](../../docs/images/99_streamlit_examples.fileqa.png)
3939

40-
#### 2. Speech to Text
40+
#### 2. Image Q&A
4141

42-
![Speech to Text](../../docs/images/99_streamlit_examples.stt.png)
42+
![Image Q&A](../../docs/images/99_streamlit_examples.imageqa.png)
4343

44-
#### 3. Image Q&A
44+
#### 3. Camera Q&A
4545

46-
![Image Q&A](../../docs/images/99_streamlit_examples.imageqa.png)
46+
![Camera Q&A](../../docs/images/99_streamlit_examples.cameraqa.png)
4747

4848
#### 4. Translate text
4949

@@ -53,6 +53,10 @@ Access to http://localhost:8501 and select the sample you want to run from the s
5353

5454
![Explain data](../../docs/images/99_streamlit_examples.explaindata.png)
5555

56+
#### 6. Speech to Text
57+
58+
![Speech to Text](../../docs/images/99_streamlit_examples.stt.png)
59+
5660
## References
5761

5862
- [🎈 Streamlit + LLM Examples App](https://github.com/streamlit/llm-examples)

apps/99_streamlit_examples/pages/3_Image_Q&A.py renamed to apps/99_streamlit_examples/pages/2_Image_Q&A.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434
"[Azure Portal](https://portal.azure.com/)"
3535
"[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/3_Image_Q&A.py)"
36+
"[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

3838
st.title("Image Q&A")
3939

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import base64
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_gpt_model = st.text_input(
29+
label="AZURE_OPENAI_GPT_MODEL",
30+
value=getenv("AZURE_OPENAI_GPT_MODEL"),
31+
key="AZURE_OPENAI_GPT_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/3_Camera_Q&A.py)"
37+
38+
st.title("Camera Q&A")
39+
40+
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model:
41+
st.warning("Please fill in the required fields at the sidebar.")
42+
st.stop()
43+
44+
st.info("Take a picture and ask a question. AI will answer the question.")
45+
46+
img_file_buffer = st.camera_input("Take a picture")
47+
question = st.text_input(
48+
"Ask a question about the captured image",
49+
placeholder="Please describe the content of the image",
50+
disabled=not img_file_buffer,
51+
)
52+
53+
if img_file_buffer and question:
54+
encoded_image = base64.b64encode(img_file_buffer.getvalue()).decode()
55+
56+
client = AzureOpenAI(
57+
api_key=azure_openai_api_key,
58+
api_version=azure_openai_api_version,
59+
azure_endpoint=azure_openai_endpoint,
60+
)
61+
62+
print(question)
63+
with st.spinner("Thinking..."):
64+
response = client.chat.completions.create(
65+
model=azure_openai_gpt_model,
66+
messages=[
67+
{
68+
"role": "system",
69+
"content": "You are a professional image analyst. Describe the image.",
70+
},
71+
{
72+
"role": "user",
73+
"content": [
74+
{
75+
"type": "image_url",
76+
"image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"},
77+
},
78+
{
79+
"type": "text",
80+
"content": question,
81+
},
82+
],
83+
},
84+
],
85+
)
86+
msg = response.choices[0].message.content
87+
st.write("### Answer")
88+
st.chat_message("assistant").write(msg)

apps/99_streamlit_examples/pages/2_Speech_to_text.py renamed to apps/99_streamlit_examples/pages/6_Speech_to_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434
"[Azure Portal](https://portal.azure.com/)"
3535
"[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/2_Speech_to_text.py)"
36+
"[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

3838
st.title("Speech to text")
3939

884 KB
Loading

0 commit comments

Comments
 (0)