artintel235 commited on
Commit
8ad5407
·
verified ·
1 Parent(s): 1ce3dab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -87
app.py CHANGED
@@ -28,52 +28,19 @@ if not DISCORD_BOT_TOKEN or not GEMINI_API_KEY:
28
  async def hello_command(interaction):
29
  await interaction.response.send_message("Hello there!")
30
 
31
- @tree.command(name="gemini", description="Chat with Gemini")
32
  async def generate_command(
33
  interaction: discord.Interaction,
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)
69
-
70
- # Call the Generative AI model
71
- response = model.generate_content(content, stream=True)
72
-
73
- current_message = None
74
  current_message_content = ""
75
 
76
- # Process streaming responses
77
  for part in response:
78
  text_chunk = part.text
79
 
@@ -84,65 +51,24 @@ async def generate_command(
84
  else:
85
  current_message = await interaction.followup.send(content=current_message_content)
86
  else:
 
87
  if current_message:
88
- await current_message.edit(content=current_message_content)
89
  else:
90
- await interaction.followup.send(content=current_message_content)
91
- current_message_content = text_chunk
92
  current_message = await interaction.followup.send(content=current_message_content)
93
 
 
 
94
  if current_message_content:
95
- if current_message:
96
- await current_message.edit(content=current_message_content)
97
- else:
98
- await interaction.followup.send(content=current_message_content)
99
 
100
  except Exception as e:
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():
 
28
  async def hello_command(interaction):
29
  await interaction.response.send_message("Hello there!")
30
 
 
31
  async def generate_command(
32
  interaction: discord.Interaction,
33
  prompt: str,
34
  ):
35
  try:
 
36
  await interaction.response.defer()
 
37
  genai.configure(api_key=GEMINI_API_KEY)
38
  model = genai.GenerativeModel("gemini-2.0-flash-exp")
39
+ response = model.generate_content(prompt, stream=True)
40
+
41
+ current_message = None # Initialize outside the loop to keep track of current message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  current_message_content = ""
43
 
 
44
  for part in response:
45
  text_chunk = part.text
46
 
 
51
  else:
52
  current_message = await interaction.followup.send(content=current_message_content)
53
  else:
54
+ # Send the current message
55
  if current_message:
56
+ await current_message.edit(content=current_message_content)
57
  else:
58
+ await interaction.followup.send(content=current_message_content)
59
+ current_message_content = text_chunk # Start new message with the current chunk
60
  current_message = await interaction.followup.send(content=current_message_content)
61
 
62
+
63
+ # Send any remaining content
64
  if current_message_content:
65
+ if current_message:
66
+ await current_message.edit(content=current_message_content)
67
+ else:
68
+ await interaction.followup.send(content=current_message_content)
69
 
70
  except Exception as e:
71
  print(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
 
74
  async def on_ready():