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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CodeAlpha Internship - Task 1: Language Translation Tool
|
| 3 |
+
Uses: Groq API (LLM inference) + langdetect (Hugging Face-style language detection) + Gradio (UI)
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from groq import Groq
|
| 8 |
+
from langdetect import detect, DetectorFactory
|
| 9 |
+
import pyperclip
|
| 10 |
+
|
| 11 |
+
DetectorFactory.seed = 0 # For consistent language detection
|
| 12 |
+
|
| 13 |
+
# βββ CONFIGURATION ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
GROQ_API_KEY = "your_groq_api_key_here" # <-- Replace with your Groq API key
|
| 15 |
+
MODEL = "llama3-8b-8192" # Fast and free on Groq
|
| 16 |
+
|
| 17 |
+
LANGUAGES = [
|
| 18 |
+
"Auto-Detect",
|
| 19 |
+
"English", "Urdu", "Arabic", "French", "Spanish", "German",
|
| 20 |
+
"Chinese (Simplified)", "Japanese", "Korean", "Hindi", "Portuguese",
|
| 21 |
+
"Italian", "Russian", "Turkish", "Dutch", "Polish", "Bengali",
|
| 22 |
+
"Vietnamese", "Thai", "Persian (Farsi)"
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
LANG_CODE_MAP = {
|
| 26 |
+
"en": "English", "ur": "Urdu", "ar": "Arabic", "fr": "French",
|
| 27 |
+
"es": "Spanish", "de": "German", "zh-cn": "Chinese (Simplified)",
|
| 28 |
+
"ja": "Japanese", "ko": "Korean", "hi": "Hindi", "pt": "Portuguese",
|
| 29 |
+
"it": "Italian", "ru": "Russian", "tr": "Turkish", "nl": "Dutch",
|
| 30 |
+
"pl": "Polish", "bn": "Bengali", "vi": "Vietnamese", "th": "Thai",
|
| 31 |
+
"fa": "Persian (Farsi)"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# βββ CORE TRANSLATION FUNCTION ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
def translate_text(input_text, source_lang, target_lang):
|
| 36 |
+
if not input_text.strip():
|
| 37 |
+
return "β οΈ Please enter some text to translate.", ""
|
| 38 |
+
|
| 39 |
+
if target_lang == "Auto-Detect":
|
| 40 |
+
return "β οΈ Please select a target language.", ""
|
| 41 |
+
|
| 42 |
+
if not GROQ_API_KEY or GROQ_API_KEY == "your_groq_api_key_here":
|
| 43 |
+
return "β οΈ Please set your GROQ_API_KEY in the script.", ""
|
| 44 |
+
|
| 45 |
+
# Auto-detect source language
|
| 46 |
+
detected_info = ""
|
| 47 |
+
if source_lang == "Auto-Detect":
|
| 48 |
+
try:
|
| 49 |
+
detected_code = detect(input_text)
|
| 50 |
+
detected_name = LANG_CODE_MAP.get(detected_code, detected_code.upper())
|
| 51 |
+
detected_info = f"π Detected language: **{detected_name}**"
|
| 52 |
+
source_lang = detected_name
|
| 53 |
+
except Exception:
|
| 54 |
+
source_lang = "the source language"
|
| 55 |
+
detected_info = "π Could not detect language, letting AI handle it."
|
| 56 |
+
|
| 57 |
+
# Build prompt
|
| 58 |
+
prompt = (
|
| 59 |
+
f"You are a professional translator. Translate the following text from {source_lang} "
|
| 60 |
+
f"to {target_lang}. Return ONLY the translated text β no explanations, no labels, "
|
| 61 |
+
f"no quotation marks, just the clean translation.\n\n"
|
| 62 |
+
f"Text to translate:\n{input_text}"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 67 |
+
response = client.chat.completions.create(
|
| 68 |
+
model=MODEL,
|
| 69 |
+
messages=[{"role": "user", "content": prompt}],
|
| 70 |
+
temperature=0.3,
|
| 71 |
+
max_tokens=1024
|
| 72 |
+
)
|
| 73 |
+
translated = response.choices[0].message.content.strip()
|
| 74 |
+
return translated, detected_info
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"β Translation error: {str(e)}", ""
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# βββ TEXT-TO-SPEECH (browser-based via JS) ββββββββββββββββββββββββββββββββββββ
|
| 81 |
+
TTS_JS = """
|
| 82 |
+
function speakText() {
|
| 83 |
+
const outputBox = document.querySelector('#translated-output textarea');
|
| 84 |
+
if (!outputBox || !outputBox.value.trim()) {
|
| 85 |
+
alert("Nothing to speak yet!");
|
| 86 |
+
return;
|
| 87 |
+
}
|
| 88 |
+
const utterance = new SpeechSynthesisUtterance(outputBox.value);
|
| 89 |
+
window.speechSynthesis.speak(utterance);
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
# βββ GRADIO UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 94 |
+
with gr.Blocks(
|
| 95 |
+
title="Language Translator β CodeAlpha",
|
| 96 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 97 |
+
css="""
|
| 98 |
+
.container { max-width: 900px; margin: auto; }
|
| 99 |
+
.title-block { text-align: center; padding: 10px 0 20px; }
|
| 100 |
+
#translated-output textarea { font-size: 16px; }
|
| 101 |
+
.footer { text-align: center; font-size: 12px; color: #888; margin-top: 20px; }
|
| 102 |
+
"""
|
| 103 |
+
) as demo:
|
| 104 |
+
|
| 105 |
+
gr.HTML("""
|
| 106 |
+
<div class="title-block">
|
| 107 |
+
<h1>π Language Translation Tool</h1>
|
| 108 |
+
<p style="color:#555;">Powered by Groq LLaMA3 + LangDetect | CodeAlpha Internship Task 1</p>
|
| 109 |
+
</div>
|
| 110 |
+
""")
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
source_lang = gr.Dropdown(
|
| 114 |
+
choices=LANGUAGES,
|
| 115 |
+
value="Auto-Detect",
|
| 116 |
+
label="Source Language",
|
| 117 |
+
scale=1
|
| 118 |
+
)
|
| 119 |
+
target_lang = gr.Dropdown(
|
| 120 |
+
choices=[l for l in LANGUAGES if l != "Auto-Detect"],
|
| 121 |
+
value="Urdu",
|
| 122 |
+
label="Target Language",
|
| 123 |
+
scale=1
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
with gr.Row():
|
| 127 |
+
with gr.Column():
|
| 128 |
+
input_text = gr.Textbox(
|
| 129 |
+
label="Input Text",
|
| 130 |
+
placeholder="Enter text here to translate...",
|
| 131 |
+
lines=8,
|
| 132 |
+
max_lines=15
|
| 133 |
+
)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
translate_btn = gr.Button("π Translate", variant="primary", scale=3)
|
| 136 |
+
clear_btn = gr.Button("ποΈ Clear", scale=1)
|
| 137 |
+
|
| 138 |
+
with gr.Column():
|
| 139 |
+
output_text = gr.Textbox(
|
| 140 |
+
label="Translated Text",
|
| 141 |
+
lines=8,
|
| 142 |
+
max_lines=15,
|
| 143 |
+
interactive=False,
|
| 144 |
+
elem_id="translated-output"
|
| 145 |
+
)
|
| 146 |
+
detection_info = gr.Markdown("")
|
| 147 |
+
with gr.Row():
|
| 148 |
+
copy_btn = gr.Button("π Copy to Clipboard", scale=2)
|
| 149 |
+
speak_btn = gr.Button("π Speak Translation", scale=2)
|
| 150 |
+
|
| 151 |
+
# ββ Event handlers ββ
|
| 152 |
+
def do_translate(text, src, tgt):
|
| 153 |
+
result, info = translate_text(text, src, tgt)
|
| 154 |
+
return result, info
|
| 155 |
+
|
| 156 |
+
def clear_all():
|
| 157 |
+
return "", "", ""
|
| 158 |
+
|
| 159 |
+
def copy_output(text):
|
| 160 |
+
if text.strip():
|
| 161 |
+
try:
|
| 162 |
+
pyperclip.copy(text)
|
| 163 |
+
return gr.update(value="β
Copied!", interactive=False)
|
| 164 |
+
except Exception:
|
| 165 |
+
return gr.update(value="β οΈ Copy failed (run in local env)", interactive=False)
|
| 166 |
+
return gr.update(value="π Copy to Clipboard")
|
| 167 |
+
|
| 168 |
+
translate_btn.click(
|
| 169 |
+
fn=do_translate,
|
| 170 |
+
inputs=[input_text, source_lang, target_lang],
|
| 171 |
+
outputs=[output_text, detection_info]
|
| 172 |
+
)
|
| 173 |
+
input_text.submit(
|
| 174 |
+
fn=do_translate,
|
| 175 |
+
inputs=[input_text, source_lang, target_lang],
|
| 176 |
+
outputs=[output_text, detection_info]
|
| 177 |
+
)
|
| 178 |
+
clear_btn.click(fn=clear_all, outputs=[input_text, output_text, detection_info])
|
| 179 |
+
copy_btn.click(fn=copy_output, inputs=[output_text], outputs=[copy_btn])
|
| 180 |
+
speak_btn.click(fn=None, js=TTS_JS) # Browser TTS via JS
|
| 181 |
+
|
| 182 |
+
gr.HTML("""
|
| 183 |
+
<div class="footer">
|
| 184 |
+
Built with β€οΈ for CodeAlpha AI Internship Β· Groq LLaMA3-8B Β· Gradio UI
|
| 185 |
+
</div>
|
| 186 |
+
""")
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
demo.launch(share=False, inbrowser=True)
|