eaglelandsonce commited on
Commit
55ea004
·
verified ·
1 Parent(s): d19eae1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
3
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
4
+ from clarifai_grpc.grpc.api.status import status_code_pb2
5
+ import io
6
+
7
+ # Function to call Clarifai API
8
+ def get_image_concepts(image_bytes):
9
+ channel = ClarifaiChannel.get_grpc_channel()
10
+ stub = service_pb2_grpc.V2Stub(channel)
11
+
12
+ PAT = 'YOUR_PERSONAL_ACCESS_TOKEN'
13
+ USER_ID = 'clarifai'
14
+ APP_ID = 'main'
15
+ MODEL_ID = 'general-image-detection'
16
+ MODEL_VERSION_ID = 'YOUR_MODEL_VERSION_ID'
17
+
18
+ metadata = (('authorization', 'Key ' + PAT),)
19
+ userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
20
+
21
+ post_model_outputs_response = stub.PostModelOutputs(
22
+ service_pb2.PostModelOutputsRequest(
23
+ user_app_id=userDataObject,
24
+ model_id=MODEL_ID,
25
+ version_id=MODEL_VERSION_ID,
26
+ inputs=[
27
+ resources_pb2.Input(
28
+ data=resources_pb2.Data(
29
+ image=resources_pb2.Image(
30
+ base64=image_bytes
31
+ )
32
+ )
33
+ )
34
+ ]
35
+ ),
36
+ metadata=metadata
37
+ )
38
+
39
+ if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
40
+ raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)
41
+
42
+ return post_model_outputs_response.outputs[0].data.regions
43
+
44
+ # Streamlit interface
45
+ st.title("Image Detection with Clarifai")
46
+
47
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
48
+ if uploaded_file is not None:
49
+ image_bytes = uploaded_file.getvalue()
50
+ regions = get_image_concepts(image_bytes)
51
+ for region in regions:
52
+ # Display each detected item
53
+ for concept in region.data.concepts:
54
+ name = concept.name
55
+ value = round(concept.value, 4)
56
+ st.write(f"{name}: {value}")
57
+
58
+ # Run this with `streamlit run your_script_name.py`