Userapi.ai Ruby
Userapi.ai is an awesome tool that brings great value to products. This gem was created to facilitate Ruby and also Rails applications to use all services of Userapi.ai:
- Midjourney API
Installation
Add the following to your Gemfile:
gem "userapi-ai"
Or install with:
gem install userapi-ai
and require with:
require "userapi-ai"
Run bundle install
.
That's it. You are now ready to go!
Usage
Make sure you have a
- A Userapi.ai account at Userapi.ai
- Discord Account or create one at Discord
- A Midjourney account or sign up at Midjourney
- A Midjourney Paid plan to use this gem or sign up at Midjourney Plans
After signing in, you can get your Discord token by checking out any message request you send in the browser.
You can do this by opening the developer tools in your browser and going to the network tab.
Then send a message in any channel and look for the request to messages
in the network tab.
Check out the Request Headers and look for the authorization
header. Copy the value of the header and use it as your Discord User Tokens. Read more here.
Configuration
Once you have your Userapi.ai Token, you can configure the gem with:
Open up the initializer file config/initializers/userapi.rb
and add your API Key as the following:
UserapiAi.configure do |config|
config.access_token = ENV["USERAPI_USER_TOKEN"]
end
Imagine
Imagine is a Midjourney command that allows you to use AI to generate images. You can use it to generate images of people, animals, objects, and anything else you can imagine.
Basic Usage
Using the /imagine command
client = UserapiAi::Client.new(service: :midjourney)
result = client.imagine(prompt: "nice girl")
or
result = client.imagine(prompt: "nice girl",
webhook_url: "https://example.com/imagine/webhook-url",
webhook_type: "progress", # or result
account_hash: "a7d44910-88a6-4673-acc8-d60ffc3479a6",
is_disable_prefilter: false)
then
client.status(result)
Examples of other commands with parameters
client = UserapiAi::Client.new(service: :midjourney)
result = client.status(hash: "xxx-xxx")
result = client.variation(hash: "xxx-xxx", choice: 1)
result = client.upsample(hash: "xxx-xxx", choice: "v6_2x_subtle")
result = client.upscale(hash: "xxx-xxx",
choice: 1,
webhook_url: "https://example.com/upscale/webhook-url",
webhook_type: "result")
See more here.
Magick Tools
You can split the quad-layout output from Midjourney into 4 separate images.
Add the gem to your Gemfile:
gem "mini_magick"
Now you can do some magick.
response = client.status result
if response["status"] == "done"
local_paths = UserapiAi::Tool.crop result: response["result"]
# or UserapiAi::Tool.crop url: "https://example.com/img.png"
local_paths.each do |path|
f = File.open(path)
...
end
end
=> local_paths
["/var/folders/yb/bjdvl6mn3cx2l7nd08726k500000gn/T/mini_magick20240324-73984-31zmkb-0.png",
"/var/folders/yb/bjdvl6mn3cx2l7nd08726k500000gn/T/mini_magick20240324-73984-31zmkb-1.png",
"/var/folders/yb/bjdvl6mn3cx2l7nd08726k500000gn/T/mini_magick20240324-73984-31zmkb-2.png",
"/var/folders/yb/bjdvl6mn3cx2l7nd08726k500000gn/T/mini_magick20240324-73984-31zmkb-3.png"]
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run bundle exec rspec
to run the tests.
You can also run bin/console
for an interactive prompt that will allow you to experiment.
Contributing
Bug reports and pull requests are welcome on GitHub at Open an Issue.
License
The gem is available as open source under the terms of the MIT License. Feel free to use it and contribute.