Spaces:
Running
Running
File size: 3,208 Bytes
56df8ed 39fd8bf ea1af42 79d85aa 56df8ed 39fd8bf 56df8ed 39fd8bf 56df8ed 35c957f 56df8ed 35c957f 84f9ffd 35c957f 56df8ed 39fd8bf 35c957f 39fd8bf 56df8ed 39fd8bf 56df8ed 39fd8bf 35c957f 39fd8bf 35c957f 39fd8bf ea1af42 35c957f ea1af42 d0dcd2d cd371ad 35c957f ea1af42 35c957f ea1af42 35c957f ea1af42 f45da3f 29c9b62 56df8ed 33310f2 56df8ed 35c957f 56df8ed ea1af42 56df8ed 39fd8bf 56df8ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import discord
from discord.ext import commands
import threading
import gradio as gr
import asyncio
import requests
import urllib.parse
from dotenv import load_dotenv
import os
# Set up intents - message_content is required to fetch message text
intents = discord.Intents.default()
intents.messages = True
intents.reactions = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Mapping flag emojis to their language names
flag_to_language = {
"๐ฌ๐ง": "English",
"๐ซ๐ท": "French",
"๐ฉ๐ช": "German",
"๐ช๐ธ": "Spanish",
"๐ฎ๐น": "Italian",
"๐ท๐บ": "Russian",
"๐ฏ๐ต": "Japanese",
"๐จ๐ณ": "Chinese",
"๐น๐ท": "Turkish",
# Add more flags and language names as needed
}
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.event
async def on_raw_reaction_add(payload):
# Ignore reactions by the bot itself
if payload.user_id == bot.user.id:
return
emoji = str(payload.emoji)
if emoji in flag_to_language:
target_lang = flag_to_language[emoji]
channel = bot.get_channel(payload.channel_id)
if channel is None:
print("Channel not found")
return
try:
# Fetch the full message to access its content
message = await channel.fetch_message(payload.message_id)
except Exception as e:
print(f"Error fetching message: {e}")
return
original_text = message.content
if not original_text:
print("Message content is empty!")
return
# Build the translation prompt using the target language
prompt = (
f'Translate "{original_text}" to {target_lang}. '
"Write only and only the translated text, no need for explanation or anything."
)
encoded_prompt = urllib.parse.quote(prompt)
server = os.getenv("api")
url = f"{server}{encoded_prompt}"
print(f"GET {url}")
try:
# Run the blocking GET request in a thread so as not to block the event loop.
response = await asyncio.to_thread(requests.get, url, timeout=10)
response.raise_for_status()
translated_text = response.text.strip()
print(f"API response: {translated_text}")
if not translated_text:
translated_text = "[No translation returned]"
except Exception as e:
print(f"Error during GET request: {e}")
translated_text = "[Error translating text]"
# Reply to the original message so that it is tagged
await message.reply(f"{translated_text}")
def run_discord_bot():
bot.run(os.getenv("token")) # Replace with your actual token
# Run the Discord bot in a separate daemon thread
threading.Thread(target=run_discord_bot, daemon=True).start()
# Gradio interface function (simple echo interface)
def process_input(text):
if not text.strip():
return "OK"
return text
iface = gr.Interface(
fn=process_input,
inputs=gr.Textbox(label="Enter something"),
outputs=gr.Textbox(label="Output")
)
iface.launch()
|