Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,22 @@ import os
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
client = OpenAI(
|
| 10 |
api_key=api_key,
|
| 11 |
base_url="https://api.groq.com/openai/v1"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
if not text.strip() or not target_lang.strip():
|
| 17 |
-
return "β οΈ Please enter both English text and
|
| 18 |
|
| 19 |
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
| 20 |
|
|
@@ -24,47 +27,50 @@ def translate_text(text, target_lang):
|
|
| 24 |
messages=[{"role": "user", "content": prompt}],
|
| 25 |
temperature=0.5
|
| 26 |
)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
-
return f"β Error: {str(e)}"
|
| 30 |
|
| 31 |
-
#
|
| 32 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 33 |
-
gr.Markdown(
|
| 34 |
-
|
| 35 |
-
# π English Language Translator
|
| 36 |
-
Translate any English text into your desired language using **Groq's LLaMA3 model**.
|
| 37 |
-
"""
|
| 38 |
-
)
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
with gr.Column(scale=1):
|
| 42 |
-
gr.
|
| 43 |
-
text_input = gr.Textbox(
|
| 44 |
-
placeholder="e.g., The construction site was damaged after the storm.",
|
| 45 |
-
lines=4,
|
| 46 |
-
label="English Text"
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
with gr.Column(scale=1):
|
| 50 |
-
gr.
|
| 51 |
-
lang_input = gr.Textbox(
|
| 52 |
-
placeholder="e.g., Urdu, Spanish, French",
|
| 53 |
-
label="Target Language"
|
| 54 |
-
)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
translate_btn = gr.Button("π Translate")
|
| 58 |
clear_btn = gr.Button("π§Ή Clear")
|
| 59 |
|
| 60 |
output_box = gr.Textbox(label="π Translated Text", lines=6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
gr.Markdown("---")
|
| 67 |
-
gr.Markdown("β‘ Powered by [Groq](https://groq.com) | Built with β€οΈ
|
| 68 |
|
| 69 |
-
#
|
| 70 |
app.launch()
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load API key securely
|
| 6 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
|
| 8 |
+
# Initialize Groq-compatible OpenAI client
|
| 9 |
client = OpenAI(
|
| 10 |
api_key=api_key,
|
| 11 |
base_url="https://api.groq.com/openai/v1"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Store session-based history
|
| 15 |
+
translation_history = []
|
| 16 |
+
|
| 17 |
+
# Translation logic
|
| 18 |
+
def translate_and_save(text, target_lang):
|
| 19 |
if not text.strip() or not target_lang.strip():
|
| 20 |
+
return "β οΈ Please enter both English text and target language.", translation_history
|
| 21 |
|
| 22 |
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
| 23 |
|
|
|
|
| 27 |
messages=[{"role": "user", "content": prompt}],
|
| 28 |
temperature=0.5
|
| 29 |
)
|
| 30 |
+
translated = response.choices[0].message.content.strip()
|
| 31 |
+
|
| 32 |
+
# Save to history
|
| 33 |
+
translation_history.append({
|
| 34 |
+
"English Text": text,
|
| 35 |
+
"Language": target_lang,
|
| 36 |
+
"Translation": translated
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
return translated, translation_history
|
| 40 |
except Exception as e:
|
| 41 |
+
return f"β Error: {str(e)}", translation_history
|
| 42 |
|
| 43 |
+
# UI
|
| 44 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 45 |
+
gr.Markdown("# π English Language Translator")
|
| 46 |
+
gr.Markdown("Translate any English sentence into your desired language using **Groq's LLaMA3 model**.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=1):
|
| 50 |
+
text_input = gr.Textbox(label="βοΈ English Text", placeholder="Enter your English sentence here...", lines=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Column(scale=1):
|
| 52 |
+
lang_input = gr.Textbox(label="π Target Language", placeholder="e.g., Urdu, French, Spanish")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
translate_btn = gr.Button("π Translate")
|
| 56 |
clear_btn = gr.Button("π§Ή Clear")
|
| 57 |
|
| 58 |
output_box = gr.Textbox(label="π Translated Text", lines=6)
|
| 59 |
+
history_table = gr.Dataframe(headers=["English Text", "Language", "Translation"], interactive=False)
|
| 60 |
+
|
| 61 |
+
translate_btn.click(
|
| 62 |
+
fn=translate_and_save,
|
| 63 |
+
inputs=[text_input, lang_input],
|
| 64 |
+
outputs=[output_box, history_table]
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
clear_btn.click(
|
| 68 |
+
lambda: ("", "", "", translation_history),
|
| 69 |
+
outputs=[text_input, lang_input, output_box, history_table]
|
| 70 |
+
)
|
| 71 |
|
| 72 |
gr.Markdown("---")
|
| 73 |
+
gr.Markdown("β‘ Powered by [Groq](https://groq.com) | Built with β€οΈ using [Gradio](https://gradio.app)")
|
| 74 |
|
| 75 |
+
# Launch the app
|
| 76 |
app.launch()
|