Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -34,24 +34,35 @@ async def generate_command(
|
|
34 |
prompt: str,
|
35 |
):
|
36 |
try:
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
content = [] # List to store the content parts
|
39 |
|
40 |
-
# Handle
|
41 |
-
if interaction.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="
|
94 |
-
async def
|
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 |
-
#
|
102 |
ref_message = await interaction.channel.fetch_message(interaction.message.reference.message_id)
|
103 |
-
|
104 |
-
# Check if the message
|
105 |
if not ref_message.attachments:
|
106 |
-
await interaction.response.send_message("The replied-to message does not contain any
|
107 |
return
|
108 |
|
109 |
-
#
|
110 |
attachment = ref_message.attachments[0]
|
111 |
-
if not attachment.content_type
|
112 |
-
await interaction.response.send_message("The
|
113 |
return
|
114 |
|
115 |
-
# Defer the
|
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 |
-
|
|
|
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 |
-
#
|
131 |
-
|
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!")
|