Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
-
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# π API
|
| 7 |
api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
-
client = OpenAI(api_key=api_key, base_url="https://api.groq.com/openai/v1")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def translate_text(text, target_lang):
|
| 15 |
if not text.strip() or not target_lang.strip():
|
| 16 |
-
return "β οΈ Please enter both text and target language.",
|
| 17 |
|
| 18 |
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
| 19 |
|
|
@@ -24,40 +28,46 @@ def translate_text(text, target_lang):
|
|
| 24 |
temperature=0.5
|
| 25 |
)
|
| 26 |
translated = response.choices[0].message.content.strip()
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
with open(filename, "w", encoding="utf-8") as f:
|
| 32 |
f.write(translated)
|
| 33 |
-
return translated, filename, chat_history
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"β Error: {e}", None, chat_history
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
gr.Markdown("Easily translate English text into any language using **Groq's LLaMA3 model**.")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
with gr.
|
|
|
|
|
|
|
| 47 |
translate_btn = gr.Button("π Translate")
|
| 48 |
-
clear_btn = gr.Button("π§Ή Clear")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
# β¨ Button handlers
|
| 55 |
translate_btn.click(
|
| 56 |
translate_text,
|
| 57 |
-
inputs=[
|
| 58 |
-
outputs=[
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
clear_btn.click(lambda: ("", "", [], ""), None, [text_input, lang_input, result_output, history_display])
|
| 62 |
|
|
|
|
| 63 |
app.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# π API key from environment or HF Secrets
|
| 6 |
api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
+
# β
OpenAI-style Groq client
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
api_key=api_key,
|
| 11 |
+
base_url="https://api.groq.com/openai/v1"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Store translation history
|
| 15 |
+
translation_history = []
|
| 16 |
|
| 17 |
+
# π Translation function
|
| 18 |
def translate_text(text, target_lang):
|
| 19 |
if not text.strip() or not target_lang.strip():
|
| 20 |
+
return "β οΈ Please enter both English text and target language.", "", ""
|
| 21 |
|
| 22 |
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
| 23 |
|
|
|
|
| 28 |
temperature=0.5
|
| 29 |
)
|
| 30 |
translated = response.choices[0].message.content.strip()
|
| 31 |
+
|
| 32 |
+
# Save history
|
| 33 |
+
translation_history.append((text, translated))
|
| 34 |
+
|
| 35 |
+
# Write to file for download
|
| 36 |
+
filename = "/mnt/data/translated_text.txt"
|
| 37 |
with open(filename, "w", encoding="utf-8") as f:
|
| 38 |
f.write(translated)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
return translated, gr.update(visible=True), filename
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"β Error: {e}", gr.update(visible=False), ""
|
|
|
|
| 43 |
|
| 44 |
+
# β¨ Gradio UI
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 46 |
+
gr.Markdown("## π English Translator App")
|
| 47 |
+
gr.Markdown("Translate English text into any language using **Groq's LLaMA3 model**.")
|
| 48 |
|
| 49 |
+
with gr.Group():
|
| 50 |
+
input_text = gr.Textbox(label="Enter English Text", placeholder="Type a sentence...", lines=3)
|
| 51 |
+
target_lang = gr.Textbox(label="Translate to (e.g. Urdu, French, Spanish)", placeholder="e.g. Urdu")
|
| 52 |
translate_btn = gr.Button("π Translate")
|
|
|
|
| 53 |
|
| 54 |
+
translated_output = gr.Textbox(label="Translated Text", lines=4, interactive=False)
|
| 55 |
+
download_btn = gr.File(label="Download Translated Text", visible=False)
|
| 56 |
+
|
| 57 |
+
with gr.Accordion("π Translation History", open=False):
|
| 58 |
+
history_display = gr.HighlightedText(label="Past Translations")
|
| 59 |
+
|
| 60 |
+
def update_history():
|
| 61 |
+
if translation_history:
|
| 62 |
+
return {"value": [{"text": f"{src} β {tgt}"} for src, tgt in translation_history]}
|
| 63 |
+
else:
|
| 64 |
+
return {"value": []}
|
| 65 |
|
|
|
|
| 66 |
translate_btn.click(
|
| 67 |
translate_text,
|
| 68 |
+
inputs=[input_text, target_lang],
|
| 69 |
+
outputs=[translated_output, download_btn, download_btn]
|
| 70 |
+
).then(update_history, outputs=[history_display])
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# π Launch
|
| 73 |
app.launch()
|