pyresearch commited on
Commit
350e0db
·
verified ·
1 Parent(s): 426a38b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -56
app.py CHANGED
@@ -3,6 +3,13 @@ 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
 
 
 
 
 
 
 
 
6
  # Set your Clarifai credentials and model details for DALL-E
7
  PAT_DALLE = 'bfdeb4029ef54d23a2e608b0aa4c00e4'
8
  USER_ID_DALLE = 'openai'
@@ -10,89 +17,99 @@ APP_ID_DALLE = 'dall-e'
10
  MODEL_ID_DALLE = 'dall-e-3'
11
  MODEL_VERSION_ID_DALLE = 'dc9dcb6ee67543cebc0b9a025861b868'
12
 
13
- # Set your Clarifai credentials and model details for GPT-4 Turbo
14
- PAT_GPT4 = 'bfdeb4029ef54d23a2e608b0aa4c00e4'
15
- USER_ID_GPT4 = 'openai'
16
- APP_ID_GPT4 = 'chat-completion'
17
- MODEL_ID_GPT4 = 'gpt-4-turbo'
18
- MODEL_VERSION_ID_GPT4 = '182136408b4b4002a920fd500839f2c8'
19
-
20
  # Streamlit app
21
- st.title("Pyresearch AI Integration App")
 
 
 
22
 
23
  # Choose model type
24
- model_type = st.radio("Select Model Type", ["DALL-E", "GPT-4 Turbo"])
25
 
26
  # Input text prompt from the user
27
- raw_text = st.text_input("Enter a text prompt:", 'ocr check mistake with image base with python opencv computer vision help out to know people')
 
 
 
28
 
29
  # Button to generate result
30
  if st.button("Generate Result"):
31
- if model_type == "DALL-E":
32
- # Connect to Clarifai API for DALL-E
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  channel_dalle = ClarifaiChannel.get_grpc_channel()
34
  stub_dalle = service_pb2_grpc.V2Stub(channel_dalle)
35
  metadata_dalle = (('authorization', 'Key ' + PAT_DALLE),)
36
  userDataObject_dalle = resources_pb2.UserAppIDSet(user_id=USER_ID_DALLE, app_id=APP_ID_DALLE)
37
 
38
- # Make a request to Clarifai API for DALL-E
 
 
 
 
 
39
  post_model_outputs_response_dalle = stub_dalle.PostModelOutputs(
40
  service_pb2.PostModelOutputsRequest(
41
  user_app_id=userDataObject_dalle,
42
  model_id=MODEL_ID_DALLE,
43
  version_id=MODEL_VERSION_ID_DALLE,
44
- inputs=[
45
- resources_pb2.Input(
46
- data=resources_pb2.Data(
47
- text=resources_pb2.Text(
48
- raw=raw_text
49
- )
50
- )
51
- )
52
- ]
53
  ),
54
  metadata=metadata_dalle
55
  )
56
 
57
- # Display the generated image if the request is successful
58
  if post_model_outputs_response_dalle.status.code != status_code_pb2.SUCCESS:
59
  st.error(f"DALL-E API request failed: {post_model_outputs_response_dalle.status.description}")
60
  else:
61
- output_dalle = post_model_outputs_response_dalle.outputs[0].data.image.base64
62
- st.image(output_dalle, caption='Generated Image (DALL-E)', use_column_width=True)
63
 
64
- elif model_type == "GPT-4 Turbo":
65
- # Connect to Clarifai API for GPT-4 Turbo
66
- channel_gpt4 = ClarifaiChannel.get_grpc_channel()
67
- stub_gpt4 = service_pb2_grpc.V2Stub(channel_gpt4)
68
- metadata_gpt4 = (('authorization', 'Key ' + PAT_GPT4),)
69
- userDataObject_gpt4 = resources_pb2.UserAppIDSet(user_id=USER_ID_GPT4, app_id=APP_ID_GPT4)
70
 
71
- # Make a request to Clarifai API for GPT-4 Turbo
72
- post_model_outputs_response_gpt4 = stub_gpt4.PostModelOutputs(
73
- service_pb2.PostModelOutputsRequest(
74
- user_app_id=userDataObject_gpt4,
75
- model_id=MODEL_ID_GPT4,
76
- version_id=MODEL_VERSION_ID_GPT4,
77
- inputs=[
78
- resources_pb2.Input(
79
- data=resources_pb2.Data(
80
- text=resources_pb2.Text(
81
- raw=raw_text
82
- )
83
- )
84
- )
85
- ]
86
- ),
87
- metadata=metadata_gpt4
88
- )
89
-
90
- # Display the generated result if the request is successful
91
- if post_model_outputs_response_gpt4.status.code != status_code_pb2.SUCCESS:
92
- st.error(f"GPT-4 Turbo API request failed: {post_model_outputs_response_gpt4.status.description}")
93
- else:
94
- output_gpt4 = post_model_outputs_response_gpt4.outputs[0].data.image.base64
95
- st.image(output_gpt4, caption='Generated Image (GPT-4 Turbo)', use_column_width=True)
96
 
97
 
98
 
 
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
 
6
+ # Set your Clarifai credentials and model details for GPT-4 Vision
7
+ PAT_GPT4 = '3ca5bd8b0f2244eb8d0e4b2838fc3cf1'
8
+ USER_ID_GPT4 = 'openai'
9
+ APP_ID_GPT4 = 'chat-completion'
10
+ MODEL_ID_GPT4 = 'openai-gpt-4-vision'
11
+ MODEL_VERSION_ID_GPT4 = '266df29bc09843e0aee9b7bf723c03c2'
12
+
13
  # Set your Clarifai credentials and model details for DALL-E
