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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
def initialize_groq_client():
|
| 7 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
+
if not api_key:
|
| 9 |
+
raise ValueError("GROQ_API_KEY environment variable not set")
|
| 10 |
+
return Groq(api_key=api_key)
|
| 11 |
+
|
| 12 |
+
def translate_to_swahili(text, temperature=0.3):
|
| 13 |
+
"""
|
| 14 |
+
Translate English text to Swahili using Groq's Llama 4
|
| 15 |
+
"""
|
| 16 |
+
if not text.strip():
|
| 17 |
+
return "Please enter some text to translate."
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
client = initialize_groq_client()
|
| 21 |
+
|
| 22 |
+
# Construct the prompt for translation
|
| 23 |
+
prompt = f"""You are a professional translator. Translate the following English text to Swahili.
|
| 24 |
+
Provide only the Swahili translation, nothing else.
|
| 25 |
+
|
| 26 |
+
English text: {text}
|
| 27 |
+
|
| 28 |
+
Swahili translation:"""
|
| 29 |
+
|
| 30 |
+
completion = client.chat.completions.create(
|
| 31 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 32 |
+
messages=[
|
| 33 |
+
{
|
| 34 |
+
"role": "user",
|
| 35 |
+
"content": prompt
|
| 36 |
+
}
|
| 37 |
+
],
|
| 38 |
+
temperature=temperature,
|
| 39 |
+
max_completion_tokens=1024,
|
| 40 |
+
top_p=1,
|
| 41 |
+
stream=False,
|
| 42 |
+
stop=None,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return completion.choices[0].message.content.strip()
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error during translation: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def translate_with_streaming(text, temperature=0.3):
|
| 51 |
+
"""
|
| 52 |
+
Translate with streaming response for better UX
|
| 53 |
+
"""
|
| 54 |
+
if not text.strip():
|
| 55 |
+
yield "Please enter some text to translate."
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
client = initialize_groq_client()
|
| 60 |
+
|
| 61 |
+
prompt = f"""You are a professional translator. Translate the following English text to Swahili.
|
| 62 |
+
Provide only the Swahili translation, nothing else.
|
| 63 |
+
|
| 64 |
+
English text: {text}
|
| 65 |
+
|
| 66 |
+
Swahili translation:"""
|
| 67 |
+
|
| 68 |
+
completion = client.chat.completions.create(
|
| 69 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 70 |
+
messages=[
|
| 71 |
+
{
|
| 72 |
+
"role": "user",
|
| 73 |
+
"content": prompt
|
| 74 |
+
}
|
| 75 |
+
],
|
| 76 |
+
temperature=temperature,
|
| 77 |
+
max_completion_tokens=1024,
|
| 78 |
+
top_p=1,
|
| 79 |
+
stream=True,
|
| 80 |
+
stop=None,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
full_response = ""
|
| 84 |
+
for chunk in completion:
|
| 85 |
+
if chunk.choices[0].delta.content:
|
| 86 |
+
full_response += chunk.choices[0].delta.content
|
| 87 |
+
yield full_response
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
yield f"Error during translation: {str(e)}"
|
| 91 |
+
|
| 92 |
+
# Create Gradio interface
|
| 93 |
+
def create_interface():
|
| 94 |
+
with gr.Blocks(title="English to Swahili Translator", theme=gr.themes.Soft()) as iface:
|
| 95 |
+
gr.Markdown("# 🌍 English to Swahili Translator")
|
| 96 |
+
gr.Markdown("Powered by Groq's Llama 4 - Translate English text to Swahili")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
input_text = gr.Textbox(
|
| 101 |
+
label="English Text",
|
| 102 |
+
placeholder="Enter English text to translate...",
|
| 103 |
+
lines=5,
|
| 104 |
+
max_lines=10
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
temperature = gr.Slider(
|
| 108 |
+
minimum=0.1,
|
| 109 |
+
maximum=1.0,
|
| 110 |
+
value=0.3,
|
| 111 |
+
step=0.1,
|
| 112 |
+
label="Temperature (creativity level)",
|
| 113 |
+
info="Lower values = more consistent, Higher values = more creative"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
translate_btn = gr.Button("🔄 Translate", variant="primary")
|
| 118 |
+
clear_btn = gr.Button("🗑️ Clear")
|
| 119 |
+
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
output_text = gr.Textbox(
|
| 122 |
+
label="Swahili Translation",
|
| 123 |
+
lines=5,
|
| 124 |
+
max_lines=10,
|
| 125 |
+
interactive=False
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Examples
|
| 129 |
+
gr.Examples(
|
| 130 |
+
examples=[
|
| 131 |
+
["Hello, how are you?"],
|
| 132 |
+
["I love learning new languages."],
|
| 133 |
+
["The weather is beautiful today."],
|
| 134 |
+
["Thank you for your help."],
|
| 135 |
+
["What time is it?"]
|
| 136 |
+
],
|
| 137 |
+
inputs=[input_text],
|
| 138 |
+
label="Try these examples:"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Event handlers
|
| 142 |
+
translate_btn.click(
|
| 143 |
+
fn=translate_with_streaming,
|
| 144 |
+
inputs=[input_text, temperature],
|
| 145 |
+
outputs=[output_text],
|
| 146 |
+
show_progress=True
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
clear_btn.click(
|
| 150 |
+
fn=lambda: ("", ""),
|
| 151 |
+
outputs=[input_text, output_text]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Allow Enter key to trigger translation
|
| 155 |
+
input_text.submit(
|
| 156 |
+
fn=translate_with_streaming,
|
| 157 |
+
inputs=[input_text, temperature],
|
| 158 |
+
outputs=[output_text],
|
| 159 |
+
show_progress=True
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return iface
|
| 163 |
+
|
| 164 |
+
# For local testing
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
# For local development, you can set the API key here (NOT recommended for production)
|
| 167 |
+
# os.environ["GROQ_API_KEY"] = "your_api_key_here"
|
| 168 |
+
|
| 169 |
+
iface = create_interface()
|
| 170 |
+
iface.launch(
|
| 171 |
+
debug=True,
|
| 172 |
+
share=False, # Set to True if you want a public link
|
| 173 |
+
server_name="0.0.0.0",
|
| 174 |
+
server_port=7860
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# For Hugging Face Spaces deployment
|
| 178 |
+
def launch_hf_space():
|
| 179 |
+
iface = create_interface()
|
| 180 |
+
iface.launch()
|
| 181 |
+
|
| 182 |
+
# Uncomment the line below for Hugging Face deployment
|
| 183 |
+
# launch_hf_space()
|