Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,70 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
from openai import OpenAI
|
|
|
|
| 4 |
|
| 5 |
-
# π API key from
|
| 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 |
-
# 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 |
-
|
| 24 |
try:
|
| 25 |
response = client.chat.completions.create(
|
| 26 |
model="llama3-8b-8192",
|
| 27 |
messages=[{"role": "user", "content": prompt}],
|
| 28 |
temperature=0.5
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Save history
|
| 33 |
-
translation_history.append(f"ENGLISH: {text}\nTRANSLATED ({target_lang}): {translated}\n")
|
| 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, "\n---\n".join(translation_history)
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"β Error: {e}"
|
| 43 |
|
| 44 |
-
#
|
| 45 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 46 |
-
gr.Markdown(
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
translate_btn = gr.Button("π Translate")
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
download_btn = gr.File(label="Download Translated Text", visible=False)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
translate_text,
|
| 63 |
-
inputs=[input_text, target_lang],
|
| 64 |
-
outputs=[translated_output, download_btn, download_btn, history_box]
|
| 65 |
-
)
|
| 66 |
|
| 67 |
# π Launch
|
| 68 |
app.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# π Load API key securely (from HF Spaces Secrets or local environment)
|
| 6 |
+
api_key = os.getenv("GROQ_API_KEY") # Set this in your environment
|
| 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 |
# π Translation function
|
| 15 |
def translate_text(text, target_lang):
|
| 16 |
if not text.strip() or not target_lang.strip():
|
| 17 |
+
return "β οΈ Please enter both English text and the target language."
|
| 18 |
|
| 19 |
prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
response = client.chat.completions.create(
|
| 23 |
model="llama3-8b-8192",
|
| 24 |
messages=[{"role": "user", "content": prompt}],
|
| 25 |
temperature=0.5
|
| 26 |
)
|
| 27 |
+
return response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"β Error: {str(e)}"
|
| 30 |
|
| 31 |
+
# π¨ Gradio UI
|
| 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.Markdown("### βοΈ Enter English Text")
|
| 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.Markdown("### π Target Language")
|
| 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 |
+
# Bind button actions
|
| 63 |
+
translate_btn.click(fn=translate_text, inputs=[text_input, lang_input], outputs=[output_box])
|
| 64 |
+
clear_btn.click(lambda: ("", "", ""), outputs=[text_input, lang_input, output_box])
|
| 65 |
|
| 66 |
+
gr.Markdown("---")
|
| 67 |
+
gr.Markdown("β‘ Powered by [Groq](https://groq.com) | Built with β€οΈ and [Gradio](https://gradio.app)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# π Launch
|
| 70 |
app.launch()
|