Spaces:
Runtime error
Runtime error
adding next commands
Browse files- bot.py +34 -11
- redis2sheets.py β redis2sheets_img.py +2 -2
- redis2sheets_sound.py +28 -0
bot.py
CHANGED
@@ -2,6 +2,7 @@ import discord
|
|
2 |
from discord import Option, commands
|
3 |
import redis
|
4 |
import os
|
|
|
5 |
|
6 |
# Function to generate auto-incremented IDs
|
7 |
def generate_id(key):
|
@@ -136,13 +137,17 @@ def get_conf_score(confidence: str):
|
|
136 |
])
|
137 |
async def identify_image(ctx, id:int, label:str, confidence: str):
|
138 |
|
139 |
-
|
|
|
|
|
|
|
140 |
|
141 |
label_cnt = generate_id(f'cnt:label:image:{id}')
|
142 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
143 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'author', ctx.author.name.encode('utf-8'))
|
144 |
conf_score = get_conf_score(confidence)
|
145 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'confidence', conf_score)
|
|
|
146 |
|
147 |
r.hset(f'image:{id}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
148 |
r.hset(f'image:{id}'.encode('utf-8'), b'author', ctx.author.mention.encode('utf-8'))
|
@@ -197,12 +202,18 @@ async def sound(ctx, id): # a slash command will be created with the name "ping"
|
|
197 |
])
|
198 |
async def identify_sound(ctx, id:int, label:str, timestamp:str, confidence: str):
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
conf_score = get_conf_score(confidence)
|
205 |
-
r.hset(f'label:sound:{id}:{
|
206 |
|
207 |
r.hset(f'sound:{id}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
208 |
r.hset(f'sound:{id}'.encode('utf-8'), b'label_at', timestamp.encode('utf-8'))
|
@@ -225,6 +236,22 @@ async def identify_sound(ctx, id:int, label:str, timestamp:str, confidence: str)
|
|
225 |
|
226 |
await ctx.respond(f'{ctx.author.mention} labeled sound observation **{id}** as **{label}** at **{timestamp}** π (Earned 10 XP).\nListen here:\nhttps://gainforest.app/observations/{sound_data["uuid"]} π¦π΅', embed=embed)
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
@bot.command(
|
229 |
description="Check your profile"
|
230 |
)
|
@@ -233,10 +260,6 @@ async def profile(ctx):
|
|
233 |
if not r.exists(xp_key):
|
234 |
r.set(xp_key, 0)
|
235 |
|
236 |
-
role_key = f'{ctx.author.name}:role'.encode('utf-8')
|
237 |
-
if not r.exists(role_key):
|
238 |
-
r.set(role_key, 'NATURALIST')
|
239 |
-
|
240 |
img_oc_key = f'{ctx.author.name}:image'.encode('utf-8')
|
241 |
if not r.exists(img_oc_key):
|
242 |
r.set(img_oc_key, 0)
|
@@ -251,7 +274,7 @@ async def profile(ctx):
|
|
251 |
embed.add_field(name="**PROGRESS**", value=f"**Level**: 1\n**XP**: {r.get(xp_key).decode()}/1000\n", inline=False)
|
252 |
embed.add_field(name="**STATS**", value=f"**π± Images labeled**: {r.get(img_oc_key).decode()}", inline=False)
|
253 |
embed.set_thumbnail(url=ctx.author.display_avatar.url)
|
254 |
-
embed.set_footer(text=f"π RANK: {r.get(role_key).decode()}")
|
255 |
await ctx.respond(embed=embed)
|
256 |
|
257 |
bot.run('MTA5NzMzMDI2MDI4NDAzNTEyMg.GNFiQP.hZC_2HLTvVAROlKKUmnVQjviT0G4wHeQq23-rs')
|
|
|
2 |
from discord import Option, commands
|
3 |
import redis
|
4 |
import os
|
5 |
+
import random
|
6 |
|
7 |
# Function to generate auto-incremented IDs
|
8 |
def generate_id(key):
|
|
|
137 |
])
|
138 |
async def identify_image(ctx, id:int, label:str, confidence: str):
|
139 |
|
140 |
+
role = "Hobbyist"
|
141 |
+
role_names = [role.name for role in ctx.author.roles]
|
142 |
+
if "Expert" in role_names:
|
143 |
+
expert = "Expert"
|
144 |
|
145 |
label_cnt = generate_id(f'cnt:label:image:{id}')
|
146 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
147 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'author', ctx.author.name.encode('utf-8'))
|
148 |
conf_score = get_conf_score(confidence)
|
149 |
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'confidence', conf_score)
|
150 |
+
r.hset(f'label:image:{id}:{label_cnt}'.encode('utf-8'), b'role', role)
|
151 |
|
152 |
r.hset(f'image:{id}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
153 |
r.hset(f'image:{id}'.encode('utf-8'), b'author', ctx.author.mention.encode('utf-8'))
|
|
|
202 |
])
|
203 |
async def identify_sound(ctx, id:int, label:str, timestamp:str, confidence: str):
|
204 |
|
205 |
+
role = "Hobbyist"
|
206 |
+
role_names = [role.name for role in ctx.author.roles]
|
207 |
+
if "Expert" in role_names:
|
208 |
+
expert = "Expert"
|
209 |
+
|
210 |
+
label_cnt = generate_id(f'cnt:label:sound:{id}')
|
211 |
+
r.hset(f'label:sound:{id}:{label_cnt}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
212 |
+
r.hset(f'label:sound:{id}:{label_cnt}'.encode('utf-8'), b'author', ctx.author.name.encode('utf-8'))
|
213 |
+
r.hset(f'label:sound:{id}:{label_cnt}'.encode('utf-8'), b'role', role)
|
214 |
+
r.hset(f'label:sound:{id}:{label_cnt}'.encode('utf-8'), b'label_at', timestamp.encode('utf-8'))
|
215 |
conf_score = get_conf_score(confidence)
|
216 |
+
r.hset(f'label:sound:{id}:{label_cnt}'.encode('utf-8'), b'confidence', conf_score)
|
217 |
|
218 |
r.hset(f'sound:{id}'.encode('utf-8'), b'label', label.encode('utf-8'))
|
219 |
r.hset(f'sound:{id}'.encode('utf-8'), b'label_at', timestamp.encode('utf-8'))
|
|
|
236 |
|
237 |
await ctx.respond(f'{ctx.author.mention} labeled sound observation **{id}** as **{label}** at **{timestamp}** π (Earned 10 XP).\nListen here:\nhttps://gainforest.app/observations/{sound_data["uuid"]} π¦π΅', embed=embed)
|
238 |
|
239 |
+
@bot.command(
|
240 |
+
description="Get a random unlabeled image observation"
|
241 |
+
)
|
242 |
+
async def next_image(ctx):
|
243 |
+
total_images = r.get(b'cnt:image').decode('utf-8')
|
244 |
+
id = random.randint(1, int(total_images))
|
245 |
+
await image(ctx, id)
|
246 |
+
|
247 |
+
@bot.command(
|
248 |
+
description="Get a random unlabeled sound observation"
|
249 |
+
)
|
250 |
+
async def next_sound(ctx):
|
251 |
+
total_sound = r.get(b'cnt:sound').decode('utf-8')
|
252 |
+
id = random.randint(1, int(total_sound))
|
253 |
+
await sound(ctx, id)
|
254 |
+
|
255 |
@bot.command(
|
256 |
description="Check your profile"
|
257 |
)
|
|
|
260 |
if not r.exists(xp_key):
|
261 |
r.set(xp_key, 0)
|
262 |
|
|
|
|
|
|
|
|
|
263 |
img_oc_key = f'{ctx.author.name}:image'.encode('utf-8')
|
264 |
if not r.exists(img_oc_key):
|
265 |
r.set(img_oc_key, 0)
|
|
|
274 |
embed.add_field(name="**PROGRESS**", value=f"**Level**: 1\n**XP**: {r.get(xp_key).decode()}/1000\n", inline=False)
|
275 |
embed.add_field(name="**STATS**", value=f"**π± Images labeled**: {r.get(img_oc_key).decode()}", inline=False)
|
276 |
embed.set_thumbnail(url=ctx.author.display_avatar.url)
|
277 |
+
# embed.set_footer(text=f"π RANK: {r.get(role_key).decode()}")
|
278 |
await ctx.respond(embed=embed)
|
279 |
|
280 |
bot.run('MTA5NzMzMDI2MDI4NDAzNTEyMg.GNFiQP.hZC_2HLTvVAROlKKUmnVQjviT0G4wHeQq23-rs')
|
redis2sheets.py β redis2sheets_img.py
RENAMED
@@ -9,7 +9,7 @@ gc = gspread.service_account()
|
|
9 |
# Open a sheet from a spreadsheet in one go
|
10 |
wks = gc.open("Labels").sheet1
|
11 |
|
12 |
-
rows = [['image id', 'label id', 'author', 'label', 'confidence (0 = low, 1 = medium, 2 = high)', 'image url', 'dashboard url', 'image preview']]
|
13 |
|
14 |
redis_keys = r.keys('label:image:*')
|
15 |
for i_key in redis_keys:
|
@@ -20,7 +20,7 @@ for i_key in redis_keys:
|
|
20 |
raw_image_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_image_data_bytes.items()}
|
21 |
path = f'https://gainforest-transparency-dashboard.s3.amazonaws.com/{raw_image_data["awsCID"]}'
|
22 |
dashboard_url = f'https://gainforest.app/observations/{raw_image_data["uuid"]}'
|
23 |
-
row = [image_id, i_key.decode().split(':')[-1], image_data['author'], image_data['label'], image_data['confidence'], path, dashboard_url,'=IMAGE("{}", 2)'.format(path)]
|
24 |
rows.append(row)
|
25 |
|
26 |
# Update a range of cells using the top left corner address
|
|
|
9 |
# Open a sheet from a spreadsheet in one go
|
10 |
wks = gc.open("Labels").sheet1
|
11 |
|
12 |
+
rows = [['image id', 'label id', 'author', 'role', 'label', 'confidence (0 = low, 1 = medium, 2 = high)', 'image url', 'dashboard url', 'image preview']]
|
13 |
|
14 |
redis_keys = r.keys('label:image:*')
|
15 |
for i_key in redis_keys:
|
|
|
20 |
raw_image_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_image_data_bytes.items()}
|
21 |
path = f'https://gainforest-transparency-dashboard.s3.amazonaws.com/{raw_image_data["awsCID"]}'
|
22 |
dashboard_url = f'https://gainforest.app/observations/{raw_image_data["uuid"]}'
|
23 |
+
row = [image_id, i_key.decode().split(':')[-1], image_data['author'], image_data['role'], image_data['label'], image_data['confidence'], path, dashboard_url,'=IMAGE("{}", 2)'.format(path)]
|
24 |
rows.append(row)
|
25 |
|
26 |
# Update a range of cells using the top left corner address
|
redis2sheets_sound.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import redis
|
2 |
+
import gspread
|
3 |
+
|
4 |
+
redis_url = 'redis://default:[email protected]:7369'
|
5 |
+
r = redis.from_url(redis_url)
|
6 |
+
|
7 |
+
gc = gspread.service_account()
|
8 |
+
|
9 |
+
# Open a sheet from a spreadsheet in one for sound
|
10 |
+
wks = gc.open("Labels").get_worksheet(1)
|
11 |
+
|
12 |
+
rows = [['sound id', 'label id', 'author', 'role', 'timestamp', 'label', 'confidence (0 = low, 1 = medium, 2 = high)', 'sound url', 'dashboard url']]
|
13 |
+
|
14 |
+
redis_keys = r.keys('label:sound:*')
|
15 |
+
for i_key in redis_keys:
|
16 |
+
sound_data_bytes = r.hgetall(i_key)
|
17 |
+
sound_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in sound_data_bytes.items()}
|
18 |
+
sound_id = i_key.decode().split(':')[-2]
|
19 |
+
raw_sound_data_bytes = r.hgetall(f'sound:{sound_id}'.encode('utf-8'))
|
20 |
+
raw_sound_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_sound_data_bytes.items()}
|
21 |
+
path = f'https://gainforest-transparency-dashboard.s3.amazonaws.com/{raw_sound_data["awsCID"]}'
|
22 |
+
dashboard_url = f'https://gainforest.app/observations/{raw_sound_data["uuid"]}'
|
23 |
+
row = [sound_id, i_key.decode().split(':')[-1], sound_data['author'], sound_data['role'], sound_data['label_at'], sound_data['label'], sound_data['confidence'], path, dashboard_url]
|
24 |
+
rows.append(row)
|
25 |
+
|
26 |
+
# Update a range of cells using the top left corner address
|
27 |
+
wks.update('A1', rows, value_input_option='USER_ENTERED')
|
28 |
+
wks.format('A1:I1', {'textFormat': {'bold': True}})
|