Spaces:
Sleeping
Sleeping
File size: 1,692 Bytes
bf16e39 |
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 |
from groq import Groq
import gradio as gr
import os
# Load API key from environment variable (secure)
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
client = Groq(api_key=GROQ_API_KEY)
language_options = [
"Spanish", "French", "German", "Arabic", "Chinese", "Urdu", "Hindi", "Turkish", "Russian", "Japanese"
]
def translate_text(text, target_lang):
if not text.strip():
return "Please enter some text to translate."
try:
response = client.chat.completions.create(
model="llama3-8b-8192",
messages=[
{
"role": "system",
"content": f"You are a translation assistant. Translate all English text into {target_lang}."
},
{
"role": "user",
"content": text
}
],
temperature=0.3
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"❌ Error: {str(e)}"
with gr.Blocks() as demo:
gr.Markdown("## 🌐 English Text Translator using Groq API")
with gr.Row():
input_text = gr.Textbox(label="Enter English Text", lines=5)
output_text = gr.Textbox(label="Translated Text", lines=5)
target_language = gr.Dropdown(language_options, label="Select Target Language", value="Urdu")
with gr.Row():
translate_btn = gr.Button("Translate")
clear_btn = gr.Button("Clear")
translate_btn.click(fn=translate_text, inputs=[input_text, target_language], outputs=output_text)
clear_btn.click(fn=lambda: ("", ""), inputs=[], outputs=[input_text, output_text])
demo.launch()
|