项目作者: Clarifai

项目描述 :
DEPRECATED Clarifai API Python Client, use clarifai-python-grpc instead
高级语言: Python
项目地址: git://github.com/Clarifai/clarifai-python.git
创建时间: 2014-05-30T15:10:36Z
项目社区:https://github.com/Clarifai/clarifai-python

开源协议:Other

下载



Clarifai


Clarifai Python SDK


Discord

PyPI - Downloads

PyPI - Versions

This is the official Python client for interacting with our powerful API. The Clarifai Python SDK offers a comprehensive set of tools to integrate Clarifai’s AI platform to leverage computer vision capabilities like classification , detection ,segementation and natural language capabilities like classification , summarisation , generation , Q&A ,etc into your applications. With just a few lines of code, you can leverage cutting-edge artificial intelligence to unlock valuable insights from visual and textual content.

Website | Schedule Demo | Signup for a Free Account | API Docs | Clarifai Community | Python SDK Docs | Examples | Colab Notebooks | Discord

Give the repo a star ⭐

Table Of Contents

:rocket: Installation

Install from PyPi:

  1. pip install -U clarifai

Install from Source:

  1. git clone https://github.com/Clarifai/clarifai-python.git
  2. cd clarifai-python
  3. python3 -m venv .venv
  4. source .venv/bin/activate
  5. pip install -r requirements.txt
  6. python setup.py install

:memo: Getting started

Clarifai uses Personal Access Tokens(PATs) to validate requests. You can create and manage PATs under your Clarifai account security settings.

  • 🔗 Create PAT: Log into Portal → Profile Icon → Security Settings → Create Personal Access Token → Set the scopes → Confirm

  • 🔗 Get User ID: Log into Portal → Profile Icon → Account → Profile → User-ID

Export your PAT as an environment variable. Then, import and initialize the API Client.

Set PAT as environment variable through terminal:

  1. export CLARIFAI_PAT={your personal access token}
  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.user import User
  3. client = User(user_id="user_id")
  4. # Get all apps
  5. apps_generator = client.list_apps()
  6. apps = list(apps_generator)

OR

PAT can be passed as constructor argument

  1. from clarifai.client.user import User
  2. client = User(user_id="user_id", pat="your personal access token")

:rocket: Compute Orchestration

Clarifai’s Compute Orchestration offers a streamlined solution for managing the infrastructure required for training, deploying, and scaling machine learning models and workflows.

This flexible system supports any compute instance — across various hardware providers and deployment methods — and provides automatic scaling to match workload demands. More Details

Cluster Operations

  1. from clarifai.client.user import User
  2. client = User(user_id="user_id",base_url="https://api.clarifai.com")
  3. # Create a new compute cluster
  4. compute_cluster = client.create_compute_cluster(compute_cluster_id="demo-id",config_filepath="computer_cluster_config.yaml")
  5. # List Clusters
  6. all_compute_clusters = list(client.list_compute_clusters())
  7. print(all_compute_clusters)
Example Cluster Config

Nodepool Operations

  1. from clarifai.client.compute_cluster import ComputeCluster
  2. # Initialize the ComputeCluster instance
  3. compute_cluster = ComputeCluster(user_id="user_id",compute_cluster_id="demo-id")
  4. # Create a new nodepool
  5. nodepool = compute_cluster.create_nodepool(nodepool_id="demo-nodepool-id",config_filepath="nodepool_config.yaml")
  6. #Get a nodepool
  7. nodepool = compute_cluster.nodepool(nodepool_id="demo-nodepool-id")
  8. print(nodepool)
  9. # List nodepools
  10. all_nodepools = list(compute_cluster.list_nodepools())
  11. print(all_nodepools)
Example Nodepool config

Deployment Operations

  1. from clarifai.client.nodepool import Nodepool
  2. # Initialize the Nodepool instance
  3. nodepool = Nodepool(user_id="user_id",nodepool_id="demo-nodepool-id")
  4. # Create a new deployment
  5. deployment = nodepool.create_deployment(deployment_id="demo-deployment-id",config_filepath="deployment_config.yaml")
  6. #Get a deployment
  7. deployment = nodepool.deployment(nodepool_id="demo-deployment-id")
  8. print(deployment)
  9. # List deployments
  10. all_deployments = list(nodepool.list_deployments())
  11. print(all_deployments)
