Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import discord
|
3 |
+
from discord.ext import commands
|
4 |
+
import requests
|
5 |
+
|
6 |
+
# Load the bot token and API key from environment variables
|
7 |
+
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
8 |
+
GLIF_API_KEY = os.getenv("GLIF_API_KEY")
|
9 |
+
GLIF_API_URL = "https://simple-api.glif.app"
|
10 |
+
|
11 |
+
# Create a bot instance with the command prefix '!'
|
12 |
+
intents = discord.Intents.default()
|
13 |
+
bot = commands.Bot(command_prefix="/imagine", intents=intents)
|
14 |
+
|
15 |
+
# Function to call the GLIF API
|
16 |
+
def generate_image(prompt):
|
17 |
+
aspect_ratio = "9:16" # Hardcoded aspect ratio
|
18 |
+
payload = {
|
19 |
+
"id": "cm3ugmzv2002gnckiosrwk6xi", # Your GLIF ID
|
20 |
+
"inputs": [prompt, aspect_ratio]
|
21 |
+
}
|
22 |
+
headers = {"Authorization": f"Bearer {GLIF_API_KEY}"}
|
23 |
+
|
24 |
+
try:
|
25 |
+
response = requests.post(GLIF_API_URL, json=payload, headers=headers)
|
26 |
+
if response.status_code == 200:
|
27 |
+
response_data = response.json()
|
28 |
+
if "output" in response_data:
|
29 |
+
return response_data["output"] # Image URL
|
30 |
+
elif "error" in response_data:
|
31 |
+
return f"Error: {response_data['error']}"
|
32 |
+
else:
|
33 |
+
return f"API request failed with status code: {response.status_code}"
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error: {str(e)}"
|
36 |
+
|
37 |
+
# Command to generate an image
|
38 |
+
@bot.command(name="generate")
|
39 |
+
async def generate(ctx, *, prompt: str):
|
40 |
+
await ctx.send(f"Generating an image for: `{prompt}`...")
|
41 |
+
image_url = generate_image(prompt)
|
42 |
+
|
43 |
+
if image_url.startswith("http"):
|
44 |
+
await ctx.send(image_url) # Send the image URL
|
45 |
+
else:
|
46 |
+
await ctx.send(f"Failed to generate image: {image_url}")
|
47 |
+
|
48 |
+
# Event: Bot is ready
|
49 |
+
@bot.event
|
50 |
+
async def on_ready():
|
51 |
+
print(f"Logged in as {bot.user}")
|
52 |
+
|
53 |
+
# Run the bot
|
54 |
+
bot.run(DISCORD_BOT_TOKEN)
|