Mahrukhh commited on
Commit
c16d378
Β·
verified Β·
1 Parent(s): aa816ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from openai import OpenAI
3
  import gradio as gr
4
 
@@ -11,13 +12,20 @@ client = OpenAI(
11
  base_url="https://api.groq.com/openai/v1"
12
  )
13
 
14
- # Store session-based history
15
  translation_history = []
16
 
17
- # Translation logic
 
 
 
 
 
 
 
18
  def translate_and_save(text, target_lang):
19
  if not text.strip() or not target_lang.strip():
20
- return "⚠️ Please enter both English text and target language.", translation_history
21
 
22
  prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
23
 
@@ -29,18 +37,21 @@ def translate_and_save(text, target_lang):
29
  )
30
  translated = response.choices[0].message.content.strip()
31
 
32
- # Save to history
33
  translation_history.append({
34
  "English Text": text,
35
  "Language": target_lang,
36
  "Translation": translated
37
  })
38
 
39
- return translated, translation_history
 
 
 
40
  except Exception as e:
41
- return f"❌ Error: {str(e)}", translation_history
42
 
43
- # UI
44
  with gr.Blocks(theme=gr.themes.Soft()) as app:
45
  gr.Markdown("# 🌍 English Language Translator")
46
  gr.Markdown("Translate any English sentence into your desired language using **Groq's LLaMA3 model**.")
@@ -49,24 +60,30 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
49
  with gr.Column(scale=1):
50
  text_input = gr.Textbox(label="✏️ English Text", placeholder="Enter your English sentence here...", lines=4)
51
  with gr.Column(scale=1):
52
- lang_input = gr.Textbox(label="🌐 Target Language", placeholder="e.g., Urdu, French, Spanish")
 
 
 
 
 
 
53
 
54
  with gr.Row():
55
  translate_btn = gr.Button("🌐 Translate")
56
  clear_btn = gr.Button("🧹 Clear")
57
 
58
  output_box = gr.Textbox(label="πŸ“ Translated Text", lines=6)
59
- history_table = gr.Dataframe(headers=["English Text", "Language", "Translation"], interactive=False)
60
 
61
  translate_btn.click(
62
  fn=translate_and_save,
63
- inputs=[text_input, lang_input],
64
  outputs=[output_box, history_table]
65
  )
66
 
67
  clear_btn.click(
68
- lambda: ("", "", "", translation_history),
69
- outputs=[text_input, lang_input, output_box, history_table]
70
  )
71
 
72
  gr.Markdown("---")
 
1
  import os
2
+ import pandas as pd
3
  from openai import OpenAI
4
  import gradio as gr
5
 
 
12
  base_url="https://api.groq.com/openai/v1"
13
  )
14
 
15
+ # In-memory translation history
16
  translation_history = []
17
 
18
+ # Full language list
19
+ language_list = [
20
+ "Urdu", "Hindi", "Arabic", "Spanish", "French", "German", "Chinese", "Japanese",
21
+ "Korean", "Turkish", "Russian", "Portuguese", "Italian", "Bengali", "Tamil",
22
+ "Punjabi", "Greek", "Dutch", "Polish", "Swedish", "Norwegian", "Hebrew"
23
+ ]
24
+
25
+ # Translation function with history tracking
26
  def translate_and_save(text, target_lang):
27
  if not text.strip() or not target_lang.strip():
28
+ return "⚠️ Please enter both English text and target language.", pd.DataFrame(translation_history)
29
 
30
  prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
31
 
 
37
  )
38
  translated = response.choices[0].message.content.strip()
39
 
40
+ # Save to history as a dict
41
  translation_history.append({
42
  "English Text": text,
43
  "Language": target_lang,
44
  "Translation": translated
45
  })
46
 
47
+ # Convert to DataFrame for display
48
+ history_df = pd.DataFrame(translation_history)
49
+
50
+ return translated, history_df
51
  except Exception as e:
52
+ return f"❌ Error: {str(e)}", pd.DataFrame(translation_history)
53
 
54
+ # Build the Gradio UI
55
  with gr.Blocks(theme=gr.themes.Soft()) as app:
56
  gr.Markdown("# 🌍 English Language Translator")
57
  gr.Markdown("Translate any English sentence into your desired language using **Groq's LLaMA3 model**.")
 
60
  with gr.Column(scale=1):
61
  text_input = gr.Textbox(label="✏️ English Text", placeholder="Enter your English sentence here...", lines=4)
62
  with gr.Column(scale=1):
63
+ lang_dropdown = gr.Dropdown(
64
+ choices=language_list,
65
+ label="🌐 Target Language",
66
+ info="Select a language",
67
+ allow_custom_value=True,
68
+ interactive=True
69
+ )
70
 
71
  with gr.Row():
72
  translate_btn = gr.Button("🌐 Translate")
73
  clear_btn = gr.Button("🧹 Clear")
74
 
75
  output_box = gr.Textbox(label="πŸ“ Translated Text", lines=6)
76
+ history_table = gr.Dataframe(label="πŸ“œ Translation History", interactive=False)
77
 
78
  translate_btn.click(
79
  fn=translate_and_save,
80
+ inputs=[text_input, lang_dropdown],
81
  outputs=[output_box, history_table]
82
  )
83
 
84
  clear_btn.click(
85
+ lambda: ("", "", "", pd.DataFrame(translation_history)),
86
+ outputs=[text_input, lang_dropdown, output_box, history_table]
87
  )
88
 
89
  gr.Markdown("---")