Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
#
|
| 6 |
groq_api_key = "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"
|
| 7 |
client = Groq(api_key=groq_api_key)
|
| 8 |
|
|
@@ -12,31 +12,41 @@ def translate_with_groq(text, src_lang, tgt_lang):
|
|
| 12 |
if src_lang == tgt_lang:
|
| 13 |
return text
|
| 14 |
|
| 15 |
-
prompt =
|
|
|
|
|
|
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
return translated
|
| 25 |
except Exception as e:
|
| 26 |
-
return f"Error: {
|
| 27 |
-
|
| 28 |
-
# Gradio UI
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
gr.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# Initialize Groq client
|
| 6 |
groq_api_key = "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"
|
| 7 |
client = Groq(api_key=groq_api_key)
|
| 8 |
|
|
|
|
| 12 |
if src_lang == tgt_lang:
|
| 13 |
return text
|
| 14 |
|
| 15 |
+
prompt = (
|
| 16 |
+
f"You are a translation assistant. Translate the following text from {src_lang} to {tgt_lang}:\n\n\"{text}\""
|
| 17 |
+
)
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
resp = client.chat.completions.create(
|
| 21 |
+
model="mistral-saba-24b",
|
| 22 |
+
messages=[
|
| 23 |
+
{"role": "system", "content": "You are a multilingual translation assistant."},
|
| 24 |
+
{"role": "user", "content": prompt}
|
| 25 |
+
],
|
| 26 |
+
temperature=0.2,
|
| 27 |
+
max_completion_tokens=512
|
| 28 |
)
|
| 29 |
+
return resp.choices[0].message.content.strip()
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
+
return f"β Error: {e}"
|
| 32 |
+
|
| 33 |
+
# Gradio UI (Blocks with improved layout)
|
| 34 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", font="poppins")) as demo:
|
| 35 |
+
gr.Markdown("# π AI Multilingual Translator")
|
| 36 |
+
gr.Markdown("Translate between multiple languages using **Groq's Mistral-Saba 24B** model.")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
with gr.Column():
|
| 40 |
+
input_text = gr.Textbox(label="π Input Text", lines=5, placeholder="Type your text here...")
|
| 41 |
+
src_lang = gr.Dropdown(lang_options, value="English", label="π From")
|
| 42 |
+
tgt_lang = gr.Dropdown(lang_options, value="Urdu", label="π To")
|
| 43 |
+
translate_btn = gr.Button("π Translate", variant="primary")
|
| 44 |
+
with gr.Column():
|
| 45 |
+
output_text = gr.Textbox(label="β
Translated Text", lines=5)
|
| 46 |
+
|
| 47 |
+
gr.Markdown("---\nPowered by Groq & Gradio")
|
| 48 |
+
|
| 49 |
+
translate_btn.click(translate_with_groq, inputs=[input_text, src_lang, tgt_lang], outputs=output_text)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|