File size: 4,898 Bytes
c70227e 47725da c70227e 47725da |
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import gradio as gr
import os
from groq import Groq
def initialize_groq_client():
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("GROQ_API_KEY environment variable not set")
return Groq(api_key=api_key)
def translate_to_swahili(text, temperature=0.3):
if not text.strip():
return "Please enter some text to translate."
try:
client = initialize_groq_client()
prompt = f"""You are a professional translator. Translate the following English text to Swahili.
Provide only the Swahili translation, nothing else.
English text: {text}
Swahili translation:"""
completion = client.chat.completions.create(
model="meta-llama/llama-4-scout-17b-16e-instruct",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=temperature,
max_completion_tokens=1024,
top_p=1,
stream=False,
stop=None,
)
return completion.choices[0].message.content.strip()
except Exception as e:
return f"Error during translation: {str(e)}"
def translate_with_streaming(text, temperature=0.3):
if not text.strip():
yield "Please enter some text to translate."
return
try:
client = initialize_groq_client()
prompt = f"""You are a professional translator. Translate the following English text to Swahili.
Provide only the Swahili translation, nothing else.
English text: {text}
Swahili translation:"""
completion = client.chat.completions.create(
model="meta-llama/llama-4-scout-17b-16e-instruct",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=temperature,
max_completion_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
full_response = ""
for chunk in completion:
if chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
yield full_response
except Exception as e:
yield f"Error during translation: {str(e)}"
def create_interface():
with gr.Blocks(title="English to Swahili Translator", theme=gr.themes.Soft()) as iface:
gr.Markdown("# π English to Swahili Translator")
gr.Markdown("Powered by Groq's Llama 4 - Translate English text to Swahili")
with gr.Row():
with gr.Column(scale=1):
input_text = gr.Textbox(
label="English Text",
placeholder="Enter English text to translate...",
lines=5,
max_lines=10
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.3,
step=0.1,
label="Temperature (creativity level)",
info="Lower values = more consistent, Higher values = more creative"
)
with gr.Row():
translate_btn = gr.Button("π Translate", variant="primary")
clear_btn = gr.Button("ποΈ Clear")
with gr.Column(scale=1):
output_text = gr.Textbox(
label="Swahili Translation",
lines=5,
max_lines=10,
interactive=False
)
gr.Examples(
examples=[
["Hello, how are you?"],
["I love learning new languages."],
["The weather is beautiful today."],
["Thank you for your help."],
["What time is it?"]
],
inputs=[input_text],
label="Try these examples:"
)
translate_btn.click(
fn=translate_with_streaming,
inputs=[input_text, temperature],
outputs=[output_text],
show_progress=True
)
clear_btn.click(
fn=lambda: ("", ""),
outputs=[input_text, output_text]
)
input_text.submit(
fn=translate_with_streaming,
inputs=[input_text, temperature],
outputs=[output_text],
show_progress=True
)
return iface
if __name__ == "__main__":
iface = create_interface()
iface.launch(
debug=True,
share=False,
server_name="0.0.0.0",
server_port=7860
)
def launch_hf_space():
iface = create_interface()
iface.launch() |