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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -43
app.py CHANGED
@@ -34,24 +34,35 @@ async def generate_command(
34
  prompt: str,
35
  ):
36
  try:
37
- await interaction.response.defer() # Defer the interaction
 
 
 
 
38
  content = [] # List to store the content parts
39
 
40
- # Handle uploaded attachments
41
- if interaction.attachments:
42
- for attachment in interaction.attachments:
43
- # Download each attachment
44
- async with aiohttp.ClientSession() as session:
45
- async with session.get(attachment.url) as resp:
46
- if resp.status == 200:
47
- image_bytes = await resp.read()
48
- base64_image = base64.b64encode(image_bytes).decode("utf-8")
49
- content.append({"mime_type": "image/jpeg", "data": base64_image})
50
- else:
51
- await interaction.followup.send(
52
- f"Failed to download image: {attachment.filename}, status code: {resp.status}"
53
- )
54
- return
 
 
 
 
 
 
 
55
 
56
  # Add the text prompt to the content
57
  content.append(prompt)
@@ -90,59 +101,50 @@ async def generate_command(
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!")
 
34
  prompt: str,
35
  ):
36
  try:
37
+ # Defer the interaction to allow time for processing
38
+ await interaction.response.defer()
39
+
40
+ genai.configure(api_key=GEMINI_API_KEY)
41
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
42
  content = [] # List to store the content parts
43
 
44
+ # Handle attachments if the command is used in reply to a message
45
+ if interaction.message.reference:
46
+ ref_message = await interaction.channel.fetch_message(interaction.message.reference.message_id)
47
+
48
+ if ref_message.attachments: # Check if the replied-to message has attachments
49
+ for attachment in ref_message.attachments:
50
+ if not attachment.content_type or not attachment.content_type.startswith("image/"):
51
+ await interaction.followup.send(f"The attachment {attachment.filename} is not an image.", ephemeral=True)
52
+ continue
53
+
54
+ # Download each attachment
55
+ async with aiohttp.ClientSession() as session:
56
+ async with session.get(attachment.url) as resp:
57
+ if resp.status == 200:
58
+ image_bytes = await resp.read()
59
+ base64_image = base64.b64encode(image_bytes).decode("utf-8")
60
+ content.append({"mime_type": "image/jpeg", "data": base64_image})
61
+ else:
62
+ await interaction.followup.send(
63
+ f"Failed to download image: {attachment.filename}, status code: {resp.status}"
64
+ )
65
+ return
66
 
67
  # Add the text prompt to the content
68
  content.append(prompt)
 
101
  print(e)
102
  await interaction.followup.send(f"An error occurred: {e}")
103
 
104
+ @tree.command(name="discribe", description="Process an image from a message")
105
+ async def process_image(interaction: discord.Interaction):
106
  try:
107
  # Ensure the command is replying to a message
108
  if not interaction.message.reference:
109
  await interaction.response.send_message("Please reply to a message with an image to use this command.", ephemeral=True)
110
  return
111
 
112
+ # Fetch the original message being replied to
113
  ref_message = await interaction.channel.fetch_message(interaction.message.reference.message_id)
114
+
115
+ # Check if the original message has attachments
116
  if not ref_message.attachments:
117
+ await interaction.response.send_message("The replied-to message does not contain any attachments.", ephemeral=True)
118
  return
119
 
120
+ # Process the first attachment (assuming it's an image)
121
  attachment = ref_message.attachments[0]
122
+ if not attachment.content_type.startswith("image/"):
123
+ await interaction.response.send_message("The attachment is not an image.", ephemeral=True)
124
  return
125
 
126
+ # Defer the response to allow time for processing
127
  await interaction.response.defer()
128
 
129
+ # Download and process the image as needed
130
  async with aiohttp.ClientSession() as session:
131
  async with session.get(attachment.url) as resp:
132
  if resp.status == 200:
133
  image_bytes = await resp.read()
134
+ # Process the image bytes as needed
135
+ # For example, pass them to your AI model
136
  else:
137
+ await interaction.followup.send(f"Failed to download the image: {attachment.filename}")
 
 
138
  return
139
 
140
+ # After processing, send a response
141
+ await interaction.followup.send("Image processed successfully.")
 
 
 
 
 
 
 
 
 
142
 
143
  except Exception as e:
144
+ print(f"An error occurred: {e}")
145
  await interaction.followup.send(f"An error occurred: {e}")
146
 
147
+
148
  async def on_ready():
149
  await tree.sync()
150
  print("Bot is ready!")