swarms-cloud


Nameswarms-cloud JSON
Version 0.2.5 PyPI version JSON
download
home_pagehttps://github.com/kyegomez/swarms-cloud
SummarySwarms Cloud - Pytorch
upload_time2024-04-16 21:55:28
maintainerNone
docs_urlNone
authorKye Gomez
requires_python<4.0,>=3.9
licenseMIT
keywords artificial intelligence deep learning optimizers prompt engineering
VCS
bugtrack_url
requirements skypilot fastapi supabase pytest torch einops tiktoken sse-starlette uvicorn loguru pydantic stripe swarms-cloud xformers diffusers transformers_stream_generator bitsandbytes peft accelerate transformers huggingface-hub optimum auto-gptq whisperx shortuuid exxa
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Multi-Modality](agorabanner.png)](https://discord.gg/qUtxnK2NMf)

# Swarms Cloud
Infrastructure for scalable, reliable, and economical Multi-Modal Model API serving and deployment. We're using terraform to orchestrate infrastructure, FastAPI to host the models. If you're into deploying models for millions of people, join our discord and help contribute.


# Install
`pip install swarms-cloud`

# Examples

### Example Python
```python
import requests
import base64
from PIL import Image
from io import BytesIO


# Convert image to Base64
def image_to_base64(image_path):
    with Image.open(image_path) as image:
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
    return img_str


# Replace 'image.jpg' with the path to your image
base64_image = image_to_base64("images/3897e80dcb0601c0.jpg")
text_data = {"type": "text", "text": "Describe what is in the image"}
image_data = {
    "type": "image_url",
    "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
}

# Construct the request data
request_data = {
    "model": "cogvlm-chat-17b",
    "messages": [{"role": "user", "content": [text_data, image_data]}],
    "temperature": 0.8,
    "top_p": 0.9,
    "max_tokens": 1024,
}

# Specify the URL of your FastAPI application
url = "https://api.swarms.world/v1/chat/completions"

# Send the request
response = requests.post(url, json=request_data)
# Print the response from the server
print(response.text)
```

### Example API Request in Node
```js
const fs = require('fs');
const https = require('https');
const sharp = require('sharp');

// Convert image to Base64
async function imageToBase64(imagePath) {
    try {
        const imageBuffer = await sharp(imagePath).jpeg().toBuffer();
        return imageBuffer.toString('base64');
    } catch (error) {
        console.error('Error converting image to Base64:', error);
    }
}

// Main function to execute the workflow
async function main() {
    const base64Image = await imageToBase64("images/3897e80dcb0601c0.jpg");
    const textData = { type: "text", text: "Describe what is in the image" };
    const imageData = {
        type: "image_url",
        image_url: { url: `data:image/jpeg;base64,${base64Image}` },
    };

    // Construct the request data
    const requestData = JSON.stringify({
        model: "cogvlm-chat-17b",
        messages: [{ role: "user", content: [textData, imageData] }],
        temperature: 0.8,
        top_p: 0.9,
        max_tokens: 1024,
    });

    const options = {
        hostname: 'api.swarms.world',
        path: '/v1/chat/completions',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': requestData.length,
        },
    };

    const req = https.request(options, (res) => {
        let responseBody = '';

        res.on('data', (chunk) => {
            responseBody += chunk;
        });

        res.on('end', () => {
            console.log('Response:', responseBody);
        });
    });

    req.on('error', (error) => {
        console.error(error);
    });

    req.write(requestData);
    req.end();
}

main();
```

### Example API Request in Go

```go
package main

import (
    "bytes"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "image"
    "image/jpeg"
    _ "image/png" // Register PNG format
    "io"
    "net/http"
    "os"
)

// imageToBase64 converts an image to a Base64-encoded string.
func imageToBase64(imagePath string) (string, error) {
    file, err := os.Open(imagePath)
    if err != nil {
        return "", err
    }
    defer file.Close()

    img, _, err := image.Decode(file)
    if err != nil {
        return "", err
    }

    buf := new(bytes.Buffer)
    err = jpeg.Encode(buf, img, nil)
    if err != nil {
        return "", err
    }

    return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}

// main is the entry point of the program.
func main() {
    base64Image, err := imageToBase64("images/3897e80dcb0601c0.jpg")
    if err != nil {
        fmt.Println("Error converting image to Base64:", err)
        return
    }

    requestData := map[string]interface{}{
        "model": "cogvlm-chat-17b",
        "messages": []map[string]interface{}{
            {
                "role":    "user",
                "content": []map[string]string{{"type": "text", "text": "Describe what is in the image"}, {"type": "image_url", "image_url": {"url": fmt.Sprintf("data:image/jpeg;base64,%s", base64Image)}}},
            },
        },
        "temperature": 0.8,
        "top_p":       0.9,
        "max_tokens":  1024,
    }

    requestBody, err := json.Marshal(requestData)
    if err != nil {
        fmt.Println("Error marshaling request data:", err)
        return
    }

    url := "https://api.swarms.world/v1/chat/completions"
    request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }

    request.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer response.Body.Close()

    responseBody, err := io.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }

    fmt.Println("Response:", string(responseBody))
}
```





## Calculate Pricing
```python
from transformers import AutoTokenizer
from swarms_cloud import calculate_pricing

# Initialize the tokenizer
tokenizer = AutoTokenizer.from_pretrained("gpt2")

# Define the example texts
texts = ["This is the first example text.", "This is the second example text."]

# Calculate pricing and retrieve the results
total_tokens, total_sentences, total_words, total_characters, total_paragraphs, cost = calculate_pricing(texts, tokenizer)

# Print the total tokens processed
print(f"Total tokens processed: {total_tokens}")

# Print the total cost
print(f"Total cost: ${cost:.5f}")
```


## Generate an API key
```python
from swarms_cloud.api_key_generator import generate_api_key

out = generate_api_key(prefix="sk", length=30)

print(out)

```

# Stack
- Backend: FastAPI
- Skypilot for container management
- Stripe for payment tracking
- Postresql for database
- TensorRT for inference
- Docker for cluster management
- Kubernetes for managing and autoscaling docker containers
- Terraform


# License
MIT

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kyegomez/swarms-cloud",
    "name": "swarms-cloud",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "artificial intelligence, deep learning, optimizers, Prompt Engineering",
    "author": "Kye Gomez",
    "author_email": "kye@apac.ai",
    "download_url": "https://files.pythonhosted.org/packages/50/a9/8e72bfd33d4aa24a0bce82c4fe4c9695f20e02ffe94d33b92ea4538604ec/swarms_cloud-0.2.5.tar.gz",
    "platform": null,
    "description": "[![Multi-Modality](agorabanner.png)](https://discord.gg/qUtxnK2NMf)\n\n# Swarms Cloud\nInfrastructure for scalable, reliable, and economical Multi-Modal Model API serving and deployment. We're using terraform to orchestrate infrastructure, FastAPI to host the models. If you're into deploying models for millions of people, join our discord and help contribute.\n\n\n# Install\n`pip install swarms-cloud`\n\n# Examples\n\n### Example Python\n```python\nimport requests\nimport base64\nfrom PIL import Image\nfrom io import BytesIO\n\n\n# Convert image to Base64\ndef image_to_base64(image_path):\n    with Image.open(image_path) as image:\n        buffered = BytesIO()\n        image.save(buffered, format=\"JPEG\")\n        img_str = base64.b64encode(buffered.getvalue()).decode(\"utf-8\")\n    return img_str\n\n\n# Replace 'image.jpg' with the path to your image\nbase64_image = image_to_base64(\"images/3897e80dcb0601c0.jpg\")\ntext_data = {\"type\": \"text\", \"text\": \"Describe what is in the image\"}\nimage_data = {\n    \"type\": \"image_url\",\n    \"image_url\": {\"url\": f\"data:image/jpeg;base64,{base64_image}\"},\n}\n\n# Construct the request data\nrequest_data = {\n    \"model\": \"cogvlm-chat-17b\",\n    \"messages\": [{\"role\": \"user\", \"content\": [text_data, image_data]}],\n    \"temperature\": 0.8,\n    \"top_p\": 0.9,\n    \"max_tokens\": 1024,\n}\n\n# Specify the URL of your FastAPI application\nurl = \"https://api.swarms.world/v1/chat/completions\"\n\n# Send the request\nresponse = requests.post(url, json=request_data)\n# Print the response from the server\nprint(response.text)\n```\n\n### Example API Request in Node\n```js\nconst fs = require('fs');\nconst https = require('https');\nconst sharp = require('sharp');\n\n// Convert image to Base64\nasync function imageToBase64(imagePath) {\n    try {\n        const imageBuffer = await sharp(imagePath).jpeg().toBuffer();\n        return imageBuffer.toString('base64');\n    } catch (error) {\n        console.error('Error converting image to Base64:', error);\n    }\n}\n\n// Main function to execute the workflow\nasync function main() {\n    const base64Image = await imageToBase64(\"images/3897e80dcb0601c0.jpg\");\n    const textData = { type: \"text\", text: \"Describe what is in the image\" };\n    const imageData = {\n        type: \"image_url\",\n        image_url: { url: `data:image/jpeg;base64,${base64Image}` },\n    };\n\n    // Construct the request data\n    const requestData = JSON.stringify({\n        model: \"cogvlm-chat-17b\",\n        messages: [{ role: \"user\", content: [textData, imageData] }],\n        temperature: 0.8,\n        top_p: 0.9,\n        max_tokens: 1024,\n    });\n\n    const options = {\n        hostname: 'api.swarms.world',\n        path: '/v1/chat/completions',\n        method: 'POST',\n        headers: {\n            'Content-Type': 'application/json',\n            'Content-Length': requestData.length,\n        },\n    };\n\n    const req = https.request(options, (res) => {\n        let responseBody = '';\n\n        res.on('data', (chunk) => {\n            responseBody += chunk;\n        });\n\n        res.on('end', () => {\n            console.log('Response:', responseBody);\n        });\n    });\n\n    req.on('error', (error) => {\n        console.error(error);\n    });\n\n    req.write(requestData);\n    req.end();\n}\n\nmain();\n```\n\n### Example API Request in Go\n\n```go\npackage main\n\nimport (\n    \"bytes\"\n    \"encoding/base64\"\n    \"encoding/json\"\n    \"fmt\"\n    \"image\"\n    \"image/jpeg\"\n    _ \"image/png\" // Register PNG format\n    \"io\"\n    \"net/http\"\n    \"os\"\n)\n\n// imageToBase64 converts an image to a Base64-encoded string.\nfunc imageToBase64(imagePath string) (string, error) {\n    file, err := os.Open(imagePath)\n    if err != nil {\n        return \"\", err\n    }\n    defer file.Close()\n\n    img, _, err := image.Decode(file)\n    if err != nil {\n        return \"\", err\n    }\n\n    buf := new(bytes.Buffer)\n    err = jpeg.Encode(buf, img, nil)\n    if err != nil {\n        return \"\", err\n    }\n\n    return base64.StdEncoding.EncodeToString(buf.Bytes()), nil\n}\n\n// main is the entry point of the program.\nfunc main() {\n    base64Image, err := imageToBase64(\"images/3897e80dcb0601c0.jpg\")\n    if err != nil {\n        fmt.Println(\"Error converting image to Base64:\", err)\n        return\n    }\n\n    requestData := map[string]interface{}{\n        \"model\": \"cogvlm-chat-17b\",\n        \"messages\": []map[string]interface{}{\n            {\n                \"role\":    \"user\",\n                \"content\": []map[string]string{{\"type\": \"text\", \"text\": \"Describe what is in the image\"}, {\"type\": \"image_url\", \"image_url\": {\"url\": fmt.Sprintf(\"data:image/jpeg;base64,%s\", base64Image)}}},\n            },\n        },\n        \"temperature\": 0.8,\n        \"top_p\":       0.9,\n        \"max_tokens\":  1024,\n    }\n\n    requestBody, err := json.Marshal(requestData)\n    if err != nil {\n        fmt.Println(\"Error marshaling request data:\", err)\n        return\n    }\n\n    url := \"https://api.swarms.world/v1/chat/completions\"\n    request, err := http.NewRequest(\"POST\", url, bytes.NewBuffer(requestBody))\n    if err != nil {\n        fmt.Println(\"Error creating request:\", err)\n        return\n    }\n\n    request.Header.Set(\"Content-Type\", \"application/json\")\n\n    client := &http.Client{}\n    response, err := client.Do(request)\n    if err != nil {\n        fmt.Println(\"Error sending request:\", err)\n        return\n    }\n    defer response.Body.Close()\n\n    responseBody, err := io.ReadAll(response.Body)\n    if err != nil {\n        fmt.Println(\"Error reading response body:\", err)\n        return\n    }\n\n    fmt.Println(\"Response:\", string(responseBody))\n}\n```\n\n\n\n\n\n## Calculate Pricing\n```python\nfrom transformers import AutoTokenizer\nfrom swarms_cloud import calculate_pricing\n\n# Initialize the tokenizer\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n\n# Define the example texts\ntexts = [\"This is the first example text.\", \"This is the second example text.\"]\n\n# Calculate pricing and retrieve the results\ntotal_tokens, total_sentences, total_words, total_characters, total_paragraphs, cost = calculate_pricing(texts, tokenizer)\n\n# Print the total tokens processed\nprint(f\"Total tokens processed: {total_tokens}\")\n\n# Print the total cost\nprint(f\"Total cost: ${cost:.5f}\")\n```\n\n\n## Generate an API key\n```python\nfrom swarms_cloud.api_key_generator import generate_api_key\n\nout = generate_api_key(prefix=\"sk\", length=30)\n\nprint(out)\n\n```\n\n# Stack\n- Backend: FastAPI\n- Skypilot for container management\n- Stripe for payment tracking\n- Postresql for database\n- TensorRT for inference\n- Docker for cluster management\n- Kubernetes for managing and autoscaling docker containers\n- Terraform\n\n\n# License\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Swarms Cloud - Pytorch",
    "version": "0.2.5",
    "project_urls": {
        "Documentation": "https://github.com/kyegomez/swarms-cloud",
        "Homepage": "https://github.com/kyegomez/swarms-cloud",
        "Repository": "https://github.com/kyegomez/swarms-cloud"
    },
    "split_keywords": [
        "artificial intelligence",
        " deep learning",
        " optimizers",
        " prompt engineering"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9b1eaefa4b0406ffa5eb457688b70656bbb525a166c9d31a73d333e0b7ed5b77",
                "md5": "6b23f4230f1866e2cdc98c65b0536593",
                "sha256": "641fa2e931e271be88514bcc06df05a858850b4b0f33c60a732025ef78dd36c7"
            },
            "downloads": -1,
            "filename": "swarms_cloud-0.2.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6b23f4230f1866e2cdc98c65b0536593",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 23751,
            "upload_time": "2024-04-16T21:55:27",
            "upload_time_iso_8601": "2024-04-16T21:55:27.035463Z",
            "url": "https://files.pythonhosted.org/packages/9b/1e/aefa4b0406ffa5eb457688b70656bbb525a166c9d31a73d333e0b7ed5b77/swarms_cloud-0.2.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50a98e72bfd33d4aa24a0bce82c4fe4c9695f20e02ffe94d33b92ea4538604ec",
                "md5": "373e3b43c3fc57788545b042b4cea389",
                "sha256": "aba66e768278d502c803f4e904fdf4287e10edda83b00c9ea81f59144541f5d7"
            },
            "downloads": -1,
            "filename": "swarms_cloud-0.2.5.tar.gz",
            "has_sig": false,
            "md5_digest": "373e3b43c3fc57788545b042b4cea389",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 20492,
            "upload_time": "2024-04-16T21:55:28",
            "upload_time_iso_8601": "2024-04-16T21:55:28.210526Z",
            "url": "https://files.pythonhosted.org/packages/50/a9/8e72bfd33d4aa24a0bce82c4fe4c9695f20e02ffe94d33b92ea4538604ec/swarms_cloud-0.2.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-16 21:55:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kyegomez",
    "github_project": "swarms-cloud",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "skypilot",
            "specs": []
        },
        {
            "name": "fastapi",
            "specs": [
                [
                    "==",
                    "0.110.0"
                ]
            ]
        },
        {
            "name": "supabase",
            "specs": []
        },
        {
            "name": "pytest",
            "specs": []
        },
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "einops",
            "specs": []
        },
        {
            "name": "tiktoken",
            "specs": []
        },
        {
            "name": "sse-starlette",
            "specs": [
                [
                    "==",
                    "2.0.0"
                ]
            ]
        },
        {
            "name": "uvicorn",
            "specs": []
        },
        {
            "name": "loguru",
            "specs": []
        },
        {
            "name": "pydantic",
            "specs": []
        },
        {
            "name": "stripe",
            "specs": []
        },
        {
            "name": "swarms-cloud",
            "specs": []
        },
        {
            "name": "xformers",
            "specs": []
        },
        {
            "name": "diffusers",
            "specs": []
        },
        {
            "name": "transformers_stream_generator",
            "specs": []
        },
        {
            "name": "bitsandbytes",
            "specs": []
        },
        {
            "name": "peft",
            "specs": []
        },
        {
            "name": "accelerate",
            "specs": []
        },
        {
            "name": "transformers",
            "specs": [
                [
                    "==",
                    "4.37.2"
                ]
            ]
        },
        {
            "name": "huggingface-hub",
            "specs": []
        },
        {
            "name": "optimum",
            "specs": []
        },
        {
            "name": "auto-gptq",
            "specs": []
        },
        {
            "name": "whisperx",
            "specs": []
        },
        {
            "name": "shortuuid",
            "specs": []
        },
        {
            "name": "exxa",
            "specs": []
        }
    ],
    "lcname": "swarms-cloud"
}
        
Elapsed time: 0.29851s