Mahrukhh commited on
Commit
883c61c
Β·
verified Β·
1 Parent(s): ebafdf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -37
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 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
-
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
- translated = response.choices[0].message.content.strip()
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}", gr.update(visible=False), "", "\n".join(translation_history)
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_box = gr.Textbox(label="", lines=10, interactive=False)
 
59
 
60
- # Bind the translate button
61
- translate_btn.click(
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()