import os import streamlit as st from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc from clarifai_grpc.grpc.api.status import status_code_pb2 import io # Function to call Clarifai API def get_image_concepts(image_bytes): channel = ClarifaiChannel.get_grpc_channel() stub = service_pb2_grpc.V2Stub(channel) PAT = os.getenv('CLARIFAI_PAT') USER_ID = 'clarifai' APP_ID = 'main' MODEL_ID = 'general-image-detection' MODEL_VERSION_ID = '1580bb1932594c93b7e2e04456af7c6f' metadata = (('authorization', 'Key ' + PAT),) userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) post_model_outputs_response = stub.PostModelOutputs( service_pb2.PostModelOutputsRequest( user_app_id=userDataObject, model_id=MODEL_ID, version_id=MODEL_VERSION_ID, inputs=[ resources_pb2.Input( data=resources_pb2.Data( image=resources_pb2.Image( base64=image_bytes ) ) ) ] ), metadata=metadata ) if post_model_outputs_response.status.code != status_code_pb2.SUCCESS: raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description) return post_model_outputs_response.outputs[0].data.regions # Streamlit interface st.title("Image Detection with Clarifai") uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: image_bytes = uploaded_file.getvalue() regions = get_image_concepts(image_bytes) unique_names = set() for region in regions: for concept in region.data.concepts: name = concept.name # Add unique names to the set unique_names.add(name) # Display unique names if unique_names: st.write("Unique items detected in the image:") for name in unique_names: st.write(name) else: st.write("No unique items detected.") """ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: image_bytes = uploaded_file.getvalue() regions = get_image_concepts(image_bytes) for region in regions: # Display each detected item for concept in region.data.concepts: name = concept.name value = round(concept.value, 4) st.write(f"{name}: {value}") # Run this with `streamlit run your_script_name.py` """