|
| 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) |
0 commit comments