Spaces:
Runtime error
Runtime error
Clean and restructure the module
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from global_config import GlobalConfig####################################################################################################### In this section, we set the user authentication, user and app ID, model details, and the URL of# the text we want as an input. Change these strings to run your own example.######################################################################################################TEXT_FILE_URL = 'https://samples.clarifai.com/negative_sentence_12.txt'############################################################################# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE############################################################################from clarifai_grpc.channel.clarifai_channel import ClarifaiChannelfrom clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpcfrom clarifai_grpc.grpc.api.status import status_code_pb2channel = ClarifaiChannel.get_grpc_channel()stub = service_pb2_grpc.V2Stub(channel)metadata = (('authorization', 'Key ' + GlobalConfig.CLARIFAI_PAT),# ('temp', '0.9'), # Does not work)userDataObject = resources_pb2.UserAppIDSet(user_id=GlobalConfig.CLARIFAI_USER_ID,app_id=GlobalConfig.CLARIFAI_APP_ID)RAW_TEXT = '''You are a helpful, intelligent chatbot.Create the slides for a presentation on the given topic.Include main headings for each slide, detailed bullet points for each slide.Add relevant content to each slide.The output should be complete, coherent, and have maximum 255 tokens.Topic:Talk about AI, covering what it is and how it works. Add its pros, cons, and future prospects. Also, cover its job prospects.'''post_model_outputs_response = stub.PostModelOutputs(service_pb2.PostModelOutputsRequest(user_app_id=userDataObject, # The userDataObject is created in the overview and is required when using a PATmodel_id=GlobalConfig.CLARIFAI_MODEL_ID,# version_id=MODEL_VERSION_ID, # This is optional. Defaults to the latest model versioninputs=[resources_pb2.Input(data=resources_pb2.Data(text=resources_pb2.Text(# url=TEXT_FILE_URL,raw=RAW_TEXT)))]),metadata=metadata)if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:print(post_model_outputs_response.status)raise Exception(f"Post model outputs failed, status: {post_model_outputs_response.status.description}")# Since we have one input, one output will exist hereoutput = post_model_outputs_response.outputs[0]print("Completion:\n")print(output.data.text.raw)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071from clarifai_grpc.channel.clarifai_channel import ClarifaiChannelfrom clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpcfrom clarifai_grpc.grpc.api.status import status_code_pb2from global_config import GlobalConfigCHANNEL = ClarifaiChannel.get_grpc_channel()STUB = service_pb2_grpc.V2Stub(CHANNEL)METADATA = (('authorization', 'Key ' + GlobalConfig.CLARIFAI_PAT),)USER_DATA_OBJECT = resources_pb2.UserAppIDSet(user_id=GlobalConfig.CLARIFAI_USER_ID,app_id=GlobalConfig.CLARIFAI_APP_ID)RAW_TEXT = '''You are a helpful, intelligent chatbot. Create the slides for a presentation on the given topic. Include main headings for each slide, detailed bullet points for each slide. Add relevant content to each slide. Do not output any blank line.Topic:Talk about AI, covering what it is and how it works. Add its pros, cons, and future prospects. Also, cover its job prospects.'''def get_text_from_llm(prompt: str) -> str:post_model_outputs_response = STUB.PostModelOutputs(service_pb2.PostModelOutputsRequest(user_app_id=USER_DATA_OBJECT, # The userDataObject is created in the overview and is required when using a PATmodel_id=GlobalConfig.CLARIFAI_MODEL_ID,# version_id=MODEL_VERSION_ID, # This is optional. Defaults to the latest model versioninputs=[resources_pb2.Input(data=resources_pb2.Data(text=resources_pb2.Text(raw=prompt)))]),metadata=METADATA)if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:print(post_model_outputs_response.status)raise Exception(f"Post model outputs failed, status: {post_model_outputs_response.status.description}")# Since we have one input, one output will exist hereoutput = post_model_outputs_response.outputs[0]# print("Completion:\n")# print(output.data.text.raw)return output.data.text.rawif __name__ == '__main__':topic = ('Talk about AI, covering what it is and how it works.'' Add its pros, cons, and future prospects.'' Also, cover its job prospects.')print(topic)with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r') as in_file:prompt_txt = in_file.read()prompt_txt = prompt_txt.replace('{topic}', topic)response_txt = get_text_from_llm(prompt_txt)print('Output:\n', response_txt)
clarifai_grpc_helper.py+50-46