Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,11 +14,17 @@ intents.message_content = True
|
|
14 |
|
15 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
16 |
|
17 |
-
# Mapping flag emojis to language
|
18 |
-
# We'll use English as target, but you can extend this to use different prompts.
|
19 |
flag_to_language = {
|
20 |
-
"๐ฌ๐ง": "
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
24 |
@bot.event
|
@@ -27,7 +33,7 @@ async def on_ready():
|
|
27 |
|
28 |
@bot.event
|
29 |
async def on_raw_reaction_add(payload):
|
30 |
-
# Ignore
|
31 |
if payload.user_id == bot.user.id:
|
32 |
return
|
33 |
|
@@ -36,7 +42,9 @@ async def on_raw_reaction_add(payload):
|
|
36 |
target_lang = flag_to_language[emoji]
|
37 |
channel = bot.get_channel(payload.channel_id)
|
38 |
if channel is None:
|
|
|
39 |
return
|
|
|
40 |
try:
|
41 |
# Fetch the full message to access its content
|
42 |
message = await channel.fetch_message(payload.message_id)
|
@@ -49,17 +57,23 @@ async def on_raw_reaction_add(payload):
|
|
49 |
print("Message content is empty!")
|
50 |
return
|
51 |
|
52 |
-
# Build the prompt
|
53 |
-
prompt =
|
|
|
|
|
|
|
54 |
encoded_prompt = urllib.parse.quote(prompt)
|
55 |
url = f"https://text.pollinations.ai/{encoded_prompt}?model=gemini"
|
56 |
-
|
|
|
57 |
try:
|
58 |
-
#
|
59 |
-
response = await asyncio.to_thread(requests.get, url)
|
60 |
response.raise_for_status()
|
61 |
translated_text = response.text.strip()
|
62 |
-
print(translated_text)
|
|
|
|
|
63 |
except Exception as e:
|
64 |
print(f"Error during GET request: {e}")
|
65 |
translated_text = "[Error translating text]"
|
@@ -67,9 +81,9 @@ async def on_raw_reaction_add(payload):
|
|
67 |
await channel.send(f"Translated to {target_lang}: {translated_text}")
|
68 |
|
69 |
def run_discord_bot():
|
70 |
-
bot.run("MTM1MjI2OTQ4MTQwNTE4NjA3MA.GrdeHW.rYndSNvb9mepFdp_uTK4IOAmKwt31QER6hRgzg") # Replace with your
|
71 |
|
72 |
-
# Run Discord bot in a separate daemon thread
|
73 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
74 |
|
75 |
# Gradio interface function (simple echo interface)
|
|
|
14 |
|
15 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
16 |
|
17 |
+
# Mapping flag emojis to their language names
|
|
|
18 |
flag_to_language = {
|
19 |
+
"๐ฌ๐ง": "English",
|
20 |
+
"๐ซ๐ท": "French",
|
21 |
+
"๐ฉ๐ช": "German",
|
22 |
+
"๐ช๐ธ": "Spanish",
|
23 |
+
"๐ฎ๐น": "Italian",
|
24 |
+
"๐ท๐บ": "Russian",
|
25 |
+
"๐ฏ๐ต": "Japanese",
|
26 |
+
"๐จ๐ณ": "Chinese",
|
27 |
+
# Add more flags and language names as needed
|
28 |
}
|
29 |
|
30 |
@bot.event
|
|
|
33 |
|
34 |
@bot.event
|
35 |
async def on_raw_reaction_add(payload):
|
36 |
+
# Ignore reactions by the bot itself
|
37 |
if payload.user_id == bot.user.id:
|
38 |
return
|
39 |
|
|
|
42 |
target_lang = flag_to_language[emoji]
|
43 |
channel = bot.get_channel(payload.channel_id)
|
44 |
if channel is None:
|
45 |
+
print("Channel not found")
|
46 |
return
|
47 |
+
|
48 |
try:
|
49 |
# Fetch the full message to access its content
|
50 |
message = await channel.fetch_message(payload.message_id)
|
|
|
57 |
print("Message content is empty!")
|
58 |
return
|
59 |
|
60 |
+
# Build the translation prompt using the target language
|
61 |
+
prompt = (
|
62 |
+
f'Translate "{original_text}" to {target_lang}. '
|
63 |
+
"Write only and only the translated text, no need for explanation or anything."
|
64 |
+
)
|
65 |
encoded_prompt = urllib.parse.quote(prompt)
|
66 |
url = f"https://text.pollinations.ai/{encoded_prompt}?model=gemini"
|
67 |
+
print(f"GET {url}")
|
68 |
+
|
69 |
try:
|
70 |
+
# Run the blocking GET request in a thread so as not to block the event loop.
|
71 |
+
response = await asyncio.to_thread(requests.get, url, timeout=10)
|
72 |
response.raise_for_status()
|
73 |
translated_text = response.text.strip()
|
74 |
+
print(f"API response: {translated_text}")
|
75 |
+
if not translated_text:
|
76 |
+
translated_text = "[No translation returned]"
|
77 |
except Exception as e:
|
78 |
print(f"Error during GET request: {e}")
|
79 |
translated_text = "[Error translating text]"
|
|
|
81 |
await channel.send(f"Translated to {target_lang}: {translated_text}")
|
82 |
|
83 |
def run_discord_bot():
|
84 |
+
bot.run("MTM1MjI2OTQ4MTQwNTE4NjA3MA.GrdeHW.rYndSNvb9mepFdp_uTK4IOAmKwt31QER6hRgzg") # Replace with your bot token
|
85 |
|
86 |
+
# Run the Discord bot in a separate daemon thread
|
87 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
88 |
|
89 |
# Gradio interface function (simple echo interface)
|