artintel235 commited on
Commit
5bd4d70
·
verified ·
1 Parent(s): e6440a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -45
app.py CHANGED
@@ -7,6 +7,7 @@ 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")
@@ -22,59 +23,30 @@ if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
22
  async def hello_command(interaction):
23
  await interaction.response.send_message("Hello there!")
24
 
25
- @tree.command(name="imagen3", description="Generates image(s) using imagen3 model")
26
- @app_commands.choices(
27
- aspect_ratio=[
28
- app_commands.Choice(name="1:1 (Square)", value="1:1"),
29
- app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
30
- app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
31
- app_commands.Choice(name="3:4", value="3:4"),
32
- app_commands.Choice(name="4:3", value="4:3"),
33
- ]
34
- )
35
  async def generate_command(
36
  interaction: discord.Interaction,
37
  prompt: str,
38
- img_count: int,
39
- aspect_ratio: app_commands.Choice[str],
40
  ):
41
  try:
42
  await interaction.response.defer() # Defer the interaction
43
  genai.configure(api_key=GEMINI_API_KEY)
44
- imagen = genai.ImageGenerationModel("imagen-3.0-generate-001")
45
-
46
- # Pass the value of the aspect_ratio
47
- result = imagen.generate_images(
48
- prompt=prompt,
49
- number_of_images=img_count,
50
- safety_filter_level="block_only_high",
51
- person_generation="allow_adult",
52
- aspect_ratio=aspect_ratio.value,
53
- )
54
-
55
- # Debugging result structure
56
- print(result)
57
-
58
- if result and result.generated_images:
59
- files = []
60
- for i, image in enumerate(result.generated_images):
61
- if isinstance(image, bytes): # Raw image bytes
62
- image_file = BytesIO(image)
63
- files.append(discord.File(fp=image_file, filename=f"image_{i+1}.png"))
64
- elif isinstance(image, str): # URL
65
- async with aiohttp.ClientSession() as session:
66
- async with session.get(image) as resp:
67
- if resp.status == 200:
68
- image_file = BytesIO(await resp.read())
69
- files.append(discord.File(fp=image_file, filename=f"image_{i+1}.png"))
70
-
71
- # Send all images
72
- await interaction.followup.send(content=f"Here are your {img_count} images for prompt: `{prompt}`", files=files)
73
- else:
74
- await interaction.followup.send(content="No images were generated.")
75
- except Exception as e:
76
- await interaction.followup.send(content=f"Exception: {e}")
77
 
 
78
  async def on_ready():
79
  await tree.sync()
80
  print("Bot is ready!")
 
7
  import gradio as gr # Import Gradio
8
  import google.generativeai as genai
9
  from io import BytesIO
10
+ import PIL.Image
11
  # --- Environment Variables & Setup ---
12
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
13
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
 
23
  async def hello_command(interaction):
24
  await interaction.response.send_message("Hello there!")
25
 
26
+ @tree.command(name="gemini", description="chat with gemini")
27
+ # @app_commands.choices(
28
+ # aspect_ratio=[
29
+ # app_commands.Choice(name="1:1 (Square)", value="1:1"),
30
+ # app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
31
+ # app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
32
+ # app_commands.Choice(name="3:4", value="3:4"),
33
+ # app_commands.Choice(name="4:3", value="4:3"),
34
+ # ]
35
+ # )
36
  async def generate_command(
37
  interaction: discord.Interaction,
38
  prompt: str,
39
+ # img_count: int,
40
+ # aspect_ratio: app_commands.Choice[str],
41
  ):
42
  try:
43
  await interaction.response.defer() # Defer the interaction
44
  genai.configure(api_key=GEMINI_API_KEY)
45
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
46
+ response = model.generate_content(prompt)
47
+ await interaction.followup.send(content=response.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+
50
  async def on_ready():
51
  await tree.sync()
52
  print("Bot is ready!")