Example Deployment config

Compute Orchestration CLI Operations

Refer Here: https://github.com/Clarifai/clarifai-python/tree/master/clarifai/cli

:floppy_disk: Interacting with Datasets

Clarifai datasets help in managing datasets used for model training and evaluation. It provides functionalities like creating datasets,uploading datasets, retrying failed uploads from logs and exporting datasets as .zip files.

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. # Create app and dataset
  3. app = client.create_app(app_id="demo_app", base_workflow="Universal")
  4. dataset = app.create_dataset(dataset_id="demo_dataset")
  5. # execute data upload to Clarifai app dataset
  6. from clarifai.datasets.upload.loaders.coco_detection import COCODetectionDataLoader
  7. coco_dataloader = COCODetectionDataLoader("images_dir", "coco_annotation_filepath")
  8. dataset.upload_dataset(dataloader=coco_dataloader, get_upload_status=True)
  9. #Try upload and record the failed outputs in log file.
  10. from clarifai.datasets.upload.utils import load_module_dataloader
  11. cifar_dataloader = load_module_dataloader('./image_classification/cifar10')
  12. dataset.upload_dataset(dataloader=cifar_dataloader,
  13. get_upload_status=True,
  14. log_warnings =True)
  15. #Retry upload from logs for `upload_dataset`
  16. # Set retry_duplicates to True if you want to ingest failed inputs due to duplication issues. by default it is set to 'False'.
  17. dataset.retry_upload_from_logs(dataloader=cifar_dataloader, log_file_path='log_file.log',
  18. retry_duplicates=True,
  19. log_warnings=True)
  20. #upload text from csv
  21. dataset.upload_from_csv(csv_path='csv_path', input_type='text', csv_type='raw', labels=True)
  22. #upload data from folder
  23. dataset.upload_from_folder(folder_path='folder_path', input_type='text', labels=True)
  24. # Export Dataset
  25. dataset.export(save_path='output.zip')

:floppy_disk: Interacting with Inputs

You can use inputs() for adding and interacting with input data. Inputs can be uploaded directly from a URL or a file. You can also view input annotations and concepts.

Input Upload

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.user import User
  3. app = User(user_id="user_id").app(app_id="app_id")
  4. input_obj = app.inputs()
  5. #input upload from url
  6. input_obj.upload_from_url(input_id = 'demo', image_url='https://samples.clarifai.com/metro-north.jpg')
  7. #input upload from filename
  8. input_obj.upload_from_file(input_id = 'demo', video_file='demo.mp4')
  9. # text upload
  10. input_obj.upload_text(input_id = 'demo', raw_text = 'This is a test')

Input Listing

  1. #listing inputs
  2. input_generator = input_obj.list_inputs(page_no=1,per_page=10,input_type='image')
  3. inputs_list = list(input_generator)
  4. #listing annotations
  5. annotation_generator = input_obj.list_annotations(batch_input=inputs_list)
  6. annotations_list = list(annotation_generator)
  7. #listing concepts
  8. all_concepts = list(app.list_concepts())

Input Download

  1. #listing inputs
  2. input_generator = input_obj.list_inputs(page_no=1,per_page=1,input_type='image')
  3. inputs_list = list(input_generator)
  4. #downloading_inputs
  5. input_bytes = input_obj.download_inputs(inputs_list)
  6. with open('demo.jpg','wb') as f:
  7. f.write(input_bytes[0])

:brain: Interacting with Models

The Model Class allows you to perform predictions using Clarifai models. You can specify which model to use by providing the model URL or ID. This gives you flexibility in choosing models. The App Class also allows listing of all available Clarifai models for discovery.
For greater control over model predictions, you can pass in an output_config to modify the model output as demonstrated below.

