clarifai


Nameclarifai JSON
Version 10.2.1 PyPI version JSON
download
home_pagehttps://github.com/Clarifai/clarifai-python
SummaryClarifai Python SDK
upload_time2024-03-19 15:53:42
maintainer
docs_urlNone
authorClarifai
requires_python>=3.8
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements clarifai-grpc numpy tqdm tritonclient rich PyYAML schema Pillow inquirerpy tabulate
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h1 align="center">
  <a href="https://www.clarifai.com/"><img alt="Clarifai" title="Clarifai" src="https://upload.wikimedia.org/wikipedia/commons/b/bc/Clarifai_Logo_FC_Web.png"></a>
</h1>

<h2 align="center">
Clarifai Python SDK</a>
</h2>



<p align="center">
  <a href="https://discord.gg/M32V7a7a" target="_blank"> <img src="https://img.shields.io/discord/1145701543228735582" alt="Discord">
  </a>
  <a href="https://pypi.org/project/clarifai" target="_blank"> <img src="https://img.shields.io/pypi/dm/clarifai" alt="PyPI - Downloads">
  </a>
</p>




This is the official Python client for interacting with our powerful [API](https://docs.clarifai.com). 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](https://www.clarifai.com/) | [Schedule Demo](https://www.clarifai.com/company/schedule-demo) | [Signup for a Free Account](https://clarifai.com/signup) | [API Docs](https://docs.clarifai.com/) | [Clarifai Community](https://clarifai.com/explore) | [Python SDK Docs](https://docs.clarifai.com/python-sdk/api-reference) | [Examples](https://github.com/Clarifai/examples) | [Colab Notebooks](https://github.com/Clarifai/colab-notebooks) | [Discord](https://discord.gg/XAPE3Vtg)

Give the repo a star ⭐
---



## Table Of Contents

* **[Installation](#rocket-installation)**
* **[Getting Started](#memo-getting-started)**
* **[Interacting with Datasets](#floppy_disk-interacting-with-datasets)**
* **[Interacting with Inputs](#floppy_disk-interacting-with-inputs)**
  * [Input Upload](#input-upload)
  * [Input Listing](#input-listing)
* **[Interacting with Models](#brain-interacting-with-models)**
  * [Model Predict](#model-predict)
  * [Model Training](#model-training)
  * [Model Listing](#models-listing)
* **[Interacting with Workflows](#fire-interacting-with-workflows)**
  * [Workflow Predict](#workflow-predict)
  * [Workflow Listing](#workflows-listing)
  * [Workflow Create](#workflow-create)
  * [Workflow Export](#workflow-export)
* **[Search](#mag-search)**
  * [Smart Image Search](#smart-image-search)
  * [Smart Text Search](#smart-text-search)
  * [Filters](#filters)
* **[Retrieval Augmented Generation (RAG)](#retrieval-augmented-generation-rag)**
* **[More Examples](#pushpin-more-examples)**







## :rocket: Installation


Install from PyPi:

```bash
pip install -U clarifai
```

Install from Source:

```bash
git clone https://github.com/Clarifai/clarifai-python.git
cd clarifai-python
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
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:](https://docs.clarifai.com/clarifai-basics/authentication/personal-access-tokens/) ***Log into Portal &rarr; Profile Icon &rarr; Security Settings &rarr; Create Personal Access Token &rarr; Set the scopes &rarr; Confirm***

* 🔗 [Get User ID:](https://help.clarifai.com/hc/en-us/articles/4408131912727-How-do-I-find-my-user-id-app-id-and-PAT-) ***Log into Portal &rarr; Profile Icon &rarr; Account &rarr; Profile &rarr; User-ID***

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

Set PAT as environment variable through terminal:

```cmd
export CLARIFAI_PAT={your personal access token}
```

```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.user import User
client = User(user_id="user_id")

# Get all apps
apps_generator = client.list_apps()
apps = list(apps_generator)
```

OR <br>

PAT can be passed as constructor argument

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


## :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.

```python
# Note: CLARIFAI_PAT must be set as env variable.

# Create app and dataset
app = client.create_app(app_id="demo_app", base_workflow="Universal")
dataset = app.create_dataset(dataset_id="demo_dataset")

# execute data upload to Clarifai app dataset
from clarifai.datasets.upload.laoders.coco_detection import COCODetectionDataLoader
coco_dataloader = COCODetectionDataLoader("images_dir", "coco_annotation_filepath")
dataset.upload_dataset(dataloader=coco_dataloader, get_upload_status=True, log_warnings =True)


#Try upload and record the failed outputs in log file.
from clarifai.datasets.upload.utils import load_module_dataloader
cifar_dataloader = load_module_dataloader('./image_classification/cifar10')
dataset.upload_dataset(dataloader=cifar_dataloader, get_upload_status=True, log_warnings =True)

#Retry upload from logs for `upload_dataset`
dataset.retry_upload_from_logs(dataloader=cifar_dataloader, log_file_path='log_file.log',
                               retry_duplicates=False,
                               log_warnings=True)

#upload text from csv
dataset.upload_from_csv(csv_path='csv_path', input_type='text', csv_type='raw', labels=True)

#upload data from folder
dataset.upload_from_folder(folder_path='folder_path', input_type='text', labels=True)

# Export Dataset
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
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.user import User
app = User(user_id="user_id").app(app_id="app_id")
input_obj = app.inputs()

#input upload from url
input_obj.upload_from_url(input_id = 'demo', image_url='https://samples.clarifai.com/metro-north.jpg')

#input upload from filename
input_obj.upload_from_file(input_id = 'demo', video_file='demo.mp4')

# text upload
input_obj.upload_text(input_id = 'demo', raw_text = 'This is a test')
```

#### Input Listing
```python
#listing inputs
input_generator = input_obj.list_inputs(page_no=1,per_page=10,input_type='image')
inputs_list = list(input_generator)

#listing annotations
annotation_generator = input_obj.list_annotations(batch_input=inputs_list)
annotations_list = list(annotation_generator)

#listing concepts
all_concepts = list(app.list_concepts())
```

#### Input Download
```python
#listing inputs
input_generator = input_obj.list_inputs(page_no=1,per_page=1,input_type='image')
inputs_list = list(input_generator)

#downloading_inputs
input_bytes = input_obj.download_inputs(inputs_list)
with open('demo.jpg','wb') as f:
  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
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.model import Model

"""
Get Model information on details of model(description, usecases..etc) and info on training or
# other inference parameters(eg: temperature, top_k, max_tokens..etc for LLMs)
"""
gpt_4_model = Model("https://clarifai.com/openai/chat-completion/models/GPT-4")
print(gpt_4_model)


# Model Predict
model_prediction = Model("https://clarifai.com/anthropic/completion/models/claude-v2").predict_by_bytes(b"Write a tweet on future of AI", input_type="text")

# Customizing Model Inference Output
model_prediction = gpt_4_model.predict_by_bytes(b"Write a tweet on future of AI", "text", inference_params=dict(temperature=str(0.7), max_tokens=30))
# Return predictions having prediction confidence > 0.98
model_prediction = model.predict_by_filepath(filepath="local_filepath", input_type, output_config={"min_value": 0.98}) # Supports image, text, audio, video

# Supports prediction by url
model_prediction = model.predict_by_url(url="url", input_type) # Supports image, text, audio, video

# Return predictions for specified interval of video
video_input_proto = [input_obj.get_input_from_url("Input_id", video_url=BEER_VIDEO_URL)]
model_prediction = model.predict(video_input_proto, input_type="video", output_config={"sample_ms": 2000})
```
#### Model Training
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.app import App
from clarifai.client.model import Model

"""
Create model with trainable model_type
"""
app = App(user_id="user_id", app_id="app_id")
model = app.create_model(model_id="model_id", model_type_id="visual-classifier")
               (or)
model = Model('url')

"""
List training templates for the model_type
"""
templates = model.list_training_templates()
print(templates)

"""
Get parameters for the model.
"""
params = model.get_params(template='classification_basemodel_v1', save_to='model_params.yaml')

"""
Update the model params yaml and pass it to model.train()
"""
model_version_id = model.train('model_params.yaml')

"""
Training status and saving logs
"""
status = model.training_status(version_id=model_version_id,training_logs=True)
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.

```python
from clarifai.client.model import Model

model = Model('url')
model.export('output/folder/')
```

#### Evaluate your trained model

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

```python
from clarifai.client.model import Model

model = Model('url')
model.evaluate(dataset_id='your-dataset-id')
```

Compare the evaluation results of your models.

```python
from clarifai.client.model import Model
from clarifai.client.dataset import Dataset
from clarifai.utils.evaluation import EvalResultCompare

models = ['model url1', 'model url2'] # or [Model(url1), Model(url2)]
dataset = 'dataset url' # or Dataset(dataset_url)

compare = EvalResultCompare(
  models=models,
  datasets=dataset,
  attempt_evaluate=True # attempt evaluate when the model is not evaluated with the dataset
  )
compare.all('output/folder/')
```

#### Models Listing
```python
# Note: CLARIFAI_PAT must be set as env variable.

# List all model versions
all_model_versions = list(model.list_versions())

# Go to specific model version
model_v1 = client.app("app_id").model(model_id="model_id", model_version_id="model_version_id")

# List all models in an app
all_models = list(app.list_models())

# List all models in community filtered by model_type, description
all_llm_community_models = App().list_models(filter_by={"query": "LLM",
                                                        "model_type_id": "text-to-text"}, only_in_app=False)
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
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.workflow import Workflow

# Workflow Predict
workflow = Workflow("workflow_url") # Example: https://clarifai.com/clarifai/main/workflows/Face-Sentiment
workflow_prediction = workflow.predict_by_url(url="url", input_type="image") # Supports image, text, audio, video

# Customizing Workflow Inference Output
workflow = Workflow(user_id="user_id", app_id="app_id", workflow_id="workflow_id",
                  output_config={"min_value": 0.98}) # Return predictions having prediction confidence > 0.98
workflow_prediction = workflow.predict_by_filepath(filepath="local_filepath", input_type="text") # Supports image, text, audio, video
```

#### Workflows Listing
```python
# Note: CLARIFAI_PAT must be set as env variable.

# List all workflow versions
all_workflow_versions = list(workflow.list_versions())

# Go to specific workflow version
workflow_v1 = Workflow(workflow_id="workflow_id", workflow_version=dict(id="workflow_version_id"), app_id="app_id", user_id="user_id")

# List all workflow in an app
all_workflow = list(app.list_workflow())

# List all workflow in community filtered by description
all_face_community_workflows = App().list_workflows(filter_by={"query": "face"}, only_in_app=False) # Get all face related workflows
all_face_community_workflows = list(all_face_community_workflows)
```
#### Workflow Create
Create a new workflow specified by a yaml config file.
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.app import App
app = App(app_id="app_id", user_id="user_id")
workflow = app.create_workflow(config_filepath="config.yml")
```

#### Workflow Export
Export an existing workflow from Clarifai as a local yaml file.
```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.workflow import Workflow
workflow = Workflow("https://clarifai.com/clarifai/main/workflows/Demographics")
workflow.export('demographics_workflow.yml')
```

## :mag: Search

#### Smart Image Search
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:

```python
# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.search import Search
search = Search(user_id="user_id", app_id="app_id", top_k=1, metric="cosine")

# Search by image url
results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}])

for data in results:
  print(data.hits[0].input.data.image.url)
```

#### Smart Text Search
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:

```python
# Note: CLARIFAI_PAT must be set as env variable.

# Search by text
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.

```python
# query for images that contain concept "deer" or "dog"
results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}],
                        filters=[{"concepts": [{"name": "deer", "value":1},
                                              {"name": "dog", "value":1}]}])

# query for images that contain concepts "deer" and "dog"
results = search.query(ranks=[{"image_url": "https://samples.clarifai.com/metro-north.jpg"}],
                        filters=[{"concepts": [{"name": "deer", "value":1}],
                                  "concepts": [{"name": "dog", "value":1}]}])
```

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

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

## 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](https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct) model.

```python
from clarifai.rag import RAG

rag_agent = RAG.setup(user_id="USER_ID")
rag_agent.upload(folder_path="~/docs")
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:

```python
from clarifai.rag import RAG

rag_agent = RAG(workflow_url="WORKFLOW_URL")
```

## :pushpin: More Examples

See many more code examples in this [repo](https://github.com/Clarifai/examples).
Also see the official [Python SDK docs](https://clarifai-python.readthedocs.io/en/latest/index.html)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Clarifai/clarifai-python",
    "name": "clarifai",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Clarifai",
    "author_email": "support@clarifai.com",
    "download_url": "https://files.pythonhosted.org/packages/00/91/757924a97f939948b484e18a65ffcf28783c89fcfc7ea4389234c5f4106f/clarifai-10.2.1.tar.gz",
    "platform": null,
    "description": "<h1 align=\"center\">\n  <a href=\"https://www.clarifai.com/\"><img alt=\"Clarifai\" title=\"Clarifai\" src=\"https://upload.wikimedia.org/wikipedia/commons/b/bc/Clarifai_Logo_FC_Web.png\"></a>\n</h1>\n\n<h2 align=\"center\">\nClarifai Python SDK</a>\n</h2>\n\n\n\n<p align=\"center\">\n  <a href=\"https://discord.gg/M32V7a7a\" target=\"_blank\"> <img src=\"https://img.shields.io/discord/1145701543228735582\" alt=\"Discord\">\n  </a>\n  <a href=\"https://pypi.org/project/clarifai\" target=\"_blank\"> <img src=\"https://img.shields.io/pypi/dm/clarifai\" alt=\"PyPI - Downloads\">\n  </a>\n</p>\n\n\n\n\nThis is the official Python client for interacting with our powerful [API](https://docs.clarifai.com). 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.\n\n[Website](https://www.clarifai.com/) | [Schedule Demo](https://www.clarifai.com/company/schedule-demo) | [Signup for a Free Account](https://clarifai.com/signup) | [API Docs](https://docs.clarifai.com/) | [Clarifai Community](https://clarifai.com/explore) | [Python SDK Docs](https://docs.clarifai.com/python-sdk/api-reference) | [Examples](https://github.com/Clarifai/examples) | [Colab Notebooks](https://github.com/Clarifai/colab-notebooks) | [Discord](https://discord.gg/XAPE3Vtg)\n\nGive the repo a star \u2b50\n---\n\n\n\n## Table Of Contents\n\n* **[Installation](#rocket-installation)**\n* **[Getting Started](#memo-getting-started)**\n* **[Interacting with Datasets](#floppy_disk-interacting-with-datasets)**\n* **[Interacting with Inputs](#floppy_disk-interacting-with-inputs)**\n  * [Input Upload](#input-upload)\n  * [Input Listing](#input-listing)\n* **[Interacting with Models](#brain-interacting-with-models)**\n  * [Model Predict](#model-predict)\n  * [Model Training](#model-training)\n  * [Model Listing](#models-listing)\n* **[Interacting with Workflows](#fire-interacting-with-workflows)**\n  * [Workflow Predict](#workflow-predict)\n  * [Workflow Listing](#workflows-listing)\n  * [Workflow Create](#workflow-create)\n  * [Workflow Export](#workflow-export)\n* **[Search](#mag-search)**\n  * [Smart Image Search](#smart-image-search)\n  * [Smart Text Search](#smart-text-search)\n  * [Filters](#filters)\n* **[Retrieval Augmented Generation (RAG)](#retrieval-augmented-generation-rag)**\n* **[More Examples](#pushpin-more-examples)**\n\n\n\n\n\n\n\n## :rocket: Installation\n\n\nInstall from PyPi:\n\n```bash\npip install -U clarifai\n```\n\nInstall from Source:\n\n```bash\ngit clone https://github.com/Clarifai/clarifai-python.git\ncd clarifai-python\npython3 -m venv .venv\nsource .venv/bin/activate\npip install -r requirements.txt\npython setup.py install\n```\n\n\n\n## :memo: Getting started\nClarifai uses **Personal Access Tokens(PATs)** to validate requests. You can create and manage PATs under your Clarifai account security settings.\n\n* \ud83d\udd17 [Create PAT:](https://docs.clarifai.com/clarifai-basics/authentication/personal-access-tokens/) ***Log into Portal &rarr; Profile Icon &rarr; Security Settings &rarr; Create Personal Access Token &rarr; Set the scopes &rarr; Confirm***\n\n* \ud83d\udd17 [Get User ID:](https://help.clarifai.com/hc/en-us/articles/4408131912727-How-do-I-find-my-user-id-app-id-and-PAT-) ***Log into Portal &rarr; Profile Icon &rarr; Account &rarr; Profile &rarr; User-ID***\n\nExport your PAT as an environment variable. Then, import and initialize the API Client.\n\nSet PAT as environment variable through terminal:\n\n```cmd\nexport CLARIFAI_PAT={your personal access token}\n```\n\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.user import User\nclient = User(user_id=\"user_id\")\n\n# Get all apps\napps_generator = client.list_apps()\napps = list(apps_generator)\n```\n\nOR <br>\n\nPAT can be passed as constructor argument\n\n```python\nfrom clarifai.client.user import User\nclient = User(user_id=\"user_id\", pat=\"your personal access token\")\n```\n\n\n## :floppy_disk: Interacting with Datasets\n\nClarifai 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.\n\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\n\n# Create app and dataset\napp = client.create_app(app_id=\"demo_app\", base_workflow=\"Universal\")\ndataset = app.create_dataset(dataset_id=\"demo_dataset\")\n\n# execute data upload to Clarifai app dataset\nfrom clarifai.datasets.upload.laoders.coco_detection import COCODetectionDataLoader\ncoco_dataloader = COCODetectionDataLoader(\"images_dir\", \"coco_annotation_filepath\")\ndataset.upload_dataset(dataloader=coco_dataloader, get_upload_status=True, log_warnings =True)\n\n\n#Try upload and record the failed outputs in log file.\nfrom clarifai.datasets.upload.utils import load_module_dataloader\ncifar_dataloader = load_module_dataloader('./image_classification/cifar10')\ndataset.upload_dataset(dataloader=cifar_dataloader, get_upload_status=True, log_warnings =True)\n\n#Retry upload from logs for `upload_dataset`\ndataset.retry_upload_from_logs(dataloader=cifar_dataloader, log_file_path='log_file.log',\n                               retry_duplicates=False,\n                               log_warnings=True)\n\n#upload text from csv\ndataset.upload_from_csv(csv_path='csv_path', input_type='text', csv_type='raw', labels=True)\n\n#upload data from folder\ndataset.upload_from_folder(folder_path='folder_path', input_type='text', labels=True)\n\n# Export Dataset\ndataset.export(save_path='output.zip')\n```\n\n\n\n## :floppy_disk: Interacting with Inputs\n\nYou 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.\n\n#### Input Upload\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.user import User\napp = User(user_id=\"user_id\").app(app_id=\"app_id\")\ninput_obj = app.inputs()\n\n#input upload from url\ninput_obj.upload_from_url(input_id = 'demo', image_url='https://samples.clarifai.com/metro-north.jpg')\n\n#input upload from filename\ninput_obj.upload_from_file(input_id = 'demo', video_file='demo.mp4')\n\n# text upload\ninput_obj.upload_text(input_id = 'demo', raw_text = 'This is a test')\n```\n\n#### Input Listing\n```python\n#listing inputs\ninput_generator = input_obj.list_inputs(page_no=1,per_page=10,input_type='image')\ninputs_list = list(input_generator)\n\n#listing annotations\nannotation_generator = input_obj.list_annotations(batch_input=inputs_list)\nannotations_list = list(annotation_generator)\n\n#listing concepts\nall_concepts = list(app.list_concepts())\n```\n\n#### Input Download\n```python\n#listing inputs\ninput_generator = input_obj.list_inputs(page_no=1,per_page=1,input_type='image')\ninputs_list = list(input_generator)\n\n#downloading_inputs\ninput_bytes = input_obj.download_inputs(inputs_list)\nwith open('demo.jpg','wb') as f:\n  f.write(input_bytes[0])\n```\n\n\n## :brain: Interacting with Models\n\nThe **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.\nFor greater control over model predictions, you can pass in an `output_config` to modify the model output as demonstrated below.\n#### Model Predict\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.model import Model\n\n\"\"\"\nGet Model information on details of model(description, usecases..etc) and info on training or\n# other inference parameters(eg: temperature, top_k, max_tokens..etc for LLMs)\n\"\"\"\ngpt_4_model = Model(\"https://clarifai.com/openai/chat-completion/models/GPT-4\")\nprint(gpt_4_model)\n\n\n# Model Predict\nmodel_prediction = Model(\"https://clarifai.com/anthropic/completion/models/claude-v2\").predict_by_bytes(b\"Write a tweet on future of AI\", input_type=\"text\")\n\n# Customizing Model Inference Output\nmodel_prediction = gpt_4_model.predict_by_bytes(b\"Write a tweet on future of AI\", \"text\", inference_params=dict(temperature=str(0.7), max_tokens=30))\n# Return predictions having prediction confidence > 0.98\nmodel_prediction = model.predict_by_filepath(filepath=\"local_filepath\", input_type, output_config={\"min_value\": 0.98}) # Supports image, text, audio, video\n\n# Supports prediction by url\nmodel_prediction = model.predict_by_url(url=\"url\", input_type) # Supports image, text, audio, video\n\n# Return predictions for specified interval of video\nvideo_input_proto = [input_obj.get_input_from_url(\"Input_id\", video_url=BEER_VIDEO_URL)]\nmodel_prediction = model.predict(video_input_proto, input_type=\"video\", output_config={\"sample_ms\": 2000})\n```\n#### Model Training\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.app import App\nfrom clarifai.client.model import Model\n\n\"\"\"\nCreate model with trainable model_type\n\"\"\"\napp = App(user_id=\"user_id\", app_id=\"app_id\")\nmodel = app.create_model(model_id=\"model_id\", model_type_id=\"visual-classifier\")\n               (or)\nmodel = Model('url')\n\n\"\"\"\nList training templates for the model_type\n\"\"\"\ntemplates = model.list_training_templates()\nprint(templates)\n\n\"\"\"\nGet parameters for the model.\n\"\"\"\nparams = model.get_params(template='classification_basemodel_v1', save_to='model_params.yaml')\n\n\"\"\"\nUpdate the model params yaml and pass it to model.train()\n\"\"\"\nmodel_version_id = model.train('model_params.yaml')\n\n\"\"\"\nTraining status and saving logs\n\"\"\"\nstatus = model.training_status(version_id=model_version_id,training_logs=True)\nprint(status)\n```\n\n#### Export your trained model\nModel 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.\n\n```python\nfrom clarifai.client.model import Model\n\nmodel = Model('url')\nmodel.export('output/folder/')\n```\n\n#### Evaluate your trained model\n\nWhen your model is trained and ready, you can evaluate by the following code\n\n```python\nfrom clarifai.client.model import Model\n\nmodel = Model('url')\nmodel.evaluate(dataset_id='your-dataset-id')\n```\n\nCompare the evaluation results of your models.\n\n```python\nfrom clarifai.client.model import Model\nfrom clarifai.client.dataset import Dataset\nfrom clarifai.utils.evaluation import EvalResultCompare\n\nmodels = ['model url1', 'model url2'] # or [Model(url1), Model(url2)]\ndataset = 'dataset url' # or Dataset(dataset_url)\n\ncompare = EvalResultCompare(\n  models=models,\n  datasets=dataset,\n  attempt_evaluate=True # attempt evaluate when the model is not evaluated with the dataset\n  )\ncompare.all('output/folder/')\n```\n\n#### Models Listing\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\n\n# List all model versions\nall_model_versions = list(model.list_versions())\n\n# Go to specific model version\nmodel_v1 = client.app(\"app_id\").model(model_id=\"model_id\", model_version_id=\"model_version_id\")\n\n# List all models in an app\nall_models = list(app.list_models())\n\n# List all models in community filtered by model_type, description\nall_llm_community_models = App().list_models(filter_by={\"query\": \"LLM\",\n                                                        \"model_type_id\": \"text-to-text\"}, only_in_app=False)\nall_llm_community_models = list(all_llm_community_models)\n```\n\n\n## :fire: Interacting with Workflows\n\nWorkflows 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.\nFor starting or making quick adjustments to existing Clarifai community workflows using an initial YAML configuration, the SDK provides an export feature.\n\n#### Workflow Predict\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.workflow import Workflow\n\n# Workflow Predict\nworkflow = Workflow(\"workflow_url\") # Example: https://clarifai.com/clarifai/main/workflows/Face-Sentiment\nworkflow_prediction = workflow.predict_by_url(url=\"url\", input_type=\"image\") # Supports image, text, audio, video\n\n# Customizing Workflow Inference Output\nworkflow = Workflow(user_id=\"user_id\", app_id=\"app_id\", workflow_id=\"workflow_id\",\n                  output_config={\"min_value\": 0.98}) # Return predictions having prediction confidence > 0.98\nworkflow_prediction = workflow.predict_by_filepath(filepath=\"local_filepath\", input_type=\"text\") # Supports image, text, audio, video\n```\n\n#### Workflows Listing\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\n\n# List all workflow versions\nall_workflow_versions = list(workflow.list_versions())\n\n# Go to specific workflow version\nworkflow_v1 = Workflow(workflow_id=\"workflow_id\", workflow_version=dict(id=\"workflow_version_id\"), app_id=\"app_id\", user_id=\"user_id\")\n\n# List all workflow in an app\nall_workflow = list(app.list_workflow())\n\n# List all workflow in community filtered by description\nall_face_community_workflows = App().list_workflows(filter_by={\"query\": \"face\"}, only_in_app=False) # Get all face related workflows\nall_face_community_workflows = list(all_face_community_workflows)\n```\n#### Workflow Create\nCreate a new workflow specified by a yaml config file.\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.app import App\napp = App(app_id=\"app_id\", user_id=\"user_id\")\nworkflow = app.create_workflow(config_filepath=\"config.yml\")\n```\n\n#### Workflow Export\nExport an existing workflow from Clarifai as a local yaml file.\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.workflow import Workflow\nworkflow = Workflow(\"https://clarifai.com/clarifai/main/workflows/Demographics\")\nworkflow.export('demographics_workflow.yml')\n```\n\n## :mag: Search\n\n#### Smart Image Search\nClarifai'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.\n\nInstead 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.\n\nHere is an example of how to use vector search to find similar images:\n\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\nfrom clarifai.client.search import Search\nsearch = Search(user_id=\"user_id\", app_id=\"app_id\", top_k=1, metric=\"cosine\")\n\n# Search by image url\nresults = search.query(ranks=[{\"image_url\": \"https://samples.clarifai.com/metro-north.jpg\"}])\n\nfor data in results:\n  print(data.hits[0].input.data.image.url)\n```\n\n#### Smart Text Search\nSmart 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.\n\nHere is an example of how to use Smart Text Search to find similar text:\n\n```python\n# Note: CLARIFAI_PAT must be set as env variable.\n\n# Search by text\nresults = search.query(ranks=[{\"text_raw\": \"I love my dog\"}])\n```\n\n#### Filters\n\nYou can use filters to narrow down your search results. Filters can be used to filter by concepts, metadata, and Geo Point.\n\nIt is possible to add together multiple search parameters to expand your search. You can even combine negated search terms for more advanced tasks.\n\nFor example, you can combine two concepts as below.\n\n```python\n# query for images that contain concept \"deer\" or \"dog\"\nresults = search.query(ranks=[{\"image_url\": \"https://samples.clarifai.com/metro-north.jpg\"}],\n                        filters=[{\"concepts\": [{\"name\": \"deer\", \"value\":1},\n                                              {\"name\": \"dog\", \"value\":1}]}])\n\n# query for images that contain concepts \"deer\" and \"dog\"\nresults = search.query(ranks=[{\"image_url\": \"https://samples.clarifai.com/metro-north.jpg\"}],\n                        filters=[{\"concepts\": [{\"name\": \"deer\", \"value\":1}],\n                                  \"concepts\": [{\"name\": \"dog\", \"value\":1}]}])\n```\n\nInput filters allows to filter by input_type, status of inputs and by inputs_dataset_id\n\n```python\nresults = search.query(filters=[{'input_types': ['image', 'text']}])\n```\n\n## Retrieval Augmented Generation (RAG)\n\nYou 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](https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct) model.\n\n```python\nfrom clarifai.rag import RAG\n\nrag_agent = RAG.setup(user_id=\"USER_ID\")\nrag_agent.upload(folder_path=\"~/docs\")\nrag_agent.chat(messages=[{\"role\":\"human\", \"content\":\"What is Clarifai\"}])\n```\n\nIf you have previously run the setup method, you can instantiate the RAG class with the prompter workflow URL:\n\n```python\nfrom clarifai.rag import RAG\n\nrag_agent = RAG(workflow_url=\"WORKFLOW_URL\")\n```\n\n## :pushpin: More Examples\n\nSee many more code examples in this [repo](https://github.com/Clarifai/examples).\nAlso see the official [Python SDK docs](https://clarifai-python.readthedocs.io/en/latest/index.html)\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "Clarifai Python SDK",
    "version": "10.2.1",
    "project_urls": {
        "Homepage": "https://github.com/Clarifai/clarifai-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "35d8899249b24aa9868565c346e01ac0225225fca0467aeb4ce3d720eda5f248",
                "md5": "661af83bf003eec05f04a8a12c52ddae",
                "sha256": "bccf2cb8f433e6122eeb46f5220e2f1f32a536f779faf6e74a4aeffe8cf93694"
            },
            "downloads": -1,
            "filename": "clarifai-10.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "661af83bf003eec05f04a8a12c52ddae",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 164007,
            "upload_time": "2024-03-19T15:53:40",
            "upload_time_iso_8601": "2024-03-19T15:53:40.910758Z",
            "url": "https://files.pythonhosted.org/packages/35/d8/899249b24aa9868565c346e01ac0225225fca0467aeb4ce3d720eda5f248/clarifai-10.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0091757924a97f939948b484e18a65ffcf28783c89fcfc7ea4389234c5f4106f",
                "md5": "41475bfb548b9319761b8c1da7e56e82",
                "sha256": "84683a766749b657d3bce716bf9abeab79df1fa182548736d58dfb1099b13991"
            },
            "downloads": -1,
            "filename": "clarifai-10.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "41475bfb548b9319761b8c1da7e56e82",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 134910,
            "upload_time": "2024-03-19T15:53:42",
            "upload_time_iso_8601": "2024-03-19T15:53:42.703459Z",
            "url": "https://files.pythonhosted.org/packages/00/91/757924a97f939948b484e18a65ffcf28783c89fcfc7ea4389234c5f4106f/clarifai-10.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-19 15:53:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Clarifai",
    "github_project": "clarifai-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "clarifai-grpc",
            "specs": [
                [
                    "~=",
                    "10.2.1"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    ">=",
                    "1.22.0"
                ]
            ]
        },
        {
            "name": "tqdm",
            "specs": [
                [
                    ">=",
                    "4.65.0"
                ]
            ]
        },
        {
            "name": "tritonclient",
            "specs": [
                [
                    ">=",
                    "2.34.0"
                ]
            ]
        },
        {
            "name": "rich",
            "specs": [
                [
                    ">=",
                    "13.4.2"
                ]
            ]
        },
        {
            "name": "PyYAML",
            "specs": [
                [
                    ">=",
                    "6.0.1"
                ]
            ]
        },
        {
            "name": "schema",
            "specs": [
                [
                    ">=",
                    "0.7.5"
                ]
            ]
        },
        {
            "name": "Pillow",
            "specs": [
                [
                    ">=",
                    "9.5.0"
                ]
            ]
        },
        {
            "name": "inquirerpy",
            "specs": [
                [
                    "==",
                    "0.3.4"
                ]
            ]
        },
        {
            "name": "tabulate",
            "specs": [
                [
                    ">=",
                    "0.9.0"
                ]
            ]
        }
    ],
    "lcname": "clarifai"
}
        
Elapsed time: 0.22624s