artintel235 commited on
Commit
700b1d7
·
verified ·
1 Parent(s): bf5669e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -32
app.py CHANGED
@@ -1,26 +1,30 @@
 
 
1
  import os
 
2
  import requests
3
- import discord
4
- from discord.ext import commands
 
 
 
 
 
5
 
6
  # GLIF Simple API endpoint
7
  GLIF_API_URL = "https://simple-api.glif.app"
8
 
 
9
  # Function to call the GLIF API
10
- def generate_image(prompt):
11
- # Hardcoded aspect ratio (9:16)
12
- aspect_ratio = "9:16"
13
-
14
  # Prepare the payload for the GLIF API
15
  payload = {
16
  "id": "cm3ugmzv2002gnckiosrwk6xi", # Glif ID
17
- "inputs": [prompt, aspect_ratio] # Passing the prompt and the fixed aspect ratio
18
  }
19
 
20
  # Set up headers with the API token
21
- headers = {
22
- "Authorization": f"Bearer {os.getenv('GLIF_API_KEY')}"
23
- }
24
 
25
  # Make the request to the GLIF API
26
  response = requests.post(GLIF_API_URL, json=payload, headers=headers)
@@ -37,28 +41,55 @@ def generate_image(prompt):
37
  else:
38
  return f"API request failed with status code: {response.status_code}"
39
 
40
- # Initialize the Discord bot
41
- intents = discord.Intents.default()
42
- intents.message_content = True
43
- bot = commands.Bot(command_prefix="!", intents=intents)
44
 
45
- @bot.event
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  async def on_ready():
47
- print(f'Logged in as {bot.user.name}')
48
-
49
- @bot.command(name="generate")
50
- async def generate(ctx, *, prompt: str):
51
- """Generate an image based on the provided prompt."""
52
- await ctx.send(f"Generating image for prompt: {prompt}...")
53
-
54
- # Call the GLIF API to generate the image
55
- image_url = generate_image(prompt)
56
-
57
- if image_url.startswith("http"):
58
- await ctx.send(image_url)
59
- else:
60
- await ctx.send(f"Failed to generate image: {image_url}")
61
 
62
- # Run the bot
63
- print(os.getenv('DISCORD_BOT_TOKEN'))
64
- bot.run(os.getenv('DISCORD_BOT_TOKEN'))
 
 
 
1
+ import discord
2
+ from discord import app_commands
3
  import os
4
+ from dotenv import load_dotenv
5
  import requests
6
+
7
+ load_dotenv()
8
+
9
+ # Set up intents
10
+ intents = discord.Intents.default()
11
+ client = discord.Client(intents=intents)
12
+ tree = app_commands.CommandTree(client)
13
 
14
  # GLIF Simple API endpoint
15
  GLIF_API_URL = "https://simple-api.glif.app"
16
 
17
+
18
  # Function to call the GLIF API
19
+ def generate_image(prompt, aspect_ratio):
 
 
 
20
  # Prepare the payload for the GLIF API
21
  payload = {
22
  "id": "cm3ugmzv2002gnckiosrwk6xi", # Glif ID
23
+ "inputs": [prompt, aspect_ratio]
24
  }
25
 
26
  # Set up headers with the API token
27
+ headers = {"Authorization": f"Bearer {os.getenv('GLIF_API_TOKEN')}"}
 
 
28
 
29
  # Make the request to the GLIF API
30
  response = requests.post(GLIF_API_URL, json=payload, headers=headers)
 
41
  else:
42
  return f"API request failed with status code: {response.status_code}"
43
 
 
 
 
 
44
 
45
+ # Define a slash command for image generation
46
+ @tree.command(name="generate",
47
+ description="Generates an image based on a text prompt")
48
+ @app_commands.choices(aspect_ratio=[
49
+ app_commands.Choice(name="1:1 (Square)", value="1:1"),
50
+ app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
51
+ app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
52
+ app_commands.Choice(name="4:5 (Portrait)", value="4:5"),
53
+ app_commands.Choice(name="5:4 (Landscape)", value="5:4"),
54
+ app_commands.Choice(name="3:4 (Portrait)", value="3:4"),
55
+ app_commands.Choice(name="4:3 (Landscape)", value="4:3")
56
+ ])
57
+ async def generate_command(interaction: discord.Interaction, prompt: str,
58
+ aspect_ratio: app_commands.Choice[str]):
59
+ """Generates an image based on the user's prompt and aspect ratio."""
60
+ await interaction.response.defer() # Acknowledge the interaction
61
+
62
+ try:
63
+ image_url = generate_image(prompt, aspect_ratio.value)
64
+
65
+ if image_url.startswith("http"):
66
+ await interaction.followup.send(
67
+ f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url}"
68
+ )
69
+ else:
70
+ await interaction.followup.send(
71
+ f"Sorry, I couldn't generate an image. {image_url}")
72
+ except Exception as e:
73
+ await interaction.followup.send(f"An error occurred: {e}")
74
+
75
+
76
+ # Define the hello slash command
77
+ @tree.command(name="hello", description="Says hello!")
78
+ async def hello_command(interaction):
79
+ await interaction.response.send_message("Hello there!")
80
+
81
+
82
+ @client.event
83
  async def on_ready():
84
+ await tree.sync() # Sync the slash commands with discord
85
+ print("Bot is ready!")
86
+
87
+
88
+ # Get the token from an environment variable
89
+ token = os.getenv("DISCORD_BOT_TOKEN")
 
 
 
 
 
 
 
 
90
 
91
+ # check if the token is a string
92
+ if isinstance(token, str):
93
+ client.run(token)
94
+ else:
95
+ print("Invalid token format")