Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from groq import Groq
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load API key from environment variable (secure)
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
+
|
| 10 |
+
language_options = [
|
| 11 |
+
"Spanish", "French", "German", "Arabic", "Chinese", "Urdu", "Hindi", "Turkish", "Russian", "Japanese"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def translate_text(text, target_lang):
|
| 15 |
+
if not text.strip():
|
| 16 |
+
return "Please enter some text to translate."
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model="llama3-8b-8192",
|
| 21 |
+
messages=[
|
| 22 |
+
{
|
| 23 |
+
"role": "system",
|
| 24 |
+
"content": f"You are a translation assistant. Translate all English text into {target_lang}."
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": text
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
temperature=0.3
|
| 32 |
+
)
|
| 33 |
+
return response.choices[0].message.content.strip()
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"❌ Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("## 🌐 English Text Translator using Groq API")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
input_text = gr.Textbox(label="Enter English Text", lines=5)
|
| 42 |
+
output_text = gr.Textbox(label="Translated Text", lines=5)
|
| 43 |
+
|
| 44 |
+
target_language = gr.Dropdown(language_options, label="Select Target Language", value="Urdu")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
translate_btn = gr.Button("Translate")
|
| 48 |
+
clear_btn = gr.Button("Clear")
|
| 49 |
+
|
| 50 |
+
translate_btn.click(fn=translate_text, inputs=[input_text, target_language], outputs=output_text)
|
| 51 |
+
clear_btn.click(fn=lambda: ("", ""), inputs=[], outputs=[input_text, output_text])
|
| 52 |
+
|
| 53 |
+
demo.launch()
|