|
| 1 | +# Author: Ansh |
| 2 | +import streamlit as st |
| 3 | +import oci |
| 4 | +import base64 |
| 5 | +from PIL import Image |
| 6 | + |
| 7 | +# OCI Configuration ( Put your compartment id below) |
| 8 | +compartmentId = "ocid1.compartment.oc1..***************************" |
| 9 | +llm_service_endpoint = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com" |
| 10 | + |
| 11 | +# Define functions |
| 12 | +def encode_image(image_path): |
| 13 | + with open(image_path, "rb") as image_file: |
| 14 | + return base64.b64encode(image_file.read()).decode("utf-8") |
| 15 | + |
| 16 | +def get_message(encoded_image, user_prompt): |
| 17 | + content1 = oci.generative_ai_inference.models.TextContent() |
| 18 | + content1.text = user_prompt |
| 19 | + |
| 20 | + content2 = oci.generative_ai_inference.models.ImageContent() |
| 21 | + image_url = oci.generative_ai_inference.models.ImageUrl() |
| 22 | + image_url.url = f"data:image/jpeg;base64,{encoded_image}" |
| 23 | + content2.image_url = image_url |
| 24 | + |
| 25 | + message = oci.generative_ai_inference.models.UserMessage() |
| 26 | + message.content = [content1, content2] |
| 27 | + return message |
| 28 | + |
| 29 | +def get_chat_request(encoded_image, user_prompt): |
| 30 | + chat_request = oci.generative_ai_inference.models.GenericChatRequest() |
| 31 | + chat_request.messages = [get_message(encoded_image, user_prompt)] |
| 32 | + chat_request.api_format = oci.generative_ai_inference.models.BaseChatRequest.API_FORMAT_GENERIC |
| 33 | + chat_request.num_generations = 1 |
| 34 | + chat_request.is_stream = False |
| 35 | + chat_request.max_tokens = 500 |
| 36 | + chat_request.temperature = 0.75 |
| 37 | + chat_request.top_p = 0.7 |
| 38 | + chat_request.top_k = -1 |
| 39 | + chat_request.frequency_penalty = 1.0 |
| 40 | + return chat_request |
| 41 | + |
| 42 | +def get_chat_detail(chat_request): |
| 43 | + chat_detail = oci.generative_ai_inference.models.ChatDetails() |
| 44 | + chat_detail.serving_mode = oci.generative_ai_inference.models.OnDemandServingMode(model_id="meta.llama-3.2-90b-vision-instruct") |
| 45 | + chat_detail.compartment_id = compartmentId |
| 46 | + chat_detail.chat_request = chat_request |
| 47 | + return chat_detail |
| 48 | + |
| 49 | +# Streamlit UI |
| 50 | +st.title("Image to Text with Oci Gen AI") |
| 51 | +st.write("Upload an image, provide a prompt, and get a response from Oci Gen AI.") |
| 52 | + |
| 53 | +# Upload image |
| 54 | +uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) |
| 55 | + |
| 56 | +# Prompt input |
| 57 | +user_prompt = st.text_input("Enter your prompt for the image:", value="Tell me about this image.") |
| 58 | + |
| 59 | +if uploaded_file: |
| 60 | + # Save the uploaded image temporarily |
| 61 | + temp_image_path = "temp_uploaded_image.jpg" |
| 62 | + with open(temp_image_path, "wb") as f: |
| 63 | + f.write(uploaded_file.getbuffer()) |
| 64 | + |
| 65 | + # Display the uploaded image |
| 66 | + st.image(temp_image_path, caption="Uploaded Image", use_column_width=True) |
| 67 | + |
| 68 | + # Process and call the model |
| 69 | + if st.button("Generate Response"): |
| 70 | + with st.spinner("Please wait while the model processes the image..."): |
| 71 | + try: |
| 72 | + # Encode the image |
| 73 | + encoded_image = encode_image(temp_image_path) |
| 74 | + |
| 75 | + # Setup OCI client |
| 76 | + CONFIG_PROFILE = "DEFAULT" |
| 77 | + config = oci.config.from_file('~/.oci/config', CONFIG_PROFILE) |
| 78 | + |
| 79 | + llm_client = oci.generative_ai_inference.GenerativeAiInferenceClient( |
| 80 | + config=config, |
| 81 | + service_endpoint=llm_service_endpoint, |
| 82 | + retry_strategy=oci.retry.NoneRetryStrategy(), |
| 83 | + timeout=(10, 240) |
| 84 | + ) |
| 85 | + |
| 86 | + # Get the chat request and response |
| 87 | + llm_request = get_chat_request(encoded_image, user_prompt) |
| 88 | + llm_payload = get_chat_detail(llm_request) |
| 89 | + llm_response = llm_client.chat(llm_payload) |
| 90 | + |
| 91 | + # Extract and display the response |
| 92 | + llm_text = llm_response.data.chat_response.choices[0].message.content[0].text |
| 93 | + st.success("Model Response:") |
| 94 | + st.write(llm_text) |
| 95 | + except Exception as e: |
| 96 | + st.error(f"An error occurred: {str(e)}") |
0 commit comments