Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import discord
|
2 |
from discord import app_commands
|
|
|
3 |
import os
|
4 |
-
import requests
|
5 |
import asyncio
|
6 |
import aiohttp
|
7 |
import gradio as gr
|
@@ -19,6 +19,7 @@ intents = discord.Intents.default()
|
|
19 |
client = discord.Client(intents=intents)
|
20 |
tree = app_commands.CommandTree(client)
|
21 |
|
|
|
22 |
async def generate_image_async(prompt, aspect_ratio):
|
23 |
payload = {
|
24 |
"id": "cm3ugmzv2002gnckiosrwk6xi",
|
@@ -45,6 +46,7 @@ async def generate_image_async(prompt, aspect_ratio):
|
|
45 |
except aiohttp.ClientError as e:
|
46 |
return f"API request failed: {e}"
|
47 |
|
|
|
48 |
@tree.command(name="generate", description="Generates an image based on a text prompt")
|
49 |
@app_commands.choices(
|
50 |
aspect_ratio=[
|
@@ -66,12 +68,31 @@ async def generate_command(
|
|
66 |
await interaction.response.defer()
|
67 |
|
68 |
image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
|
|
|
69 |
if image_url_or_error.startswith("http"):
|
70 |
# Format the prompt for display with expand/collapse
|
71 |
-
formatted_prompt = format_prompt_for_discord(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
await interaction.followup.send(
|
74 |
-
f"
|
|
|
75 |
)
|
76 |
else:
|
77 |
await interaction.followup.send(
|
@@ -80,27 +101,27 @@ async def generate_command(
|
|
80 |
except Exception as e:
|
81 |
await interaction.followup.send(f"Error: {str(e)}")
|
82 |
|
|
|
83 |
def format_prompt_for_discord(prompt):
|
84 |
-
"""Formats the prompt for display in Discord with expand/collapse functionality using
|
85 |
-
max_length = 256
|
86 |
if len(prompt) <= max_length:
|
87 |
-
return f"**Prompt:** {prompt}"
|
88 |
|
89 |
truncated_prompt = prompt[:max_length]
|
90 |
hidden_part = prompt[max_length:]
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
f"<details><summary>Read More</summary>{hidden_part}</details>\n"
|
95 |
-
f"<details><summary>Hide</summary></details>"
|
96 |
-
)
|
97 |
|
98 |
-
return
|
99 |
|
|
|
100 |
@tree.command(name="hello", description="Says hello!")
|
101 |
-
async def hello_command(interaction):
|
102 |
await interaction.response.send_message("Hello there!")
|
103 |
|
|
|
104 |
async def on_ready():
|
105 |
await tree.sync()
|
106 |
print("Bot is ready!")
|
|
|
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
|
|
|
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 |
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 |
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=full_content, view=view)
|
79 |
+
|
80 |
+
async def show_less_callback(interaction):
|
81 |
+
await interaction.response.edit_message(content=initial_content, 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}",
|
95 |
+
view=view
|
96 |
)
|
97 |
else:
|
98 |
await interaction.followup.send(
|
|
|
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 |
hidden_part = prompt[max_length:]
|
113 |
|
114 |
+
initial_content = f"**Prompt:** {truncated_prompt}..."
|
115 |
+
full_content = f"**Prompt:** {prompt}"
|
|
|
|
|
|
|
116 |
|
117 |
+
return prompt, initial_content, full_content
|
118 |
|
119 |
+
# --- Discord Command: /hello ---
|
120 |
@tree.command(name="hello", description="Says hello!")
|
121 |
+
async def hello_command(interaction: discord.Interaction):
|
122 |
await interaction.response.send_message("Hello there!")
|
123 |
|
124 |
+
# --- Bot Ready Event ---
|
125 |
async def on_ready():
|
126 |
await tree.sync()
|
127 |
print("Bot is ready!")
|