Spaces:
Sleeping
Sleeping
Download app.py from faiqyy/Translation_app: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/faiqyy/Translation_app/resolve/main/app.py
- Command line
-
hf download hf://spaces/faiqyy/Translation_app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/faiqyy/Translation_app/resolve/main/app.py
1.41 kB
| import os | |
| import gradio as gr | |
| import groq | |
| # Get API key from environment variable | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| client = groq.Groq(api_key=GROQ_API_KEY) | |
| def translate_text(text, target_language): | |
| prompt = ( | |
| f"Translate the following English text into {target_language}:\n\n" | |
| f"\"{text}\"\n\n" | |
| f"Only provide the translated text in {target_language}, without explanation." | |
| ) | |
| try: | |
| response = client.chat.completions.create( | |
| model="llama3-70b-8192", | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.3, | |
| max_tokens=300, | |
| ) | |
| return response.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| def translate_interface(text, language): | |
| if not text or not language: | |
| return "Please enter both text and target language." | |
| return translate_text(text, language) | |
| iface = gr.Interface( | |
| fn=translate_interface, | |
| inputs=[ | |
| gr.Textbox(label="Enter English Text", lines=4, placeholder="Type English sentence here..."), | |
| gr.Textbox(label="Target Language", placeholder="e.g. French, Hindi, Arabic") | |
| ], | |
| outputs="text", | |
| title="🌍 English to Any Language Translator (GROQ + LLaMA 3)", | |
| description="Translate English text into any desired language using GROQ's LLaMA 3 model." | |
| ) | |
| iface.launch() | |