### Install FlyMyAI Python Client Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Installs the FlyMyAI Python client library using pip. This is the first step to using the client for AI model inference. Ensure you have Python 3.8+ installed. ```sh pip install flymyai ``` -------------------------------- ### Use M1 Agent (Synchronous Client) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Provides examples of using the M1 agent via the synchronous FlyMyAI client for text generation. It shows basic generation, generation with history context, and generation with an image input. Requires the 'flymyai' library. ```python from flymyai import m1_client client = m1_client(apikey="fly-secret-key") result = client.generate("An Iron Man") print(result.data.text, result.data.file_url) ``` ```python from flymyai import m1_client client = m1_client(apikey="fly-secret-key") result = client.generate("An Iron Man") print(result.data.text, result.data.file_url) result = client.generate("Add him Captain America's shield") print(result.data.text, result.data.file_url) ``` ```python from pathlib import Path from flymyai import m1_client client = m1_client(apikey="fly-secret-key") result = client.generate("An Iron Man", image=Path("./image.png")) print(result.data.text, result.data.file_url) ``` -------------------------------- ### Run Models in the Background with FlyMyAI Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Shows how to execute a FlyMyAI model, specifically for audio transcription, as a background task using asyncio. The example defines an async function for the background task and then runs it concurrently with other operations. Requires 'asyncio', 'flymyai', and 'pathlib'. ```python import asyncio import flymyai import pathlib async def background_task(): payload = {"audio": pathlib.Path("/path/to/audio.mp3")} response = await flymyai.async_run( apikey="fly-secret-key", model="flymyai/whisper", payload=payload ) print("Background task completed:", response.output_data["transcription"]) async def main(): task = asyncio.create_task(background_task()) await task asyncio.run(main()) # Continue with other operations while the model runs in the background ``` -------------------------------- ### Cancel Streaming Predictions Mid-Execution with Python Source: https://context7.com/flymyai/flymyai-python/llms.txt Demonstrates how to cancel a streaming prediction request before it completes. This is useful for stopping generation early, for example, after receiving a certain number of tokens. The example shows iterating through a stream and invoking the `cancel()` method on the iterator. ```python from flymyai import client, FlyMyAIPredictException fma_client = client(apikey="fly-secret-key") stream_iterator = fma_client.stream( payload={ "prompt": "Write a very long essay about artificial intelligence", "max_tokens": 10000, "temperature": 0.7, }, model="flymyai/llama-v3-1-8b" ) token_count = 0 max_tokens = 100 try: for response in stream_iterator: if response.output_data.get("output"): token = response.output_data["output"].pop() print(token, end="") token_count += 1 # Cancel after receiving 100 tokens if token_count >= max_tokens: print("\n\nCancelling stream...") cancel_response = stream_iterator.cancel() print(f"Cancelled: {cancel_response.status}") break except FlyMyAIPredictException as e: print(f"Error: {e}") finally: print(f"\nTotal tokens received: {token_count}") ``` -------------------------------- ### M1 Agent Chat (Async) with Python Source: https://context7.com/flymyai/flymyai-python/llms.txt Employs the asynchronous M1 client for conversational AI, enabling non-blocking operations for image processing and text generation. The examples demonstrate asynchronous generation using text prompts, image URLs, and local image files. ```python import asyncio from pathlib import Path from flymyai import async_m1_client async def main(): # Initialize async M1 client client = async_m1_client(apikey="fly-secret-key") # Generate with text prompt result = await client.generate("An Iron Man") print(f"Generated: {result.data.text}") print(f"File URL: {result.data.file_url}") # Generate with image URL result = await client.generate( "Make this cyberpunk style", image="https://example.com/image.jpg" ) print(f"Result: {result.data.text}") # Generate with local image file result = await client.generate( "Add dramatic lighting", image=Path("./photo.png") ) print(f"Enhanced: {result.data.text}") print(f"Output: {result.data.file_url}") asyncio.run(main()) ``` -------------------------------- ### M1 Agent Chat (Sync) with Python Source: https://context7.com/flymyai/flymyai-python/llms.txt Utilizes the synchronous M1 client for conversational AI, supporting text prompts and image inputs. This client maintains conversation history automatically. Examples show generating text and image responses, performing contextual follow-ups, and generating responses based on local image files. ```python from flymyai import m1_client from pathlib import Path # Initialize M1 client (maintains conversation history) client = m1_client(apikey="fly-secret-key") # Generate without image result = client.generate("An Iron Man") print(f"Text: {result.data.text}") print(f"Image URL: {result.data.file_url}") # Contextual follow-up (uses previous conversation) result = client.generate("Add him Captain America's shield") print(f"Updated text: {result.data.text}") print(f"Updated image: {result.data.file_url}") # Generate with local image input result = client.generate( "Make this image more futuristic", image=Path("./input_image.png") ) print(f"Result text: {result.data.text}") print(f"Result URL: {result.data.file_url}") ``` -------------------------------- ### Synchronous Streaming with FlyMy.AI Python Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Illustrates how to stream responses from language models token by token using synchronous iteration. This is beneficial for interactive applications where immediate feedback is desired. It shows how to initialize the client, set up the stream, and handle potential exceptions during the streaming process. ```python from flymyai import client, FlyMyAIPredictException fma_client = client(apikey="fly-secret-key") stream_iterator = fma_client.stream( payload={ "prompt": "tell me a story about christmas tree", "best_of": 12, "max_tokens": 1024, "stop": 1, "temperature": 1, "top_k": 1, "top_p": "0.95", }, model="flymyai/llama-v3-1-8b" ) try: for response in stream_iterator: if response.output_data.get("output"): print(response.output_data["output"].pop(), end="") except FlyMyAIPredictException as e: print(f"Error during streaming: {e}") raise e finally: print() print(f"Input tokens: {stream_iterator.stream_details.input_tokens}") print(f"Output tokens: {stream_iterator.stream_details.output_tokens}") print(f"Model size: {stream_iterator.stream_details.size_in_billions}B") ``` -------------------------------- ### Use M1 Agent (Asynchronous Client) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Demonstrates the usage of the M1 agent with the asynchronous FlyMyAI client using asyncio. It shows how to initialize the async client and perform a text generation request. Requires 'asyncio' and 'flymyai'. ```python import asyncio from flymyai import async_m1_client async def main(): client = async_m1_client(apikey="fly-secret-key") result = await client.generate("An Iron Man") print(result.data.text, result.data.file_url) asyncio.run(main()) ``` -------------------------------- ### Submit Asynchronous Prediction Tasks (Synchronous Client) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Demonstrates submitting an asynchronous prediction task using the synchronous FlyMyAI client. It shows how to initialize the client, submit a task, and retrieve the result, including handling potential timeouts or errors. Requires 'flymyai' and its exception classes. ```python from flymyai import client from flymyai.core.exceptions import ( RetryTimeoutExceededException, FlyMyAIExceptionGroup, ) # Initialize client fma_client = client(apikey="fly-secret-key") # Submit async prediction task prediction_task = fma_client.predict_async_task( model="flymyai/flux-schnell", payload={"prompt": "Funny Cat with Stupid Dog"} ) try: # Get result result = prediction_task.result() print(f"Prediction completed: {result.inference_responses}") except RetryTimeoutExceededException: print("Prediction is taking longer than expected") except FlyMyAIExceptionGroup as e: print(f"Prediction failed: {e}") ``` -------------------------------- ### Generate Text with Image Input using Flymyai Python SDK Source: https://github.com/flymyai/flymyai-python/blob/main/README.md This snippet shows how to use the Flymyai Python SDK to generate text by providing an image file as input. It requires the `asyncio` and `pathlib` libraries, along with the `flymyai` client. The function takes an API key and an image path, returning generated text and a file URL. Ensure the image file exists at the specified path. ```python import asyncio from pathlib import Path from flymyai import async_m1_client async def main(): client = async_m1_client(apikey="fly-secret-key") result = await client.generate("An Iron Man", image=Path("./image.png")) print(result.data.text, result.data.file_url) asyncio.run(main()) ``` -------------------------------- ### Synchronous Prediction with FlyMy.AI Python Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Demonstrates how to perform instant predictions using the synchronous `run` function or the client. This method is suitable for immediate results and simple API calls. It handles API key, model specification, and payload for prediction. ```python import flymyai # Simple prediction with run function response = flymyai.run( apikey="fly-secret-key", model="flymyai/bert", payload={"text": "What a fabulous fancy building! It looks like a palace!"} ) print(response.output_data["logits"][0]) print(f"Status: {response.status}") print(f"Inference time: {response.inference_time}s") # Using client for multiple predictions with flymyai.client(apikey="fly-secret-key", model="flymyai/bert") as client: response1 = client.predict({"text": "This movie is amazing!"}) response2 = client.predict({"text": "This movie is terrible."}) print(response1.output_data) print(response2.output_data) ``` -------------------------------- ### File Input Processing for Image and Audio Source: https://context7.com/flymyai/flymyai-python/llms.txt Demonstrates how to use the FlyMyAI SDK to process file inputs, specifically for image classification using ResNet and audio transcription using Whisper. Requires pathlib for path manipulation. ```python import pathlib import flymyai # Image classification with ResNet response = flymyai.run( apikey="fly-secret-key", model="flymyai/resnet", payload={"image": pathlib.Path("/path/to/image.png")} ) print(f"Classification result: {response.output_data['495']}") # Audio transcription with Whisper response = flymyai.run( apikey="fly-secret-key", model="flymyai/whisper", payload={"audio": pathlib.Path("/path/to/audio.mp3")} ) print(f"Transcription: {response.output_data['transcription']}") ``` -------------------------------- ### File Output Handling for Image Generation Source: https://context7.com/flymyai/flymyai-python/llms.txt Shows how to generate an image using Stable Diffusion Turbo and then decode and save the base64-encoded image output. Requires base64 for decoding. ```python import base64 import flymyai # Generate image with Stable Diffusion Turbo (ultra-fast ~50ms) response = flymyai.run( apikey="fly-secret-key", model="flymyai/SDTurboFMAAceleratedH100", payload={ "prompt": "An astronaut riding a rainbow unicorn, cinematic, dramatic, photorealistic", } ) # Decode and save the base64-encoded image base64_image = response.output_data["sample"][0] image_data = base64.b64decode(base64_image) with open("generated_image.jpg", "wb") as file: file.write(image_data) print(f"Image saved successfully. Status: {response.status}") ``` -------------------------------- ### Perform Asynchronous Requests with FlyMyAI Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Demonstrates how to perform multiple asynchronous requests to FlyMyAI models using asyncio.TaskGroup for improved performance. It processes a list of prompts and prints the output data from each result. Requires the 'asyncio' and 'flymyai' libraries. ```python import asyncio import flymyai async def main(): payloads = [ { "prompt": "An astronaut riding a rainbow unicorn, cinematic, dramatic, photorealistic", "negative_prompt": "Dark colors, gloomy atmosphere, horror", "seed": count, "denoising_steps": 4, "scheduler": "DPM++ SDE" } for count in range(1, 10) ] async with asyncio.TaskGroup() as gr: tasks = [ gr.create_task( flymyai.async_run( apikey="fly-secret-key", model="flymyai/DreamShaperV2-1", payload=payload ) ) for payload in payloads ] results = await asyncio.gather(*tasks) for result in results: print(result.output_data["output"]) asyncio.run(main()) ``` -------------------------------- ### Stable Code Instruct 3b LLM Streaming with FlyMyAI (Async) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Streams responses from the Stable Code Instruct 3b model using the asynchronous client. This is ideal for LLM applications requiring non-blocking I/O. Requires an API key and specific payload parameters. ```python import asyncio from flymyai import async_client, FlyMyAIPredictException async def run_stable_code(): fma_client = async_client(apikey="fly-secret-key") stream_iterator = fma_client.stream( payload={ "prompt": "What's the difference between an iterator and a generator in Python?", "best_of": 12, "max_tokens": 512, "stop": 1, "temperature": 1, "top_k": 1, "top_p": "0.95", }, model="flymyai/Stable-Code-Instruct-3b" ) try: async for response in stream_iterator: if response.output_data.get("output"): print(response.output_data["output"].pop(), end="") except FlyMyAIPredictException as e: print(e) raise e finally: print() print(stream_iterator.stream_details) asyncio.run(run_stable_code()) ``` -------------------------------- ### Submit Asynchronous Prediction Tasks (Asynchronous Client) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Illustrates submitting an asynchronous prediction task using the asynchronous FlyMyAI client with asyncio. It covers client initialization, task submission, awaiting the result, and checking the status of inference responses. Requires 'asyncio' and 'flymyai'. ```python import asyncio from flymyai import async_client from flymyai.core.exceptions import ( RetryTimeoutExceededException, FlyMyAIExceptionGroup, ) async def run_prediction(): # Initialize async client fma_client = async_client(apikey="fly-secret-key") # Submit async prediction task prediction_task = await fma_client.predict_async_task( model="flymyai/flux-schnell", payload={"prompt": "Funny Cat with Stupid Dog"} ) try: # Await result with default timeout result = await prediction_task.result() print(f"Prediction completed: {result.inference_responses}") # Check response status all_successful = all( resp.infer_details["status"] == 200 for resp in result.inference_responses ) print(f"All predictions successful: {all_successful}") except RetryTimeoutExceededException: print("Prediction is taking longer than expected") except FlyMyAIExceptionGroup as e: print(f"Prediction failed: {e}") # Run async function asyncio.run(run_prediction()) ``` -------------------------------- ### Retrieve OpenAPI Schema with FlyMyAI Python Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Shows how to retrieve the OpenAPI schema for a deployed model using the FlyMyAI Python client. This enables understanding model inputs and outputs. It accesses schema details like paths and components. ```python from flymyai import client # Get OpenAPI schema for a model with client(apikey="fly-secret-key") as fma_client: schema_response = fma_client.openapi_schema( model="flymyai/bert", max_retries=3 ) print(f"Status: {schema_response.status}") print(f"Retry history: {len(schema_response.exc_history)} attempts") # Access schema components schema = schema_response.openapi_schema print(f"OpenAPI version: {schema.get('openapi')}") print(f"Paths: {list(schema.get('paths', {}).keys())}") print(f"Components: {list(schema.get('components', {}).keys())}") # Inspect model input schema if 'components' in schema: schemas = schema['components'].get('schemas', {}) for name, definition in schemas.items(): print(f"\nSchema: {name}") print(f"Properties: {definition.get('properties', {}).keys()}") ``` -------------------------------- ### Basic Error Handling with FlyMyAI Python Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Demonstrates how to handle potential exceptions during model prediction using a try-except block. It catches FlyMyAIPredictException and accesses error details like retry requirements and response status. ```python try: response = flymyai.run( apikey="fly-secret-key", model="flymyai/bert", payload={"text": "Test input"} ) print(response.output_data) except FlyMyAIPredictException as e: print(f"Prediction failed: {e}") print(f"Requires retry: {e.requires_retry}") print(f"Response status: {e.response.status_code}") ``` -------------------------------- ### ResNet Image Classification with File Input Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Performs image classification using a ResNet model by passing a file path as input. The 'flymyai.run' function accepts pathlib.Path objects for file inputs. Requires an API key. ```python import pathlib import flymyai response = flymyai.run( apikey="fly-secret-key", model="flymyai/resnet", payload={"image": pathlib.Path("/path/to/image.png")} ) print(response.output_data["495"]) ``` -------------------------------- ### Asynchronous Streaming with Stable Code Instruct Source: https://context7.com/flymyai/flymyai-python/llms.txt Streams responses asynchronously from the Stable Code Instruct model for non-blocking, token-by-token generation. Handles potential FlyMyAIPredictException errors during streaming and logs stream details. ```python import asyncio from flymyai import async_client, FlyMyAIPredictException async def run_stable_code(): fma_client = async_client(apikey="fly-secret-key") stream_iterator = fma_client.stream( payload={ "prompt": "What's the difference between an iterator and a generator in Python?", "best_of": 12, "max_tokens": 512, "stop": 1, "temperature": 1, "top_k": 1, "top_p": "0.95", }, model="flymyai/Stable-Code-Instruct-3b" ) try: async for response in stream_iterator: if response.output_data.get("output"): print(response.output_data["output"].pop(), end="") except FlyMyAIPredictException as e: print(f"Streaming error: {e}") raise e finally: print() print(f"Stream details: {stream_iterator.stream_details}") asyncio.run(run_stable_code()) ``` -------------------------------- ### StableDiffusion Turbo Image Generation and File Handling Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Generates an image using StableDiffusion Turbo and handles the base64 encoded file response. The output image data is decoded and saved to a local file. Requires an API key. ```python import base64 import flymyai response = flymyai.run( apikey="fly-secret-key", model="flymyai/SDTurboFMAAceleratedH100", payload={ "prompt": "An astronaut riding a rainbow unicorn, cinematic, dramatic, photorealistic", } ) base64_image = response.output_data["sample"][0] image_data = base64.b64decode(base64_image) with open("generated_image.jpg", "wb") as file: file.write(image_data) ``` -------------------------------- ### Error Handling with FlyMyAI Python SDK Source: https://context7.com/flymyai/flymyai-python/llms.txt Provides guidance on handling exceptions during API interactions with the FlyMyAI SDK. It highlights the use of `FlyMyAIPredictException` for specific prediction errors and `FlyMyAIExceptionGroup` for managing multiple errors, including custom retry logic. ```python import flymyai from flymyai import FlyMyAIPredictException, FlyMyAIExceptionGroup ``` -------------------------------- ### Llama 3.1 8B LLM Streaming with FlyMyAI (Sync) Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Streams responses from the Llama 3.1 8B model using the synchronous client. This is suitable for large language models where token-by-token output is desired. Requires an API key and specific payload parameters. ```python from flymyai import client, FlyMyAIPredictException fma_client = client(apikey="fly-secret-key") stream_iterator = fma_client.stream( payload={ "prompt": "tell me a story about christmas tree", "best_of": 12, "max_tokens": 1024, "stop": 1, "temperature": 1, "top_k": 1, "top_p": "0.95", }, model="flymyai/llama-v3-1-8b" ) try: for response in stream_iterator: if response.output_data.get("output"): print(response.output_data["output"].pop(), end="") except FlyMyAIPredictException as e: print(e) raise e finally: print() print(stream_iterator.stream_details) ``` -------------------------------- ### Async Task Submission with Sync Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Demonstrates submitting long-running prediction tasks using the synchronous FlyMyAI client and then polling for results. Handles potential RetryTimeoutExceededException and FlyMyAIExceptionGroup errors. ```python from flymyai import client from flymyai.core.exceptions import ( RetryTimeoutExceededException, FlyMyAIExceptionGroup, ) # Initialize client fma_client = client(apikey="fly-secret-key") # Submit async prediction task prediction_task = fma_client.predict_async_task( model="flymyai/flux-schnell", payload={"prompt": "Funny Cat with Stupid Dog"} ) print(f"Task submitted with ID: {prediction_task.prediction_id}") try: # Block and wait for result (default timeout) result = prediction_task.result() # Check all responses for response in result.inference_responses: print(f"Status: {response.status}") print(f"Output: {response.output_data}") # Verify all predictions succeeded all_successful = all( resp.infer_details["status"] == 200 for resp in result.inference_responses ) print(f"All predictions successful: {all_successful}") except RetryTimeoutExceededException: print("Prediction is taking longer than expected") except FlyMyAIExceptionGroup as e: print(f"Prediction failed with errors: {e.errors}") for error in e.fma_errors(): print(f" - {error.msg}") ``` -------------------------------- ### Submit Async Prediction Tasks with Python Source: https://context7.com/flymyai/flymyai-python/llms.txt Submits long-running prediction tasks asynchronously and awaits their results. Initializes an async client, submits a task with a specified model and payload, and then waits for the prediction to complete within a timeout. Includes error handling for timeouts and general exceptions. ```python import asyncio from flymyai import async_client from flymyai.core.exceptions import ( RetryTimeoutExceededException, FlyMyAIExceptionGroup, ) async def run_prediction(): # Initialize async client fma_client = async_client(apikey="fly-secret-key") # Submit async prediction task prediction_task = await fma_client.predict_async_task( model="flymyai/flux-schnell", payload={"prompt": "Funny Cat with Stupid Dog"} ) print(f"Task ID: {prediction_task.prediction_id}") try: # Await result with custom timeout (60 seconds) result = await prediction_task.result(timeout=60.0) print(f"Prediction completed: {len(result.inference_responses)} responses") for idx, resp in enumerate(result.inference_responses): print(f"Response {idx}: Status {resp.status}") print(f" Data: {resp.output_data}") # Check response status all_successful = all( resp.infer_details["status"] == 200 for resp in result.inference_responses ) print(f"All predictions successful: {all_successful}") except RetryTimeoutExceededException: print("Prediction timeout exceeded") except FlyMyAIExceptionGroup as e: print(f"Prediction failed: {e.message}") print(f"FMA-specific errors: {len(e.fma_errors())}") print(f"Other errors: {len(e.non_fma_errors())}") asyncio.run(run_prediction()) ``` -------------------------------- ### Asynchronous Prediction with FlyMy.AI Python Client Source: https://context7.com/flymyai/flymyai-python/llms.txt Shows how to execute predictions asynchronously for improved performance in async applications. This is useful for handling multiple requests concurrently without blocking the event loop. It supports both single and multiple concurrent predictions using `asyncio`. ```python import asyncio import flymyai async def main(): # Single async prediction response = await flymyai.async_run( apikey="fly-secret-key", model="flymyai/bert", payload={"text": "I love this product!"} ) print(response.output_data) # Multiple concurrent predictions payloads = [ { "prompt": "An astronaut riding a rainbow unicorn, cinematic, dramatic", "negative_prompt": "Dark colors, gloomy atmosphere", "seed": count, "denoising_steps": 4, "scheduler": "DPM++ SDE" } for count in range(1, 10) ] async with asyncio.TaskGroup() as gr: tasks = [ gr.create_task( flymyai.async_run( apikey="fly-secret-key", model="flymyai/DreamShaperV2-1", payload=payload ) ) for payload in payloads ] results = await asyncio.gather(*tasks) for result in results: print(f"Status: {result.status}, Output: {result.output_data['output']}") asyncio.run(main()) ``` -------------------------------- ### BERT Sentiment Analysis with FlyMyAI Source: https://github.com/flymyai/flymyai-python/blob/main/README.md Performs sentiment analysis using a BERT model via the FlyMyAI client. It takes a text payload and returns the logits. Requires an API key and the 'flymyai/bert' model. ```python import flymyai response = flymyai.run( apikey="fly-secret-key", model="flymyai/bert", payload={"text": "What a fabulous fancy building! It looks like a palace!"} ) print(response.output_data["logits"][0]) ``` -------------------------------- ### Custom Retry Logic for FlyMyAI Predictions Source: https://context7.com/flymyai/flymyai-python/llms.txt Illustrates custom retry logic for model predictions using a client context manager. It allows setting a maximum number of retries for specific errors and handles FlyMyAIExceptionGroup for aggregated errors. ```python with flymyai.client(apikey="fly-secret-key") as client: try: # Retry up to 5 times on retryable errors response = client.predict( payload={"text": "Test input"}, model="flymyai/bert", max_retries=5 ) print(f"Success after retries: {response.output_data}") print(f"Exception history: {len(response.exc_history)} errors") except FlyMyAIExceptionGroup as e: print(f"All retries failed: {e.message}") print(f"FMA errors: {len(e.fma_errors())}") print(f"Other errors: {len(e.non_fma_errors())}") for error in e.errors: print(f" - {error}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.