Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,20 @@
|
|
| 1 |
"""
|
| 2 |
CodeAlpha Internship - Task 1: Language Translation Tool
|
| 3 |
-
Uses: Groq API (LLM inference) + langdetect
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
| 8 |
from langdetect import detect, DetectorFactory
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
# βββ CONFIGURATION ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
-
GROQ_API_KEY = "
|
| 14 |
-
MODEL = "llama3-8b-8192"
|
| 15 |
|
| 16 |
LANGUAGES = [
|
| 17 |
"Auto-Detect",
|
|
@@ -38,10 +41,9 @@ def translate_text(input_text, source_lang, target_lang):
|
|
| 38 |
if target_lang == "Auto-Detect":
|
| 39 |
return "β οΈ Please select a target language.", ""
|
| 40 |
|
| 41 |
-
if not GROQ_API_KEY
|
| 42 |
-
return "β οΈ
|
| 43 |
|
| 44 |
-
# Auto-detect source language
|
| 45 |
detected_info = ""
|
| 46 |
if source_lang == "Auto-Detect":
|
| 47 |
try:
|
|
@@ -53,12 +55,10 @@ def translate_text(input_text, source_lang, target_lang):
|
|
| 53 |
source_lang = "the source language"
|
| 54 |
detected_info = "π Could not detect language, letting AI handle it."
|
| 55 |
|
| 56 |
-
# Build prompt
|
| 57 |
prompt = (
|
| 58 |
f"You are a professional translator. Translate the following text from {source_lang} "
|
| 59 |
f"to {target_lang}. Return ONLY the translated text β no explanations, no labels, "
|
| 60 |
-
f"no quotation marks, just the clean translation.\n\n"
|
| 61 |
-
f"Text to translate:\n{input_text}"
|
| 62 |
)
|
| 63 |
|
| 64 |
try:
|
|
@@ -71,12 +71,11 @@ def translate_text(input_text, source_lang, target_lang):
|
|
| 71 |
)
|
| 72 |
translated = response.choices[0].message.content.strip()
|
| 73 |
return translated, detected_info
|
| 74 |
-
|
| 75 |
except Exception as e:
|
| 76 |
return f"β Translation error: {str(e)}", ""
|
| 77 |
|
| 78 |
|
| 79 |
-
# βββ
|
| 80 |
TTS_JS = """
|
| 81 |
function speakText() {
|
| 82 |
const outputBox = document.querySelector('#translated-output textarea');
|
|
@@ -89,12 +88,24 @@ function speakText() {
|
|
| 89 |
}
|
| 90 |
"""
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# βββ GRADIO UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
with gr.Blocks(
|
| 94 |
title="Language Translator β CodeAlpha",
|
| 95 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 96 |
css="""
|
| 97 |
-
.container { max-width: 900px; margin: auto; }
|
| 98 |
.title-block { text-align: center; padding: 10px 0 20px; }
|
| 99 |
#translated-output textarea { font-size: 16px; }
|
| 100 |
.footer { text-align: center; font-size: 12px; color: #888; margin-top: 20px; }
|
|
@@ -110,16 +121,11 @@ with gr.Blocks(
|
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
source_lang = gr.Dropdown(
|
| 113 |
-
choices=LANGUAGES,
|
| 114 |
-
value="Auto-Detect",
|
| 115 |
-
label="Source Language",
|
| 116 |
-
scale=1
|
| 117 |
)
|
| 118 |
target_lang = gr.Dropdown(
|
| 119 |
choices=[l for l in LANGUAGES if l != "Auto-Detect"],
|
| 120 |
-
value="Urdu",
|
| 121 |
-
label="Target Language",
|
| 122 |
-
scale=1
|
| 123 |
)
|
| 124 |
|
| 125 |
with gr.Row():
|
|
@@ -127,8 +133,7 @@ with gr.Blocks(
|
|
| 127 |
input_text = gr.Textbox(
|
| 128 |
label="Input Text",
|
| 129 |
placeholder="Enter text here to translate...",
|
| 130 |
-
lines=8,
|
| 131 |
-
max_lines=15
|
| 132 |
)
|
| 133 |
with gr.Row():
|
| 134 |
translate_btn = gr.Button("π Translate", variant="primary", scale=3)
|
|
@@ -137,8 +142,7 @@ with gr.Blocks(
|
|
| 137 |
with gr.Column():
|
| 138 |
output_text = gr.Textbox(
|
| 139 |
label="Translated Text",
|
| 140 |
-
lines=8,
|
| 141 |
-
max_lines=15,
|
| 142 |
interactive=False,
|
| 143 |
elem_id="translated-output"
|
| 144 |
)
|
|
@@ -147,7 +151,6 @@ with gr.Blocks(
|
|
| 147 |
copy_btn = gr.Button("π Copy to Clipboard", scale=2)
|
| 148 |
speak_btn = gr.Button("π Speak Translation", scale=2)
|
| 149 |
|
| 150 |
-
# ββ Event handlers ββ
|
| 151 |
def do_translate(text, src, tgt):
|
| 152 |
result, info = translate_text(text, src, tgt)
|
| 153 |
return result, info
|
|
@@ -155,11 +158,6 @@ with gr.Blocks(
|
|
| 155 |
def clear_all():
|
| 156 |
return "", "", ""
|
| 157 |
|
| 158 |
-
def copy_output(text):
|
| 159 |
-
if text.strip():
|
| 160 |
-
return gr.update(value="β
Copied! (select text manually on web)", interactive=False)
|
| 161 |
-
return gr.update(value="π Copy to Clipboard")
|
| 162 |
-
|
| 163 |
translate_btn.click(
|
| 164 |
fn=do_translate,
|
| 165 |
inputs=[input_text, source_lang, target_lang],
|
|
@@ -171,8 +169,8 @@ with gr.Blocks(
|
|
| 171 |
outputs=[output_text, detection_info]
|
| 172 |
)
|
| 173 |
clear_btn.click(fn=clear_all, outputs=[input_text, output_text, detection_info])
|
| 174 |
-
copy_btn.click(fn=
|
| 175 |
-
speak_btn.click(fn=None, js=TTS_JS)
|
| 176 |
|
| 177 |
gr.HTML("""
|
| 178 |
<div class="footer">
|
|
@@ -181,4 +179,4 @@ with gr.Blocks(
|
|
| 181 |
""")
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
demo.launch(
|
|
|
|
| 1 |
"""
|
| 2 |
CodeAlpha Internship - Task 1: Language Translation Tool
|
| 3 |
+
Uses: Groq API (LLM inference) + langdetect + Gradio (UI)
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
| 8 |
from langdetect import detect, DetectorFactory
|
| 9 |
+
import os
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
load_dotenv() # Load .env file locally
|
| 13 |
+
DetectorFactory.seed = 0
|
| 14 |
|
| 15 |
# βββ CONFIGURATION ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 17 |
+
MODEL = "llama3-8b-8192"
|
| 18 |
|
| 19 |
LANGUAGES = [
|
| 20 |
"Auto-Detect",
|
|
|
|
| 41 |
if target_lang == "Auto-Detect":
|
| 42 |
return "β οΈ Please select a target language.", ""
|
| 43 |
|
| 44 |
+
if not GROQ_API_KEY:
|
| 45 |
+
return "β οΈ GROQ_API_KEY is not set. Add it in .env file or HF Space Secrets.", ""
|
| 46 |
|
|
|
|
| 47 |
detected_info = ""
|
| 48 |
if source_lang == "Auto-Detect":
|
| 49 |
try:
|
|
|
|
| 55 |
source_lang = "the source language"
|
| 56 |
detected_info = "π Could not detect language, letting AI handle it."
|
| 57 |
|
|
|
|
| 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\nText to translate:\n{input_text}"
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
try:
|
|
|
|
| 71 |
)
|
| 72 |
translated = response.choices[0].message.content.strip()
|
| 73 |
return translated, detected_info
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
return f"β Translation error: {str(e)}", ""
|
| 76 |
|
| 77 |
|
| 78 |
+
# βββ JS FUNCTIONS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
TTS_JS = """
|
| 80 |
function speakText() {
|
| 81 |
const outputBox = document.querySelector('#translated-output textarea');
|
|
|
|
| 88 |
}
|
| 89 |
"""
|
| 90 |
|
| 91 |
+
COPY_JS = """
|
| 92 |
+
function copyText() {
|
| 93 |
+
const outputBox = document.querySelector('#translated-output textarea');
|
| 94 |
+
if (!outputBox || !outputBox.value.trim()) {
|
| 95 |
+
alert("Nothing to copy yet!");
|
| 96 |
+
return;
|
| 97 |
+
}
|
| 98 |
+
navigator.clipboard.writeText(outputBox.value).then(() => {
|
| 99 |
+
alert("Copied to clipboard!");
|
| 100 |
+
});
|
| 101 |
+
}
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
# βββ GRADIO UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 105 |
with gr.Blocks(
|
| 106 |
title="Language Translator β CodeAlpha",
|
| 107 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 108 |
css="""
|
|
|
|
| 109 |
.title-block { text-align: center; padding: 10px 0 20px; }
|
| 110 |
#translated-output textarea { font-size: 16px; }
|
| 111 |
.footer { text-align: center; font-size: 12px; color: #888; margin-top: 20px; }
|
|
|
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
source_lang = gr.Dropdown(
|
| 124 |
+
choices=LANGUAGES, value="Auto-Detect", label="Source Language", scale=1
|
|
|
|
|
|
|
|
|
|
| 125 |
)
|
| 126 |
target_lang = gr.Dropdown(
|
| 127 |
choices=[l for l in LANGUAGES if l != "Auto-Detect"],
|
| 128 |
+
value="Urdu", label="Target Language", scale=1
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
with gr.Row():
|
|
|
|
| 133 |
input_text = gr.Textbox(
|
| 134 |
label="Input Text",
|
| 135 |
placeholder="Enter text here to translate...",
|
| 136 |
+
lines=8, max_lines=15
|
|
|
|
| 137 |
)
|
| 138 |
with gr.Row():
|
| 139 |
translate_btn = gr.Button("π Translate", variant="primary", scale=3)
|
|
|
|
| 142 |
with gr.Column():
|
| 143 |
output_text = gr.Textbox(
|
| 144 |
label="Translated Text",
|
| 145 |
+
lines=8, max_lines=15,
|
|
|
|
| 146 |
interactive=False,
|
| 147 |
elem_id="translated-output"
|
| 148 |
)
|
|
|
|
| 151 |
copy_btn = gr.Button("π Copy to Clipboard", scale=2)
|
| 152 |
speak_btn = gr.Button("π Speak Translation", scale=2)
|
| 153 |
|
|
|
|
| 154 |
def do_translate(text, src, tgt):
|
| 155 |
result, info = translate_text(text, src, tgt)
|
| 156 |
return result, info
|
|
|
|
| 158 |
def clear_all():
|
| 159 |
return "", "", ""
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
translate_btn.click(
|
| 162 |
fn=do_translate,
|
| 163 |
inputs=[input_text, source_lang, target_lang],
|
|
|
|
| 169 |
outputs=[output_text, detection_info]
|
| 170 |
)
|
| 171 |
clear_btn.click(fn=clear_all, outputs=[input_text, output_text, detection_info])
|
| 172 |
+
copy_btn.click(fn=None, js=COPY_JS)
|
| 173 |
+
speak_btn.click(fn=None, js=TTS_JS)
|
| 174 |
|
| 175 |
gr.HTML("""
|
| 176 |
<div class="footer">
|
|
|
|
| 179 |
""")
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
demo.launch()
|