artintel235 commited on
Commit
5633cd6
·
verified ·
1 Parent(s): a8c126e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -8,6 +8,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")
@@ -36,15 +37,30 @@ async def hello_command(interaction):
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, stream=True)
47
- current_message = None # Initialize outside the loop to keep track of current message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  current_message_content = ""
49
 
50
  for part in response:
@@ -57,23 +73,21 @@ async def generate_command(
57
  else:
58
  current_message = await interaction.followup.send(content=current_message_content)
59
  else:
60
- # Send the current message
61
  if current_message:
62
  await current_message.edit(content=current_message_content)
63
  else:
64
  await interaction.followup.send(content=current_message_content)
65
- current_message_content = text_chunk # Start new message with the current chunk
66
  current_message = await interaction.followup.send(content=current_message_content)
67
 
68
-
69
- # Send any remaining content
70
  if current_message_content:
71
  if current_message:
72
  await current_message.edit(content=current_message_content)
73
  else:
74
- await interaction.followup.send(content=current_message_content)
75
  except Exception as e:
76
  print(e)
 
77
 
78
 
79
  async def on_ready():
 
8
  import google.generativeai as genai
9
  from io import BytesIO
10
  import PIL.Image
11
+ import base64
12
  # --- Environment Variables & Setup ---
13
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
14
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
 
37
  async def generate_command(
38
  interaction: discord.Interaction,
39
  prompt: str,
40
+ images: list[discord.Attachment] = None,
 
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
+ content = [] # List to store the content parts
47
+ if images:
48
+ for image in images:
49
+ async with aiohttp.ClientSession() as session:
50
+ async with session.get(image.url) as resp:
51
+ if resp.status == 200:
52
+ image_bytes = await resp.read()
53
+ base64_image = base64.b64encode(image_bytes).decode('utf-8')
54
+ content.append({'mime_type':'image/jpeg', 'data': base64_image})
55
+
56
+ else:
57
+ await interaction.followup.send(f"Failed to download image: {image.filename}, status code: {resp.status}")
58
+ return
59
+ content.append(prompt)
60
+
61
+ response = model.generate_content(content, stream=True)
62
+
63
+ current_message = None
64
  current_message_content = ""
65
 
66
  for part in response:
 
73
  else:
74
  current_message = await interaction.followup.send(content=current_message_content)
75
  else:
 
76
  if current_message:
77
  await current_message.edit(content=current_message_content)
78
  else:
79
  await interaction.followup.send(content=current_message_content)
80
+ current_message_content = text_chunk
81
  current_message = await interaction.followup.send(content=current_message_content)
82
 
 
 
83
  if current_message_content:
84
  if current_message:
85
  await current_message.edit(content=current_message_content)
86
  else:
87
+ await interaction.followup.send(content=current_message_content)
88
  except Exception as e:
89
  print(e)
90
+ await interaction.followup.send(f"An error occurred: {e}")
91
 
92
 
93
  async def on_ready():