Model Predict

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.model import Model
  3. """
  4. Get Model information on details of model(description, usecases..etc) and info on training or
  5. # other inference parameters(eg: temperature, top_k, max_tokens..etc for LLMs)
  6. """
  7. gpt_4_model = Model("https://clarifai.com/openai/chat-completion/models/GPT-4")
  8. print(gpt_4_model)
  9. # Model Predict
  10. model_prediction = Model("https://clarifai.com/anthropic/completion/models/claude-v2").predict_by_bytes(b"Write a tweet on future of AI")
  11. # Customizing Model Inference Output
  12. model_prediction = gpt_4_model.predict_by_bytes(b"Write a tweet on future of AI", inference_params=dict(temperature=str(0.7), max_tokens=30))
  13. # Return predictions having prediction confidence > 0.98
  14. model_prediction = model.predict_by_filepath(filepath="local_filepath", output_config={"min_value": 0.98}) # Supports image, text, audio, video
  15. # Supports prediction by url
  16. model_prediction = model.predict_by_url(url="url") # Supports image, text, audio, video
  17. # Return predictions for specified interval of video
  18. video_input_proto = [input_obj.get_input_from_url("Input_id", video_url=BEER_VIDEO_URL)]
  19. model_prediction = model.predict(video_input_proto, output_config={"sample_ms": 2000})

Model Training

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.app import App
  3. from clarifai.client.model import Model
  4. """
  5. Create model with trainable model_type
  6. """
  7. app = App(user_id="user_id", app_id="app_id")
  8. model = app.create_model(model_id="model_id", model_type_id="visual-classifier")
  9. (or)
  10. model = Model('url')
  11. """
  12. List training templates for the model_type
  13. """
  14. templates = model.list_training_templates()
  15. print(templates)
  16. """
  17. Get parameters for the model.
  18. """
  19. params = model.get_params(template='classification_basemodel_v1', save_to='model_params.yaml')
  20. """
  21. Update the model params yaml and pass it to model.train()
  22. """
  23. model_version_id = model.train('model_params.yaml')
  24. """
  25. Training status and saving logs
  26. """
  27. status = model.training_status(version_id=model_version_id,training_logs=True)
  28. print(status)

Export your trained model

Model Export feature enables you to package your trained model into a model.tar file. This file enables deploying your model within a Triton Inference Server deployment.

  1. from clarifai.client.model import Model
  2. model = Model('url')
  3. model.export('output/folder/')

Evaluate your trained model

When your model is trained and ready, you can evaluate by the following code

  1. from clarifai.client.model import Model
  2. model = Model('url')
  3. model.evaluate(dataset_id='your-dataset-id')

Compare the evaluation results of your models.

  1. from clarifai.client.model import Model
  2. from clarifai.client.dataset import Dataset
  3. from clarifai.utils.evaluation import EvalResultCompare
  4. models = ['model url1', 'model url2'] # or [Model(url1), Model(url2)]
  5. dataset = 'dataset url' # or Dataset(dataset_url)
  6. compare = EvalResultCompare(
  7. models=models,
  8. datasets=dataset,
  9. attempt_evaluate=True # attempt evaluate when the model is not evaluated with the dataset
  10. )
  11. compare.all('output/folder/')

Models Listing

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. # List all model versions
  3. all_model_versions = list(model.list_versions())
  4. # Go to specific model version
  5. model_v1 = client.app("app_id").model(model_id="model_id", model_version_id="model_version_id")
  6. # List all models in an app
  7. all_models = list(app.list_models())
  8. # List all models in community filtered by model_type, description
  9. all_llm_community_models = App().list_models(filter_by={"query": "LLM",
  10. "model_type_id": "text-to-text"}, only_in_app=False)
  11. all_llm_community_models = list(all_llm_community_models)

:fire: Interacting with Workflows

Workflows offer a versatile framework for constructing the inference pipeline, simplifying the integration of diverse models. You can use the Workflow class to create and manage workflows using YAML configuration.
For starting or making quick adjustments to existing Clarifai community workflows using an initial YAML configuration, the SDK provides an export feature.

Workflow Predict

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.workflow import Workflow
  3. # Workflow Predict
  4. workflow = Workflow("workflow_url") # Example: https://clarifai.com/clarifai/main/workflows/Face-Sentiment
  5. workflow_prediction = workflow.predict_by_url(url="url") # Supports image, text, audio, video
  6. # Customizing Workflow Inference Output
  7. workflow = Workflow(user_id="user_id", app_id="app_id", workflow_id="workflow_id",
  8. output_config={"min_value": 0.98}) # Return predictions having prediction confidence > 0.98
  9. workflow_prediction = workflow.predict_by_filepath(filepath="local_filepath") # Supports image, text, audio, video

