artintel235 commited on
Commit
c18c359
·
verified ·
1 Parent(s): 79ef4e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -43
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import discord
2
  from discord import app_commands
3
- from discord.ui import Button, View
4
  import os
 
5
  import asyncio
6
- import aiohttp
7
- import gradio as gr
8
 
9
  # --- Environment Variables & Setup ---
10
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
@@ -19,7 +19,7 @@ intents = discord.Intents.default()
19
  client = discord.Client(intents=intents)
20
  tree = app_commands.CommandTree(client)
21
 
22
- # --- Image Generation Function ---
23
  async def generate_image_async(prompt, aspect_ratio):
24
  payload = {
25
  "id": "cm3ugmzv2002gnckiosrwk6xi",
@@ -46,7 +46,7 @@ async def generate_image_async(prompt, aspect_ratio):
46
  except aiohttp.ClientError as e:
47
  return f"API request failed: {e}"
48
 
49
- # --- Discord Command: /generate ---
50
  @tree.command(name="generate", description="Generates an image based on a text prompt")
51
  @app_commands.choices(
52
  aspect_ratio=[
@@ -68,31 +68,9 @@ async def generate_command(
68
  await interaction.response.defer()
69
 
70
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
71
-
72
  if image_url_or_error.startswith("http"):
73
- # Format the prompt for display with expand/collapse
74
- formatted_prompt, initial_content, full_content = format_prompt_for_discord(prompt)
75
-
76
- # Create buttons
77
- async def show_more_callback(interaction):
78
- await interaction.response.edit_message(content=f"{full_content}\n{image_url_or_error}", view=view)
79
-
80
- async def show_less_callback(interaction):
81
- await interaction.response.edit_message(content=f"{initial_content}\n{image_url_or_error}", view=view)
82
-
83
- show_more_button = Button(label="Read More", style=discord.ButtonStyle.primary)
84
- show_more_button.callback = show_more_callback
85
- show_less_button = Button(label="Hide", style=discord.ButtonStyle.secondary)
86
- show_less_button.callback = show_less_callback
87
-
88
- view = View()
89
- if len(prompt) > 256:
90
- view.add_item(show_more_button)
91
- view.add_item(show_less_button)
92
-
93
  await interaction.followup.send(
94
- f"{initial_content}\n{image_url_or_error}", # Include image URL initially
95
- view=view
96
  )
97
  else:
98
  await interaction.followup.send(
@@ -101,35 +79,25 @@ async def generate_command(
101
  except Exception as e:
102
  await interaction.followup.send(f"Error: {str(e)}")
103
 
104
- # --- Helper Function: Format Prompt for Discord ---
105
- def format_prompt_for_discord(prompt):
106
- """Formats the prompt for display in Discord with expand/collapse functionality using buttons."""
107
- max_length = 256
108
- if len(prompt) <= max_length:
109
- return prompt, f"**Prompt:** {prompt}", f"**Prompt:** {prompt}"
110
 
111
- truncated_prompt = prompt[:max_length]
112
- initial_content = f"**Prompt:** {truncated_prompt}..."
113
- full_content = f"**Prompt:** {prompt}"
114
-
115
- return prompt, initial_content, full_content
116
-
117
- # --- Discord Command: /hello ---
118
  @tree.command(name="hello", description="Says hello!")
119
- async def hello_command(interaction: discord.Interaction):
120
  await interaction.response.send_message("Hello there!")
121
 
122
- # --- Bot Ready Event ---
123
  async def on_ready():
124
  await tree.sync()
125
  print("Bot is ready!")
126
 
 
127
  client.event(on_ready)
128
 
 
129
  # --- Gradio Interface ---
130
  def echo_text(text):
131
  return text
132
 
 
133
  def run_gradio():
134
  gr.Interface(
135
  fn=echo_text,
@@ -139,6 +107,7 @@ def run_gradio():
139
  title="Minimal Gradio Interface",
140
  ).launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
141
 
 
142
  # --- Main ---
143
  async def main():
144
  bot_task = asyncio.create_task(client.start(DISCORD_BOT_TOKEN))
@@ -146,5 +115,6 @@ async def main():
146
 
147
  await asyncio.gather(bot_task, gradio_task)
148
 
 
149
  if __name__ == "__main__":
150
  asyncio.run(main())
 
1
  import discord
2
  from discord import app_commands
 
3
  import os
4
+ import requests
5
  import asyncio
6
+ import aiohttp # Use aiohttp for asynchronous HTTP requests
7
+ import gradio as gr # Import Gradio
8
 
9
  # --- Environment Variables & Setup ---
10
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
 
19
  client = discord.Client(intents=intents)
20
  tree = app_commands.CommandTree(client)
21
 
22
+
23
  async def generate_image_async(prompt, aspect_ratio):
24
  payload = {
25
  "id": "cm3ugmzv2002gnckiosrwk6xi",
 
46
  except aiohttp.ClientError as e:
47
  return f"API request failed: {e}"
48
 
49
+
50
  @tree.command(name="generate", description="Generates an image based on a text prompt")
51
  @app_commands.choices(
52
  aspect_ratio=[
 
68
  await interaction.response.defer()
69
 
70
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
 
71
  if image_url_or_error.startswith("http"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  await interaction.followup.send(
73
+ f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url_or_error}"
 
74
  )
75
  else:
76
  await interaction.followup.send(
 
79
  except Exception as e:
80
  await interaction.followup.send(f"Error: {str(e)}")
81
 
 
 
 
 
 
 
82
 
 
 
 
 
 
 
 
83
  @tree.command(name="hello", description="Says hello!")
84
+ async def hello_command(interaction):
85
  await interaction.response.send_message("Hello there!")
86
 
87
+
88
  async def on_ready():
89
  await tree.sync()
90
  print("Bot is ready!")
91
 
92
+
93
  client.event(on_ready)
94
 
95
+
96
  # --- Gradio Interface ---
97
  def echo_text(text):
98
  return text
99
 
100
+
101
  def run_gradio():
102
  gr.Interface(
103
  fn=echo_text,
 
107
  title="Minimal Gradio Interface",
108
  ).launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
109
 
110
+
111
  # --- Main ---
112
  async def main():
113
  bot_task = asyncio.create_task(client.start(DISCORD_BOT_TOKEN))
 
115
 
116
  await asyncio.gather(bot_task, gradio_task)
117
 
118
+
119
  if __name__ == "__main__":
120
  asyncio.run(main())