Spaces:
Sleeping
Sleeping
File size: 1,911 Bytes
c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 c01a950 dda26c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import os
import requests
import discord
from discord.ext import commands
# GLIF Simple API endpoint
GLIF_API_URL = "https://simple-api.glif.app"
# Function to call the GLIF API
def generate_image(prompt):
# Hardcoded aspect ratio (9:16)
aspect_ratio = "9:16"
# Prepare the payload for the GLIF API
payload = {
"id": "cm3ugmzv2002gnckiosrwk6xi", # Glif ID
"inputs": [prompt, aspect_ratio] # Passing the prompt and the fixed aspect ratio
}
# Set up headers with the API token
headers = {
"Authorization": f"Bearer {os.getenv('GLIF_API_KEY')}"
}
# Make the request to the GLIF API
response = requests.post(GLIF_API_URL, json=payload, headers=headers)
if response.status_code == 200:
# Parse the response
response_data = response.json()
if "output" in response_data:
# Return the image URL
return response_data["output"]
elif "error" in response_data:
# Handle errors (even though the status code is 200)
return f"Error: {response_data['error']}"
else:
return f"API request failed with status code: {response.status_code}"
# Initialize the Discord bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command(name="generate")
async def generate(ctx, *, prompt: str):
"""Generate an image based on the provided prompt."""
await ctx.send(f"Generating image for prompt: {prompt}...")
# Call the GLIF API to generate the image
image_url = generate_image(prompt)
if image_url.startswith("http"):
await ctx.send(image_url)
else:
await ctx.send(f"Failed to generate image: {image_url}")
# Run the bot
bot.run(os.getenv('DISCORD_BOT_TOKEN')) |