14
  PAT_DALLE = 'bfdeb4029ef54d23a2e608b0aa4c00e4'
15
  USER_ID_DALLE = 'openai'
 
17
  MODEL_ID_DALLE = 'dall-e-3'
18
  MODEL_VERSION_ID_DALLE = 'dc9dcb6ee67543cebc0b9a025861b868'
19
 
 
 
 
 
 
 
 
20
  # Streamlit app
21
+ st.title("Smart Crop Adviser")
22
+
23
+ # Inserting logo
24
+ st.image("https://cdn.tractorkarvan.com/tr:f-webp/images/Blogs/smart-farming-in-india/Smart-Farming-Blog.jpg", width=200)
25
 
26
  # Choose model type
27
+ model_type = st.radio("Select Model Type", ["GPT-4 Vision", "DALL-E"])
28
 
29
  # Input text prompt from the user
30
+ raw_text = st.text_input("Enter a text prompt:", 'What time of day is it?')
31
+
32
+ # File upload for image
33
+ image_upload = st.file_uploader("Upload an image:", type=["jpg", "jpeg", "png"])
34
 
35
  # Button to generate result
36
  if st.button("Generate Result"):
37
+ if model_type == "GPT-4 Vision":
38
+ # Set up gRPC channel for GPT-4 Vision
39
+ channel_gpt4 = ClarifaiChannel.get_grpc_channel()
40
+ stub_gpt4 = service_pb2_grpc.V2Stub(channel_gpt4)
41
+ metadata_gpt4 = (('authorization', 'Key ' + PAT_GPT4),)
42
+ userDataObject_gpt4 = resources_pb2.UserAppIDSet(user_id=USER_ID_GPT4, app_id=APP_ID_GPT4)
43
+
44
+ # Prepare the request for GPT-4 Vision
45
+ input_data_gpt4 = resources_pb2.Data()
46
+
47
+ if raw_text:
48
+ input_data_gpt4.text.raw = raw_text
49
+
50
+ if image_upload is not None:
51
+ image_bytes_gpt4 = image_upload.read()
52
+ input_data_gpt4.image.base64 = image_bytes_gpt4
53
+
54
+ post_model_outputs_response_gpt4 = stub_gpt4.PostModelOutputs(
55
+ service_pb2.PostModelOutputsRequest(
56
+ user_app_id=userDataObject_gpt4,
57
+ model_id=MODEL_ID_GPT4,
58
+ version_id=MODEL_VERSION_ID_GPT4,
59
+ inputs=[resources_pb2.Input(data=input_data_gpt4)]
60
+ ),
61
+ metadata=metadata_gpt4
62
+ )
63
+
64
+ # Check if the request was successful for GPT-4 Vision
65
+ if post_model_outputs_response_gpt4.status.code != status_code_pb2.SUCCESS:
66
+ st.error(f"GPT-4 Vision API request failed: {post_model_outputs_response_gpt4.status.description}")
67
+ else:
68
+ # Get the output for GPT-4 Vision
69
+ output_gpt4 = post_model_outputs_response_gpt4.outputs[0].data
70
+
71
+ # Display the result for GPT-4 Vision
72
+ if output_gpt4.HasField("image"):
73
+ st.image(output_gpt4.image.base64, caption='Generated Image (GPT-4 Vision)', use_column_width=True)
74
+ elif output_gpt4.HasField("text"):
75
+ st.text(output_gpt4.text.raw)
76
+
77
+ elif model_type == "DALL-E":
78
+ # Set up gRPC channel for DALL-E
79
  channel_dalle = ClarifaiChannel.get_grpc_channel()
80
  stub_dalle = service_pb2_grpc.V2Stub(channel_dalle)
81
  metadata_dalle = (('authorization', 'Key ' + PAT_DALLE),)
82
  userDataObject_dalle = resources_pb2.UserAppIDSet(user_id=USER_ID_DALLE, app_id=APP_ID_DALLE)
83
 
84
+ # Prepare the request for DALL-E
85
+ input_data_dalle = resources_pb2.Data()
86
+
87
+ if raw_text:
88
+ input_data_dalle.text.raw = raw_text
89
+
90
  post_model_outputs_response_dalle = stub_dalle.PostModelOutputs(
91
  service_pb2.PostModelOutputsRequest(
92
  user_app_id=userDataObject_dalle,
93
  model_id=MODEL_ID_DALLE,
94
  version_id=MODEL_VERSION_ID_DALLE,
95
+ inputs=[resources_pb2.Input(data=input_data_dalle)]
 
 
 
 
 
 
 
 
96
  ),
97
  metadata=metadata_dalle
98
  )
99
 
100
+ # Check if the request was successful for DALL-E
101
  if post_model_outputs_response_dalle.status.code != status_code_pb2.SUCCESS:
102
  st.error(f"DALL-E API request failed: {post_model_outputs_response_dalle.status.description}")
103
  else:
104
+ # Get the output for DALL-E
105
+ output_dalle = post_model_outputs_response_dalle.outputs[0].data
106
 
107
+ # Display the result for DALL-E
108
+ if output_dalle.HasField("image"):
109
+ st.image(output_dalle.image.base64, caption='Generated Image (DALL-E)', use_column_width=True)
110
+ elif output_dalle.HasField("text"):
111
+ st.text(output_dalle.text.raw)
 
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
 
115