Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,25 +5,55 @@ 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")
|
11 |
-
|
12 |
# --- Discord Bot Setup ---
|
13 |
intents = discord.Intents.default()
|
14 |
client = discord.Client(intents=intents)
|
15 |
tree = app_commands.CommandTree(client)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# --- Gradio Interface ---
|
29 |
def echo_text(text):
|
|
|
5 |
import asyncio
|
6 |
import aiohttp # Use aiohttp for asynchronous HTTP requests
|
7 |
import gradio as gr # Import Gradio
|
8 |
+
import google.generativeai as genai
|
9 |
+
from io import BytesIO
|
10 |
# --- Environment Variables & Setup ---
|
11 |
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
12 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
13 |
# --- Discord Bot Setup ---
|
14 |
intents = discord.Intents.default()
|
15 |
client = discord.Client(intents=intents)
|
16 |
tree = app_commands.CommandTree(client)
|
17 |
|
18 |
+
if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
|
19 |
+
raise ValueError("Both DISCORD_BOT_TOKEN and GEMINI_API_KEY must be set.")
|
20 |
+
|
21 |
+
|
22 |
+
@tree.command(name="imagen3", description="Generates iamge(s) using imagen3 model")
|
23 |
+
@app_commands.choices(
|
24 |
+
aspect_ratio=[
|
25 |
+
app_commands.Choice(name="1:1 (Square)", value="1:1"),
|
26 |
+
app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
|
27 |
+
app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
|
28 |
+
app_commands.Choice(name="3:4", value="3:4"),
|
29 |
+
app_commands.Choice(name="4:3", value="4:3"),
|
30 |
+
]
|
31 |
+
)
|
32 |
+
async def generate_command(
|
33 |
+
interaction: discord.Interaction,
|
34 |
+
prompt: str,
|
35 |
+
img_count:int,
|
36 |
+
aspect_ratio: app_commands.Choice[str],
|
37 |
+
):
|
38 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
39 |
+
imagen = genai.ImageGenerationModel("imagen-3.0-generate-001")
|
40 |
+
|
41 |
+
result = imagen.generate_images(
|
42 |
+
prompt=prompt,
|
43 |
+
number_of_images=img_count,
|
44 |
+
safety_filter_level="block_only_high",
|
45 |
+
person_generation="allow_adult",
|
46 |
+
aspect_ratio=aspect_ratio,
|
47 |
+
)
|
48 |
+
files = []
|
49 |
+
for i, image in enumerate(result.generated_images):
|
50 |
+
# Assuming `image` is a byte array or binary stream of the image
|
51 |
+
image_file = BytesIO(image) # Convert to BytesIO
|
52 |
+
files.append(discord.File(fp=image_file, filename=f"image_{i+1}.png"))
|
53 |
+
|
54 |
+
# Send images to Discord
|
55 |
+
await interaction.followup.send(content=f"Here are your {img_count} images for prompt: `{prompt}`", files=files)
|
56 |
+
|
57 |
|
58 |
# --- Gradio Interface ---
|
59 |
def echo_text(text):
|