i guess this counts as img & pdf recog
This commit is contained in:
parent
65fbb69733
commit
2750b5fa78
1 changed files with 42 additions and 8 deletions
50
main.py
50
main.py
|
|
@ -5,6 +5,7 @@ from discord import app_commands
|
|||
from discord.ext import commands
|
||||
from tools import searxng, open_url, run_command
|
||||
from traceback import print_exc
|
||||
from typing import Optional
|
||||
import asyncio
|
||||
import os
|
||||
import io
|
||||
|
|
@ -38,28 +39,61 @@ async def generation(interaction: discord.Interaction) -> None:
|
|||
@bot.tree.command(name="ask", description="ai thing yes 👍")
|
||||
@app_commands.allowed_installs(guilds=False, users=True)
|
||||
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
|
||||
async def ask(interaction: discord.Interaction, prompt: str) -> None:
|
||||
async def ask(interaction: discord.Interaction, prompt: str, attachment: Optional[discord.Attachment]) -> None:
|
||||
await interaction.response.defer()
|
||||
|
||||
attachment_text: str | None = None
|
||||
attachment_data: bytes | None = None
|
||||
content_type: str | None = None
|
||||
if attachment:
|
||||
attachment_data = await attachment.read()
|
||||
content_type = attachment.content_type
|
||||
if content_type and ('text/plain' in content_type or 'text/markdown' in content_type):
|
||||
attachment_text = attachment_data.decode("utf-8")
|
||||
|
||||
response: types.GenerateContentResponse | None = None
|
||||
for _ in range(5):
|
||||
try:
|
||||
response = await client.aio.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=[
|
||||
prompt
|
||||
],
|
||||
config=config
|
||||
)
|
||||
if not attachment:
|
||||
response = await client.aio.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=[
|
||||
prompt
|
||||
],
|
||||
config=config
|
||||
)
|
||||
elif attachment and attachment_text:
|
||||
response = await client.aio.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=[
|
||||
prompt,
|
||||
types.Part.from_text(text=attachment_text)
|
||||
],
|
||||
config=config
|
||||
)
|
||||
elif content_type and attachment_data and ("image/" in content_type or "application/pdf" in content_type):
|
||||
response = await client.aio.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=[
|
||||
prompt,
|
||||
types.Part.from_bytes(
|
||||
data=attachment_data, mime_type=content_type)
|
||||
],
|
||||
config=config
|
||||
)
|
||||
break
|
||||
except:
|
||||
print_exc()
|
||||
await asyncio.sleep(10)
|
||||
continue
|
||||
|
||||
if not response:
|
||||
print("e")
|
||||
await interaction.edit_original_response(content="`[E] API Error. Please try again later.`")
|
||||
return
|
||||
if not response.text:
|
||||
await interaction.edit_original_response(content="`[E] Model returned no response`")
|
||||
|
||||
generation: str = response.text or ""
|
||||
if len(generation) > 2000:
|
||||
buffer = io.BytesIO(generation.encode("utf-8"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue