Spaces:
Sleeping
Sleeping
File size: 1,124 Bytes
6f34fe8 3e750e4 6f34fe8 3e750e4 6f34fe8 3e750e4 6f34fe8 3e750e4 |
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 |
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()
|