Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
-
from datetime import datetime
|
| 5 |
|
| 6 |
-
# π Load Groq API key
|
| 7 |
api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
-
if not api_key:
|
| 9 |
-
raise RuntimeError("β GROQ_API_KEY is missing. Set it in your environment.")
|
| 10 |
|
| 11 |
# β
Groq-compatible OpenAI client
|
| 12 |
client = OpenAI(
|
|
@@ -14,16 +11,14 @@ client = OpenAI(
|
|
| 14 |
base_url="https://api.groq.com/openai/v1"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# π§
|
| 18 |
translation_history = []
|
| 19 |
|
| 20 |
-
# π Translation function
|
| 21 |
def translate_text(text, target_lang):
|
| 22 |
if not text.strip() or not target_lang.strip():
|
| 23 |
-
return "β οΈ Please enter both English text and
|
| 24 |
|
| 25 |
-
prompt = f"Translate the following English text to {target_lang}
|
| 26 |
-
\n{text}"
|
| 27 |
|
| 28 |
try:
|
| 29 |
response = client.chat.completions.create(
|
|
@@ -31,50 +26,60 @@ def translate_text(text, target_lang):
|
|
| 31 |
messages=[{"role": "user", "content": prompt}],
|
| 32 |
temperature=0.5
|
| 33 |
)
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
translation_history
|
|
|
|
| 39 |
|
| 40 |
-
return
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"β Error: {e}",
|
| 44 |
|
| 45 |
-
#
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
| 47 |
-
gr.Markdown(""
|
| 48 |
-
|
| 49 |
-
Translate English into any major language using Groq's blazing-fast LLaMA 3 model.
|
| 50 |
-
""")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
with gr.Column():
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
choices=["Urdu", "French", "Spanish", "German", "Arabic", "Hindi", "Chinese", "Russian", "Turkish", "Japanese"],
|
| 58 |
-
value="Urdu"
|
| 59 |
-
)
|
| 60 |
-
translate_button = gr.Button("π Translate")
|
| 61 |
with gr.Column():
|
| 62 |
-
output_text = gr.Textbox(label="
|
| 63 |
copy_btn = gr.Button("π Copy to Clipboard")
|
| 64 |
-
download_btn = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
history_box = gr.Textbox(label="History", lines=8, interactive=False)
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
with open(
|
| 72 |
-
f.write(
|
| 73 |
-
return
|
| 74 |
|
| 75 |
-
|
| 76 |
-
copy_btn.click(lambda x: x, inputs=output_text, outputs=output_text, show_progress=False)
|
| 77 |
-
download_btn.click(download_translation, inputs=output_text, outputs=gr.File(label="Download Translation"))
|
| 78 |
|
| 79 |
# π Launch app
|
| 80 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# π Load Groq API key
|
| 6 |
api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# β
Groq-compatible OpenAI client
|
| 9 |
client = OpenAI(
|
|
|
|
| 11 |
base_url="https://api.groq.com/openai/v1"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# π§ Translation function with history
|
| 15 |
translation_history = []
|
| 16 |
|
|
|
|
| 17 |
def translate_text(text, target_lang):
|
| 18 |
if not text.strip() or not target_lang.strip():
|
| 19 |
+
return "β οΈ Please enter both English text and a target language.", gr.update(visible=False)
|
| 20 |
|
| 21 |
+
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
response = client.chat.completions.create(
|
|
|
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
| 27 |
temperature=0.5
|
| 28 |
)
|
| 29 |
+
translation = response.choices[0].message.content.strip()
|
| 30 |
|
| 31 |
+
# Add to history (limit 5)
|
| 32 |
+
translation_history.append(f"**{target_lang}**: {translation}")
|
| 33 |
+
if len(translation_history) > 5:
|
| 34 |
+
translation_history.pop(0)
|
| 35 |
|
| 36 |
+
return translation, gr.update(visible=True)
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"β Error: {e}", gr.update(visible=False)
|
| 40 |
|
| 41 |
+
# π Languages list
|
| 42 |
+
language_choices = ["Urdu", "French", "Spanish", "German", "Chinese", "Arabic", "Hindi", "Turkish", "Japanese", "Russian"]
|
| 43 |
+
|
| 44 |
+
# π Gradio UI
|
| 45 |
with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
| 46 |
+
gr.Markdown("# π English Translator App")
|
| 47 |
+
gr.Markdown("Translate any English sentence into your desired language using Groq's **LLaMA3** model.")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
+
text_input = gr.Textbox(label="Enter English Text", placeholder="Type your sentence here...", lines=4)
|
| 52 |
+
language_dropdown = gr.Dropdown(choices=language_choices, label="Translate to", value="Urdu")
|
| 53 |
+
translate_btn = gr.Button("π Translate")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Column():
|
| 55 |
+
output_text = gr.Textbox(label="Translated Text", lines=4)
|
| 56 |
copy_btn = gr.Button("π Copy to Clipboard")
|
| 57 |
+
download_btn = gr.Button("β¬οΈ Download Translation", visible=False)
|
| 58 |
+
|
| 59 |
+
with gr.Accordion("π Translation History", open=False):
|
| 60 |
+
history_box = gr.Markdown("")
|
| 61 |
+
|
| 62 |
+
# π Bind functions
|
| 63 |
+
translate_btn.click(
|
| 64 |
+
translate_text,
|
| 65 |
+
inputs=[text_input, language_dropdown],
|
| 66 |
+
outputs=[output_text, download_btn]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
def update_history():
|
| 70 |
+
return "\n\n".join(translation_history)
|
| 71 |
+
|
| 72 |
+
translate_btn.click(fn=update_history, outputs=[history_box])
|
| 73 |
|
| 74 |
+
copy_btn.click(fn=None, inputs=[], outputs=[], _js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Translated Text\"]').value)")
|
|
|
|
| 75 |
|
| 76 |
+
def download_text(translated_text):
|
| 77 |
+
path = "/mnt/data/translation.txt"
|
| 78 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 79 |
+
f.write(translated_text)
|
| 80 |
+
return path
|
| 81 |
|
| 82 |
+
download_btn.click(download_text, inputs=[output_text], outputs=[download_btn])
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# π Launch app
|
| 85 |
app.launch()
|