Mahrukhh commited on
Commit
88af9f8
Β·
verified Β·
1 Parent(s): 89e30ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -1,19 +1,23 @@
1
  import os
2
- from openai import OpenAI
3
  import gradio as gr
4
- import datetime
5
 
6
- # πŸ” API Setup
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
- # 🧠 Chat history
11
- chat_history = []
 
 
 
 
 
 
12
 
13
- # πŸ“˜ Translate function
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.", None, chat_history
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
- chat_history.append(f"πŸ—£οΈ **You**: {text}\n🌐 **{target_lang}**: {translated}")
28
-
29
- # Save translated text to file
30
- filename = f"translation_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
 
 
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
- # 🎨 Gradio App Layout
38
- with gr.Blocks(theme=gr.themes.Soft(), css=".gr-button {font-size: 16px !important;}") as app:
39
- gr.Markdown("## 🌍 **English Translator App**")
40
- gr.Markdown("Easily translate English text into any language using **Groq's LLaMA3 model**.")
41
 
42
- with gr.Row():
43
- text_input = gr.Textbox(label="Enter English Text", placeholder="Type something to translate...", lines=3)
44
- lang_input = gr.Textbox(label="Translate to (e.g. Urdu, French, Spanish)", placeholder="e.g. Urdu")
 
45
 
46
- with gr.Row():
 
 
47
  translate_btn = gr.Button("🌐 Translate")
48
- clear_btn = gr.Button("🧹 Clear")
49
 
50
- result_output = gr.Textbox(label="Translated Text", lines=4)
51
- file_output = gr.File(label="πŸ“„ Download Translated Text")
52
- history_display = gr.Markdown("")
 
 
 
 
 
 
 
 
53
 
54
- # ✨ Button handlers
55
  translate_btn.click(
56
  translate_text,
57
- inputs=[text_input, lang_input],
58
- outputs=[result_output, file_output, history_display]
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()