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 # Set your Clarifai credentials and model details for GPT-4 Vision PAT_GPT4 = '3ca5bd8b0f2244eb8d0e4b2838fc3cf1' USER_ID_GPT4 = 'openai' APP_ID_GPT4 = 'chat-completion' MODEL_ID_GPT4 = 'openai-gpt-4-vision' MODEL_VERSION_ID_GPT4 = '266df29bc09843e0aee9b7bf723c03c2' # Set your Clarifai credentials and model details for DALL-E PAT_DALLE = 'bfdeb4029ef54d23a2e608b0aa4c00e4' USER_ID_DALLE = 'openai' APP_ID_DALLE = 'dall-e' MODEL_ID_DALLE = 'dall-e-3' MODEL_VERSION_ID_DALLE = 'dc9dcb6ee67543cebc0b9a025861b868' # Streamlit app # Set your Clarifai credentials for Text-to-Speech (TTS) PAT_TTS = 'bfdeb4029ef54d23a2e608b0aa4c00e4' USER_ID_TTS = 'openai' APP_ID_TTS = 'tts' MODEL_ID_TTS = 'openai-tts-1' MODEL_VERSION_ID_TTS = 'fff6ce1fd487457da95b79241ac6f02d' # Set up gRPC channel for Text-to-Speech (TTS) channel_tts = ClarifaiChannel.get_grpc_channel() stub_tts = service_pb2_grpc.V2Stub(channel_tts) metadata_tts = (('authorization', 'Key ' + PAT_TTS),) userDataObject_tts = resources_pb2.UserAppIDSet(user_id=USER_ID_TTS, app_id=APP_ID_TTS,) # Streamlit app st.title("Smart Crop Advisor") # Inserting logo st.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAA9lBMVEUALlb///8ALFUAKFIAGEoAIk8AJlEAG0y4wcsALlUAKlQAJ1IAJFDY3+W0v8oFNVwAHk0AEkjr7vEAH03P1t1AW3itvMhdc4v09vjs8PNrg5JodYoAD0jg5uv3+fpKiz0VOF0yS2uAkaJtoWHG0Nigr71nfJN0h5sAC0VFiTeaqLY6VnVIYn5UbIZ6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saFDvM4Q3iOGn8+Qy9OxGWNVxVulOfSUeImpb3ibow/KN4R4Mw6LGJvh9GRJi+O/t+fhfhpBU6jhPa7EfQ05WWZVe6sHrF/Psw5DT5ykY6q/H49evLrpfmKfBqeM89DslsnXLwuEWTS1fr1O2vRhy4cGwCEo4d/YZvkPxmjkYvGK6dXhGnvAAv6/2Nr3Ys+XgV0Ttx5AzrhmzVwGTOMPZ4IoZmnmFcxwcPaRBC+jLRWJfydsMXJE3CbMYVnYzZMxeaw4Fh2b4CTl78+5yxdY8oHL+6QWLuTeWHrIEZs1IC0anMtIk4oMBPyn7aIarxQg+ok7Y44+aodUAkrag+snKC01Hqhfc6TN8f3Hx4fry740NH0BaNVsZXgqyaY0CVfLe9Fcp2bZdmsxnmhB1skBWvC4NPvmRnPwoolDAUBrzSQnBGU1lr6oBZOpOZXZ7+1Z7e7vl5Sqy3mhm2Hna/CDvbY2CAFVJ0DO/sHE7eKgLCBIMeo8iD1e8PQl1+1v3+CwO/1ibkAscEnaW1Ao1LfDEKFSwwNGgUOYCBQr8EhyescncmvAoo3hEHNyAM6NgmM8tT4i9/lDQIf2FzsMSxx4oCBYo8MR4mI4egIaneAHU9vsPyWcU2IQD0NQH4jkzfM5jK1AgbxT6XiB/PIZWPSdNfU5jOSg8n4l7PiM5RBSz9+h4aDpY4Ilx7PI7dn5HT/AJ+B37lBYoUODF4NjNWbGj8EvxMqY7t/eYH/vqO3YciPz2HGbur+t/RGR9Ucoe9zwTnJ7uGDg4VGY0PN2L6Z/XHeB/dPjsChQoUOBF4P+xvGxHlRtZjAAAAABJRU5ErkJggg==", width=200) # Function to get gRPC channel for Text-to-Speech (TTS) def get_tts_channel(): channel_tts = ClarifaiChannel.get_grpc_channel() return channel_tts, channel_tts.metadata # User input model_type = st.selectbox("Select Model", ["GPT-4 Vision", "DALL-E", "Text-to-Speech (TTS)"]) raw_text = st.text_area("Enter Text") image_upload = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"]) # Button to generate result if st.button("Generate Result"): if model_type == "GPT-4 Vision": # Set up gRPC channel for GPT-4 Vision channel_gpt4 = ClarifaiChannel.get_grpc_channel() stub_gpt4 = service_pb2_grpc.V2Stub(channel_gpt4) metadata_gpt4 = (('authorization', 'Key ' + PAT_GPT4),) userDataObject_gpt4 = resources_pb2.UserAppIDSet(user_id=USER_ID_GPT4, app_id=APP_ID_GPT4) # Prepare the request for GPT-4 Vision input_data_gpt4 = resources_pb2.Data() if raw_text: input_data_gpt4.text.raw = raw_text if image_upload is not None: image_bytes_gpt4 = image_upload.read() input_data_gpt4.image.base64 = image_bytes_gpt4 post_model_outputs_response_gpt4 = stub_gpt4.PostModelOutputs( service_pb2.PostModelOutputsRequest( user_app_id=userDataObject_gpt4, model_id=MODEL_ID_GPT4, version_id=MODEL_VERSION_ID_GPT4, inputs=[resources_pb2.Input(data=input_data_gpt4)] ), metadata=metadata_gpt4 # Use metadata directly in the gRPC request ) # Check if the request was successful for GPT-4 Vision if post_model_outputs_response_gpt4.status.code != status_code_pb2.SUCCESS: st.error(f"GPT-4 Vision API request failed: {post_model_outputs_response_gpt4.status.description}") else: # Get the output for GPT-4 Vision output_gpt4 = post_model_outputs_response_gpt4.outputs[0].data # Display the result for GPT-4 Vision if output_gpt4.HasField("image"): st.image(output_gpt4.image.base64, caption='Generated Image (GPT-4 Vision)', use_column_width=True) elif output_gpt4.HasField("text"): # Display the text result st.text(output_gpt4.text.raw) # Convert text to speech and play the audio stub_tts = service_pb2_grpc.V2Stub(channel_gpt4) # Use the same channel for TTS tts_input_data = resources_pb2.Data() tts_input_data.text.raw = output_gpt4.text.raw tts_response = stub_tts.PostModelOutputs( service_pb2.PostModelOutputsRequest( user_app_id=userDataObject_tts, model_id=MODEL_ID_TTS, version_id=MODEL_VERSION_ID_TTS, inputs=[resources_pb2.Input(data=tts_input_data)] ), metadata=metadata_gpt4 # Use the same metadata for TTS ) # Check if the TTS request was successful if tts_response.status.code == status_code_pb2.SUCCESS: tts_output = tts_response.outputs[0].data st.audio(tts_output.audio.base64, format='audio/wav') else: st.error(f"Text-to-Speech (TTS) API request failed: {tts_response.status.description}") elif model_type == "DALL-E": # Set up gRPC channel for DALL-E channel_dalle = ClarifaiChannel.get_grpc_channel() stub_dalle = service_pb2_grpc.V2Stub(channel_dalle) metadata_dalle = (('authorization', 'Key ' + PAT_DALLE),) userDataObject_dalle = resources_pb2.UserAppIDSet(user_id=USER_ID_DALLE, app_id=APP_ID_DALLE) # Prepare the request for DALL-E input_data_dalle = resources_pb2.Data() if raw_text: input_data_dalle.text.raw = raw_text post_model_outputs_response_dalle = stub_dalle.PostModelOutputs( service_pb2.PostModelOutputsRequest( user_app_id=userDataObject_dalle, model_id=MODEL_ID_DALLE, version_id=MODEL_VERSION_ID_DALLE, inputs=[resources_pb2.Input(data=input_data_dalle)] ), metadata=metadata_dalle ) # Check if the request was successful for DALL-E if post_model_outputs_response_dalle.status.code != status_code_pb2.SUCCESS: st.error(f"DALL-E API request failed: {post_model_outputs_response_dalle.status.description}") else: # Get the output for DALL-E output_dalle = post_model_outputs_response_dalle.outputs[0].data # Display the result for DALL-E if output_dalle.HasField("image"): st.image(output_dalle.image.base64, caption='Generated Image (DALL-E)', use_column_width=True) elif output_dalle.HasField("text"): st.text(output_dalle.text.raw) elif model_type == "Text-to-Speech (TTS)": # Set up gRPC channel for Text-to-Speech (TTS) channel_tts = ClarifaiChannel.get_grpc_channel() stub_tts = service_pb2_grpc.V2Stub(channel_tts) metadata_tts = (('authorization', 'Key ' + PAT_TTS),) userDataObject_tts = resources_pb2.UserAppIDSet(user_id=USER_ID_TTS, app_id=APP_ID_TTS) # Prepare the request for Text-to-Speech (TTS) input_data_tts = resources_pb2.Data() if raw_text: input_data_tts.text.raw = raw_text post_model_outputs_response_tts = stub_tts.PostModelOutputs( service_pb2.PostModelOutputsRequest( user_app_id=userDataObject_tts, model_id=MODEL_ID_TTS, version_id=MODEL_VERSION_ID_TTS, inputs=[resources_pb2.Input(data=input_data_tts)] ), metadata=metadata_tts ) # Check if the request was successful for Text-to-Speech (TTS) if post_model_outputs_response_tts.status.code != status_code_pb2.SUCCESS: st.error(f"Text-to-Speech (TTS) API request failed: {post_model_outputs_response_tts.status.description}") else: # Get the output for Text-to-Speech (TTS) output_tts = post_model_outputs_response_tts.outputs[0].data # Display the result for Text-to-Speech (TTS) if output_tts.HasField("text"): st.text(output_tts.text.raw) if output_tts.HasField("audio"): st.audio(output_tts.audio.base64, format='audio/wav') # Add the beautiful social media icon section st.markdown("""

""", unsafe_allow_html=True)