Workflows Listing

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. # List all workflow versions
  3. all_workflow_versions = list(workflow.list_versions())
  4. # Go to specific workflow version
  5. workflow_v1 = Workflow(workflow_id="workflow_id", workflow_version=dict(id="workflow_version_id"), app_id="app_id", user_id="user_id")
  6. # List all workflow in an app
  7. all_workflow = list(app.list_workflow())
  8. # List all workflow in community filtered by description
  9. all_face_community_workflows = App().list_workflows(filter_by={"query": "face"}, only_in_app=False) # Get all face related workflows
  10. all_face_community_workflows = list(all_face_community_workflows)

Workflow Create

Create a new workflow specified by a yaml config file.

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.app import App
  3. app = App(app_id="app_id", user_id="user_id")
  4. workflow = app.create_workflow(config_filepath="config.yml")

Workflow Export

Export an existing workflow from Clarifai as a local yaml file.

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.workflow import Workflow
  3. workflow = Workflow("https://clarifai.com/clarifai/main/workflows/Demographics")
  4. workflow.export('demographics_workflow.yml')

Clarifai’s Smart Search feature leverages vector search capabilities to power the search experience. Vector search is a type of search engine that uses vectors to search and retrieve text, images, and videos.

Instead of traditional keyword-based search, where exact matches are sought, vector search allows for searching based on visual and/or semantic similarity by calculating distances between vector embedding representations of the data.

Here is an example of how to use vector search to find similar images:

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.search import Search
  3. search = Search(user_id="user_id", app_id="app_id", top_k=1, metric="cosine")
  4. # Search by image url
  5. results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}])
  6. for data in results:
  7. print(data.hits[0].input.data.image.url)

Smart Text Search is our proprietary feature that uses deep learning techniques to sort, rank, and retrieve text data based on their content and semantic similarity.

Here is an example of how to use Smart Text Search to find similar text:

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. # Search by text
  3. results = search.query(ranks=[{"text_raw": "I love my dog"}])

Filters

You can use filters to narrow down your search results. Filters can be used to filter by concepts, metadata, and Geo Point.

It is possible to add together multiple search parameters to expand your search. You can even combine negated search terms for more advanced tasks.

For example, you can combine two concepts as below.

  1. # query for images that contain concept "deer" or "dog"
  2. results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}],
  3. filters=[{"concepts": [{"name": "deer", "value":1},
  4. {"name": "dog", "value":1}]}])
  5. # query for images that contain concepts "deer" and "dog"
  6. results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}],
  7. filters=[{"concepts": [{"name": "deer", "value":1}],
  8. "concepts": [{"name": "dog", "value":1}]}])

Input filters allows to filter by input_type, status of inputs and by inputs_dataset_id

  1. results = search.query(filters=[{'input_types': ['image', 'text']}])

Pagination

Below is an example of using Search with Pagination.

  1. # Note: CLARIFAI_PAT must be set as env variable.
  2. from clarifai.client.search import Search
  3. search = Search(user_id="user_id", app_id="app_id", metric="cosine", pagination=True)
  4. # Search by image url
  5. results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}],page_no=2,per_page=5)
  6. for data in results:
  7. print(data.hits[0].input.data.image.url)

Retrieval Augmented Generation (RAG)

You can setup and start your RAG pipeline in 4 lines of code. The setup method automatically creates a new app and the necessary components under the hood. By default it uses the mistral-7B-Instruct model.

  1. from clarifai.rag import RAG
  2. rag_agent = RAG.setup(user_id="USER_ID")
  3. rag_agent.upload(folder_path="~/docs")
  4. rag_agent.chat(messages=[{"role":"human", "content":"What is Clarifai"}])

If you have previously run the setup method, you can instantiate the RAG class with the prompter workflow URL:

  1. from clarifai.rag import RAG
  2. rag_agent = RAG(workflow_url="WORKFLOW_URL")

:pushpin: More Examples

See many more code examples in this repo.
Also see the official Python SDK docs