Skip to main content

A unified interface for text generation using Hugging Face, OpenAI, and Gemini models.

Project description

AnyGen: A Unified Interface for Text Generation

AnyGen is a minimal Python library that unifies text generation tasks using Hugging Face, OpenAI, and Gemini models. It offers a minimalistic and unified pipeline for loading models and generating outputs with ease and efficiency.

Features

  • Support for Hugging Face models
  • Support for OpenAI's GPT models
  • Support for Gemini models
  • Easy-to-use interface for text generation

Installation

Using pip

You can install AnyGen from PyPI:

pip install -U anygen

From Source

Clone the repository and install it manually:

git clone https://github.com/macabdul9/AnyGen.git
cd AnyGen
pip install .

Requirements

Ensure the following libraries are installed:

pip install transformers google-generativeai requests openai

Usage

Below are step-by-step instructions to generate text using each model type.

1. Hugging Face Model

from anygen import AnyGen

# Initialize the generator
model_name_or_path = "meta-llama/Llama-3.2-1B-Instruct"  # Replace with your Hugging Face model name
device = "cuda"  # Use "cpu" if GPU is not available
hf_generator = AnyGen(model_type="huggingface", model_name_or_path=model_name_or_path, device=device)

# Generate text
prompt = "Write python code for binary search"
generated_text = hf_generator.generate(prompt)
print(generated_text)

2. OpenAI Model

from anygen import AnyGen

# Initialize the generator
api_key_fp = "openai_keys.json"  # Path to your OpenAI credentials file
openai_generator = AnyGen(model_type="openai", api_key_fp=api_key_fp)

# Generate text
prompt = "Write python code for binary search"
generated_text = openai_generator.generate(prompt)
print(generated_text)

3. Gemini Model

from anygen import AnyGen

# Initialize the generator
api_key_fp = "gemini_keys.json"  # Path to your Gemini credentials file
gemini_generator = AnyGen(model_type="gemini", api_key_fp=api_key_fp)

# Generate text
prompt = "Write python code for binary search"
generated_text = gemini_generator.generate(prompt)
print(generated_text)

Example with Parameters

from anygen import AnyGen

# Initialize the generator
api_key_fp = "openai_keys.json"  # Example for OpenAI
openai_generator = AnyGen(model_type="openai", api_key_fp=api_key_fp)

# Generate text with parameters
prompt = "Write python code for binary search"
parameters = {"temperature": 0.7, "max_tokens": 512}
generated_text = openai_generator.generate(prompt, parameters)
print(generated_text)

API Key File Format

Both OpenAI and Gemini models require an API key stored in a JSON file. Below is an example format:

openai_keys.json:

{
    "openai_model_name": {
        "api_key": "your_openai_api_key",
        "endpoint": "your_endpoint"
    }
}

Replace openai_model_name with the OpenAI model name (e.g., gpt-4o-mini), api_key with your API key, and your_endpoint with the provided endpoint URL.

gemini_keys.json:

{
    "gemini_model_name": {
        "api_key": "your_gemini_api_key"
    }
}

Replace gemini_model_name with the Gemini model name (e.g., gemini-2.0-flash-exp), api_key with your Gemini API key.

Parameters

  • temperature: Controls the randomness of the output. Higher values produce more random results.
  • max_tokens: The maximum number of tokens to generate.
  • top_p: The cumulative probability of the top tokens to sample from.
  • top_k: The number of highest probability vocabulary tokens to keep for top-k-filtering.
  • beam_size: The number of beams to use for beam search.

Running Tests

You can run the tests using the following commands.

  1. Clone the repository:
git clone git@github.com:macabdul9/AnyGen.git
cd AnyGen
  1. Install the requirements:
pip install -r requirements.txt
  1. Run the tests:
python -m tests.test_anygen

Contributions

Feel free to submit issues and/or contribute to this repository!

License

This project is licensed under the MIT License.

Cite this Work

If you use AnyGen in your research or work, please cite it using the following BibTeX entry:

@software{anygen,
  author = {Abdul Waheed},
  title = {AnyGen: A Unified Interface for Text Generation},
  year = {2024},
  publisher = {GitHub},
  url = {https://github.com/macabdul9/AnyGen},
  doi = {https://doi.org/10.5281/zenodo.14533072}
}

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

anygen-1.0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

anygen-1.0.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file anygen-1.0.4.tar.gz.

File metadata

  • Download URL: anygen-1.0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for anygen-1.0.4.tar.gz
Algorithm Hash digest
SHA256 b1e27f1a15a91044eb41bd8c35500a88de8da2e3c894ea053057f359508df434
MD5 891e719eff516af3cfc11ed72c0f957b
BLAKE2b-256 f868fb5a9a4375dd729a86a1bb45a748e27715ca3a8f947817a9aca977ca3b15

See more details on using hashes here.

File details

Details for the file anygen-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: anygen-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for anygen-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f22882fe104e79fdd4cc0bd8a9b8b8fe0b5507738b37392bc8059bd8121b0826
MD5 a3e7b1343feecae6ba291c715149bd8f
BLAKE2b-256 46e4c31230143459f0371f3204439a4d2bcf33bca09f5e14a67835ec7fd5d12e

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