Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from groq import Groq | |
| import os | |
| # Use your API key from environment variable | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| def translate_text(text, target_language): | |
| prompt = f"Translate the following English sentence into {target_language}:\n\n\"{text}\"" | |
| try: | |
| response = client.chat.completions.create( | |
| messages=[ | |
| {"role": "system", "content": "You are a multilingual translator."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| model="llama3-8b-8192" | |
| ) | |
| return response.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| iface = gr.Interface( | |
| fn=translate_text, | |
| inputs=[ | |
| gr.Textbox(lines=2, label="Enter English text"), | |
| gr.Textbox(label="Target Language (e.g., Spanish, French, Urdu)") | |
| ], | |
| outputs=gr.Textbox(label="Translated Text"), | |
| title="🌍 Translation App", | |
| description="Translate English text into any language using Groq LLM" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |