artintel235 commited on
Commit
15d0115
·
verified ·
1 Parent(s): 02f58c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -2
app.py CHANGED
@@ -18,6 +18,9 @@ intents = discord.Intents.default()
18
  client = discord.Client(intents=intents)
19
  tree = app_commands.CommandTree(client)
20
 
 
 
 
21
  if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
22
  raise ValueError("Both DISCORD_BOT_TOKEN and GEMINI_API_KEY must be set.")
23
 
@@ -32,8 +35,6 @@ async def generate_command(
32
  ):
33
  try:
34
  await interaction.response.defer() # Defer the interaction
35
- genai.configure(api_key=GEMINI_API_KEY)
36
- model = genai.GenerativeModel("gemini-2.0-flash-exp")
37
  content = [] # List to store the content parts
38
 
39
  # Handle uploaded attachments
@@ -89,7 +90,59 @@ async def generate_command(
89
  print(e)
90
  await interaction.followup.send(f"An error occurred: {e}")
91
 
 
 
 
 
 
 
 
 
 
 
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  async def on_ready():
94
  await tree.sync()
95
  print("Bot is ready!")
 
18
  client = discord.Client(intents=intents)
19
  tree = app_commands.CommandTree(client)
20
 
21
+ genai.configure(api_key=GEMINI_API_KEY)
22
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
23
+
24
  if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
25
  raise ValueError("Both DISCORD_BOT_TOKEN and GEMINI_API_KEY must be set.")
26
 
 
35
  ):
36
  try:
37
  await interaction.response.defer() # Defer the interaction
 
 
38
  content = [] # List to store the content parts
39
 
40
  # Handle uploaded attachments
 
90
  print(e)
91
  await interaction.followup.send(f"An error occurred: {e}")
92
 
93
+ @tree.command(name="prompt", description="Generate a prompt based on an image")
94
+ async def prompt_from_image(interaction: discord.Interaction):
95
+ try:
96
+ # Ensure the command is replying to a message
97
+ if not interaction.message.reference:
98
+ await interaction.response.send_message("Please reply to a message with an image to use this command.", ephemeral=True)
99
+ return
100
+
101
+ # Get the original message
102
+ ref_message = await interaction.channel.fetch_message(interaction.message.reference.message_id)
103
 
104
+ # Check if the message contains attachments
105
+ if not ref_message.attachments:
106
+ await interaction.response.send_message("The replied-to message does not contain any images.", ephemeral=True)
107
+ return
108
+
109
+ # Ensure the first attachment is an image
110
+ attachment = ref_message.attachments[0]
111
+ if not attachment.content_type or not attachment.content_type.startswith("image/"):
112
+ await interaction.response.send_message("The replied-to message must contain an image.", ephemeral=True)
113
+ return
114
+
115
+ # Defer the interaction
116
+ await interaction.response.defer()
117
+
118
+ # Download the image
119
+ async with aiohttp.ClientSession() as session:
120
+ async with session.get(attachment.url) as resp:
121
+ if resp.status == 200:
122
+ image_bytes = await resp.read()
123
+ base64_image = base64.b64encode(image_bytes).decode("utf-8")
124
+ else:
125
+ await interaction.followup.send(
126
+ f"Failed to download the image: {attachment.filename}, status code: {resp.status}"
127
+ )
128
+ return
129
+
130
+ # Call the Generative AI model to generate a description
131
+ content = [{"mime_type": "image/jpeg", "data": base64_image}]
132
+
133
+ # Ask Gemini to generate a description (prompt)
134
+ response = model.generate_content(content, stream=False)
135
+
136
+ # Collect the response text
137
+ prompt = "".join([part.text for part in response])
138
+
139
+ # Send the generated prompt back to the user
140
+ await interaction.followup.send(f"Here is the suggested prompt for the image:\n```{prompt}```")
141
+
142
+ except Exception as e:
143
+ print(e)
144
+ await interaction.followup.send(f"An error occurred: {e}")
145
+
146
  async def on_ready():
147
  await tree.sync()
148
  print("Bot is ready!")