Skip to main content

G4F-like OpenAI compatible wrapper for Perchance AI generators

Project description

Perchancy 🚀

PyPI version License: MIT

Perchancy is a high-speed, OpenAI-compatible Python wrapper for Perchance AI generators. Generate text and images programmatically with zero manual browser management.


✨ Key Features

  • ⚡ Blazing Fast: Optimized DOM scanning for instant extraction.
  • 🤖 Auto Lifecycle: Browser closes automatically via atexit.
  • 🔄 Auto-Sync Chrome: Automatically downloads matching Chrome for Testing.
  • 📽️ Streaming Support: Real-time text generation.
  • 🌍 Auto-Translation: Built-in Google Translate support for prompt/response.
  • 🛡️ Per-Request Safety: Toggle disable_safety_settings individually for each call.

📦 Installation

pip install perchancy

🚀 Quick Start

Text Generation (Non-Streaming)

import perchancy

client = perchancy.Client(headless=True)

# Generate full text synchronously
response = client.chat.completions.create(
    model="ai-text-generator",
    messages=[{"role": "user", "content": "Write a 3-sentence horror story."}],
    stream=False,                 # Wait for the complete response
    translation="auto",           # Automatically translate back to your prompt's language
)

# Access content using standard OpenAI dictionary keys
content = response["choices"][0]["message"]["content"]

print(f"ID: {response['id']}")
print(f"Model: {response['model']}")
print("-" * 20)
print(content)

Text Generation (Streaming)

import perchancy

client = perchancy.Client(headless=True)

response = client.chat.completions.create(
    model="ai-text-generator",
    messages=[{"role": "user", "content": "Write a short sci-fi intro."}],
    stream=True,
    translation="auto",
)

for chunk in response:
    content = chunk["choices"][0]["delta"].get("content", "")
    if content:
        print(content, end="", flush=True)

Image Generation

import perchancy
import base64

client = perchancy.Client(headless=True)

response = client.images.generate(
    model="ai-text-to-image-generator",
    prompt="cyberpunk city, neon lights",
    num_images=2,
    disable_safety_settings=True
)

for i, img in enumerate(response.get("data", [])):
    with open(f"out_{i}.png", "wb") as f:
        f.write(base64.b64decode(img["url"]))

🛠 Advanced: Extra Params & Mappings

Perchance generators often have sliders, dropdowns, or input fields (like seed, guidanceScale, or negativePrompt). You can control these using extra_params.

1. How to find parameters?

  1. Open the generator in your browser.
  2. Right-click any UI element (slider, input, etc.) and click Inspect.
  3. Note the id, name, or data-name (e.g., id="seedInput").

2. Usage Example

client.images.generate(
    model="ai-text-to-image-generator",
    prompt="A magical forest",
    # Set values directly using element IDs
    extra_params={
        "guidanceScale": "7.5",
        "negativePrompt": "low quality, blurry"
    },
    # Use mappings to create aliases for complex selectors
    param_mappings={
        "quality": ["#resolution-dropdown-id", "[data-name='qualitySelect']"]
    },
    quality="high" # Now 'quality' works as a direct argument via **kwargs
)

🔐 Authentication (Optional)

from perchancy.auth import login_and_save_cookies
login_and_save_cookies()

Note:

  • Authentication is NOT required. The library works perfectly as a guest.
  • This is intended for advanced users to bypass guest limits.
  • Disclaimer: The auth module's functionality has not been verified as guest mode proved sufficient during development.

📖 Documentation Summary

  • Finding Models: The "model" is the slug in the URL: perchance.org/NAME.
  • Client Configuration:
    • headless (bool): Run in background (default: True).
    • debug (bool): Enable detailed status logs.
  • Custom Selectors: You can manually override input_selectors, button_selectors, or output_selectors in the create/generate methods if a specific generator has a non-standard layout.

⚠️ Disclaimer

Unofficial wrapper. Use for educational purposes only. Author is not responsible for any misuse or bans.

📝 License

MIT License.


Created with ❤️ by BlackCrowy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perchancy-0.0.5.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

perchancy-0.0.5-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file perchancy-0.0.5.tar.gz.

File metadata

  • Download URL: perchancy-0.0.5.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for perchancy-0.0.5.tar.gz
Algorithm Hash digest
SHA256 61731ec0f862546bf8096afc1d35240173b2168d4e844cfa115175e150063393
MD5 1c114c3fd0fe1245bb6149c258d7b5bb
BLAKE2b-256 0508a46fd1fefe1cdb261d8c71ec76c80999605df9c85df1e1336bb9de93a6cc

See more details on using hashes here.

File details

Details for the file perchancy-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: perchancy-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for perchancy-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ffd28c9ca27959b0e3d87c3c2f9b4d44791f7787a3778c545b501e0233487518
MD5 bedf55639a2a10fb063b6b0a825a91a8
BLAKE2b-256 98f8dcdde81e88af972cf6d3fd1a3154adb6ca5764430cd4859f3d77d64a14af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page