NihalGazi commited on
Commit
ea1af42
ยท
verified ยท
1 Parent(s): 02b91da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -3,6 +3,8 @@ from discord.ext import commands
3
  import threading
4
  import gradio as gr
5
  import asyncio
 
 
6
 
7
  # Set up intents - message_content is required to fetch message text
8
  intents = discord.Intents.default()
@@ -12,25 +14,17 @@ intents.message_content = True
12
 
13
  bot = commands.Bot(command_prefix="!", intents=intents)
14
 
15
- # Dummy translate function
16
- def translate(text, target_language):
17
- return f"[{target_language} translation of] {text}"
18
-
19
- # Mapping flag emojis to language codes
20
  flag_to_language = {
21
- "๐Ÿ‡ซ๐Ÿ‡ท": "fr", # French
22
- "๐Ÿ‡ฉ๐Ÿ‡ช": "de", # German
23
- "๐Ÿ‡ช๐Ÿ‡ธ": "es", # Spanish
24
- "๐Ÿ‡ฎ๐Ÿ‡น": "it", # Italian
25
  "๐Ÿ‡ฌ๐Ÿ‡ง": "en", # English
26
- "๐Ÿ‡ท๐Ÿ‡บ": "ru", # Russian
27
  }
28
 
29
  @bot.event
30
  async def on_ready():
31
  print(f"Logged in as {bot.user}")
32
 
33
- # Use on_raw_reaction_add to reliably catch reactions on uncached messages
34
  @bot.event
35
  async def on_raw_reaction_add(payload):
36
  # Ignore if the reaction was added by the bot itself
@@ -40,7 +34,6 @@ async def on_raw_reaction_add(payload):
40
  emoji = str(payload.emoji)
41
  if emoji in flag_to_language:
42
  target_lang = flag_to_language[emoji]
43
- # Get the channel from the payload
44
  channel = bot.get_channel(payload.channel_id)
45
  if channel is None:
46
  return
@@ -54,7 +47,22 @@ async def on_raw_reaction_add(payload):
54
  original_text = message.content
55
  if not original_text:
56
  print("Message content is empty!")
57
- translated_text = translate(original_text, target_lang)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  await channel.send(f"Translated to {target_lang}: {translated_text}")
59
 
60
  def run_discord_bot():
@@ -63,13 +71,12 @@ def run_discord_bot():
63
  # Run Discord bot in a separate daemon thread
64
  threading.Thread(target=run_discord_bot, daemon=True).start()
65
 
66
- # Gradio interface function
67
  def process_input(text):
68
  if not text.strip():
69
  return "OK"
70
  return text
71
 
72
- # Create a simple Gradio UI
73
  iface = gr.Interface(
74
  fn=process_input,
75
  inputs=gr.Textbox(label="Enter something"),
 
3
  import threading
4
  import gradio as gr
5
  import asyncio
6
+ import requests
7
+ import urllib.parse
8
 
9
  # Set up intents - message_content is required to fetch message text
10
  intents = discord.Intents.default()
 
14
 
15
  bot = commands.Bot(command_prefix="!", intents=intents)
16
 
17
+ # Mapping flag emojis to language codes (for now we support English only)
18
+ # We'll use English as target, but you can extend this to use different prompts.
 
 
 
19
  flag_to_language = {
 
 
 
 
20
  "๐Ÿ‡ฌ๐Ÿ‡ง": "en", # English
21
+ # Add more if needed; for now our prompt forces translation to English.
22
  }
23
 
24
  @bot.event
25
  async def on_ready():
26
  print(f"Logged in as {bot.user}")
27
 
 
28
  @bot.event
29
  async def on_raw_reaction_add(payload):
30
  # Ignore if the reaction was added by the bot itself
 
34
  emoji = str(payload.emoji)
35
  if emoji in flag_to_language:
36
  target_lang = flag_to_language[emoji]
 
37
  channel = bot.get_channel(payload.channel_id)
38
  if channel is None:
39
  return
 
47
  original_text = message.content
48
  if not original_text:
49
  print("Message content is empty!")
50
+ return
51
+
52
+ # Build the prompt for the translation AI.
53
+ prompt = f'Translate "{original_text}" to English. Write only and only the translated text, no need for explanation or anything.'
54
+ encoded_prompt = urllib.parse.quote(prompt)
55
+ url = f"https://text.pollinations.ai/{encoded_prompt}?model=gemini"
56
+
57
+ try:
58
+ # Use asyncio.to_thread to run the blocking GET request in a separate thread.
59
+ response = await asyncio.to_thread(requests.get, url)
60
+ response.raise_for_status()
61
+ translated_text = response.text.strip()
62
+ except Exception as e:
63
+ print(f"Error during GET request: {e}")
64
+ translated_text = "[Error translating text]"
65
+
66
  await channel.send(f"Translated to {target_lang}: {translated_text}")
67
 
68
  def run_discord_bot():
 
71
  # Run Discord bot in a separate daemon thread
72
  threading.Thread(target=run_discord_bot, daemon=True).start()
73
 
74
+ # Gradio interface function (simple echo interface)
75
  def process_input(text):
76
  if not text.strip():
77
  return "OK"
78
  return text
79
 
 
80
  iface = gr.Interface(
81
  fn=process_input,
82
  inputs=gr.Textbox(label="Enter something"),