Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import groq | |
| # --- Load Groq API Key from environment (use HF Secrets) --- | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| if not GROQ_API_KEY: | |
| raise ValueError("โ ๏ธ GROQ_API_KEY not found. Please add it to your Hugging Face Space secrets.") | |
| client = groq.Groq(api_key=GROQ_API_KEY) | |
| # --- Translation Function --- | |
| def translate(text, target_language): | |
| prompt = ( | |
| f"You are a professional translator. Translate the following English text into {target_language}.\n" | |
| f"Use proper {target_language} script (e.g., Urdu: ุงูุฑุฏู), not phonetics, romanized text, or mixed characters.\n\n" | |
| f"English: {text}\n" | |
| f"{target_language}:" | |
| ) | |
| response = client.chat.completions.create( | |
| model="llama3-8b-8192", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful translation assistant."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content.strip() | |
| # --- Gradio UI --- | |
| language_choices = [ | |
| "French", "Spanish", "German", "Urdu", "Chinese", "Japanese", | |
| "Arabic", "Hindi", "Korean", "Russian" | |
| ] | |
| with gr.Blocks(title="๐ Translation App with Groq + LLaMA3") as demo: | |
| gr.Markdown("## ๐ English Translator") | |
| gr.Markdown("Translate English text into any selected language using **Groq's LLaMA3**.") | |
| with gr.Row(): | |
| input_text = gr.Textbox(label="Enter English Text", placeholder="Type your text here...", lines=4) | |
| language = gr.Dropdown(choices=language_choices, value="Urdu", label="Target Language") | |
| output_text = gr.Textbox(label="Translated Text", lines=4) | |
| translate_btn = gr.Button("๐ Translate") | |
| translate_btn.click(fn=translate, inputs=[input_text, language], outputs=output_text) | |
| # --- Launch --- | |
| demo.launch() | |