Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -25,45 +25,43 @@ if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
|
|
25 |
async def hello_command(interaction):
|
26 |
await interaction.response.send_message("Hello there!")
|
27 |
|
28 |
-
@tree.command(name="gemini", description="
|
29 |
-
# @app_commands.choices(
|
30 |
-
# aspect_ratio=[
|
31 |
-
# app_commands.Choice(name="1:1 (Square)", value="1:1"),
|
32 |
-
# app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
|
33 |
-
# app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
|
34 |
-
# app_commands.Choice(name="3:4", value="3:4"),
|
35 |
-
# app_commands.Choice(name="4:3", value="4:3"),
|
36 |
-
# ]
|
37 |
-
# )
|
38 |
async def generate_command(
|
39 |
interaction: discord.Interaction,
|
40 |
prompt: str,
|
41 |
-
images: List[discord.Attachment] = None,
|
42 |
):
|
43 |
try:
|
44 |
await interaction.response.defer() # Defer the interaction
|
45 |
genai.configure(api_key=GEMINI_API_KEY)
|
46 |
model = genai.GenerativeModel("gemini-2.0-flash-exp")
|
47 |
content = [] # List to store the content parts
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
async with aiohttp.ClientSession() as session:
|
51 |
-
async with session.get(
|
52 |
-
|
53 |
image_bytes = await resp.read()
|
54 |
-
base64_image = base64.b64encode(image_bytes).decode(
|
55 |
-
content.append({
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
return
|
|
|
|
|
60 |
content.append(prompt)
|
61 |
|
|
|
62 |
response = model.generate_content(content, stream=True)
|
63 |
|
64 |
current_message = None
|
65 |
current_message_content = ""
|
66 |
|
|
|
67 |
for part in response:
|
68 |
text_chunk = part.text
|
69 |
|
@@ -75,17 +73,18 @@ async def generate_command(
|
|
75 |
current_message = await interaction.followup.send(content=current_message_content)
|
76 |
else:
|
77 |
if current_message:
|
78 |
-
|
79 |
else:
|
80 |
-
|
81 |
current_message_content = text_chunk
|
82 |
current_message = await interaction.followup.send(content=current_message_content)
|
83 |
|
84 |
if current_message_content:
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
89 |
except Exception as e:
|
90 |
print(e)
|
91 |
await interaction.followup.send(f"An error occurred: {e}")
|
|
|
25 |
async def hello_command(interaction):
|
26 |
await interaction.response.send_message("Hello there!")
|
27 |
|
28 |
+
@tree.command(name="gemini", description="Chat with Gemini")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
async def generate_command(
|
30 |
interaction: discord.Interaction,
|
31 |
prompt: str,
|
|
|
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
|
40 |
+
if interaction.attachments:
|
41 |
+
for attachment in interaction.attachments:
|
42 |
+
# Download each attachment
|
43 |
async with aiohttp.ClientSession() as session:
|
44 |
+
async with session.get(attachment.url) as resp:
|
45 |
+
if resp.status == 200:
|
46 |
image_bytes = await resp.read()
|
47 |
+
base64_image = base64.b64encode(image_bytes).decode("utf-8")
|
48 |
+
content.append({"mime_type": "image/jpeg", "data": base64_image})
|
49 |
+
else:
|
50 |
+
await interaction.followup.send(
|
51 |
+
f"Failed to download image: {attachment.filename}, status code: {resp.status}"
|
52 |
+
)
|
53 |
return
|
54 |
+
|
55 |
+
# Add the text prompt to the content
|
56 |
content.append(prompt)
|
57 |
|
58 |
+
# Call the Generative AI model
|
59 |
response = model.generate_content(content, stream=True)
|
60 |
|
61 |
current_message = None
|
62 |
current_message_content = ""
|
63 |
|
64 |
+
# Process streaming responses
|
65 |
for part in response:
|
66 |
text_chunk = part.text
|
67 |
|
|
|
73 |
current_message = await interaction.followup.send(content=current_message_content)
|
74 |
else:
|
75 |
if current_message:
|
76 |
+
await current_message.edit(content=current_message_content)
|
77 |
else:
|
78 |
+
await interaction.followup.send(content=current_message_content)
|
79 |
current_message_content = text_chunk
|
80 |
current_message = await interaction.followup.send(content=current_message_content)
|
81 |
|
82 |
if current_message_content:
|
83 |
+
if current_message:
|
84 |
+
await current_message.edit(content=current_message_content)
|
85 |
+
else:
|
86 |
+
await interaction.followup.send(content=current_message_content)
|
87 |
+
|
88 |
except Exception as e:
|
89 |
print(e)
|
90 |
await interaction.followup.send(f"An error occurred: {e}")
|