snjev310 commited on
Commit
5079abc
·
verified ·
1 Parent(s): ab98cf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -7,25 +7,22 @@ from peft import PeftModel
7
  # --- Configuration ---
8
  BASE_MODEL_ID = "CohereForAI/aya-101"
9
 
10
- # Map the dropdown options to your 3 Hugging Face Model IDs
11
  MODEL_MAP = {
12
  "English to Angika": "snjev310/aya-101-english-angika",
13
  "Hindi to Angika": "snjev310/aya-101-hindi-angika",
14
  "Angika to English": "snjev310/aya-101-angika-english"
15
  }
16
 
17
- # Load Tokenizer globally (it's small and stays in CPU RAM)
18
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
19
 
20
- @spaces.GPU(duration=180) # 3 minutes to allow for 13B model loading + inference
21
  def translate(text, model_choice):
22
  if not text.strip():
23
  return "Please enter text to translate."
24
 
25
  adapter_id = MODEL_MAP[model_choice]
26
 
27
- # 1. Load Base Model in bfloat16 (Standard for Aya-101)
28
- # Pro ZeroGPU has ~70GB VRAM, so we don't need 4-bit quantization
29
  base_model = AutoModelForSeq2SeqLM.from_pretrained(
30
  BASE_MODEL_ID,
31
  torch_dtype=torch.bfloat16,
@@ -33,16 +30,14 @@ def translate(text, model_choice):
33
  device_map="auto"
34
  )
35
 
36
- # 2. Load the specific PEFT Adapter
37
  model = PeftModel.from_pretrained(base_model, adapter_id)
38
  model.eval()
39
 
40
- # 3. Prepare Input
41
- # Using a prompt format helps the model understand the task
42
- prompt = f"{model_choice}: {text}"
43
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
44
 
45
- # 4. Generate
46
  with torch.no_grad():
47
  outputs = model.generate(
48
  **inputs,
@@ -54,7 +49,7 @@ def translate(text, model_choice):
54
 
55
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
 
57
- # 5. Cleanup (CRITICAL for ZeroGPU to release resources)
58
  del model
59
  del base_model
60
  torch.cuda.empty_cache()
@@ -63,8 +58,8 @@ def translate(text, model_choice):
63
 
64
  # --- Gradio UI ---
65
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
- gr.Markdown("# 🗣️ Angika Multi-Translator")
67
- gr.Markdown("Powered by **Aya-101** and **ZeroGPU**. Select your translation direction below.")
68
 
69
  with gr.Row():
70
  with gr.Column():
@@ -73,25 +68,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  value="English to Angika",
74
  label="Select Translation Mode"
75
  )
76
- input_text = gr.Textbox(
77
- label="Input Text",
78
- placeholder="Type here...",
79
- lines=5
80
- )
81
  submit_btn = gr.Button("Translate", variant="primary")
82
 
83
  with gr.Column():
84
- output_text = gr.Textbox(
85
- label="Translated Text",
86
- lines=5,
87
- interactive=False
88
- )
89
 
90
- submit_btn.click(
91
- fn=translate,
92
- inputs=[input_text, model_dropdown],
93
- outputs=output_text
94
- )
95
 
96
  gr.Examples(
97
  examples=[
@@ -101,4 +84,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
101
  inputs=[input_text, model_dropdown]
102
  )
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  demo.launch()
 
7
  # --- Configuration ---
8
  BASE_MODEL_ID = "CohereForAI/aya-101"
9
 
 
10
  MODEL_MAP = {
11
  "English to Angika": "snjev310/aya-101-english-angika",
12
  "Hindi to Angika": "snjev310/aya-101-hindi-angika",
13
  "Angika to English": "snjev310/aya-101-angika-english"
14
  }
15
 
 
16
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
17
 
18
+ @spaces.GPU(duration=180)
19
  def translate(text, model_choice):
20
  if not text.strip():
21
  return "Please enter text to translate."
22
 
23
  adapter_id = MODEL_MAP[model_choice]
24
 
25
+ # Load Base Model
 
26
  base_model = AutoModelForSeq2SeqLM.from_pretrained(
27
  BASE_MODEL_ID,
28
  torch_dtype=torch.bfloat16,
 
30
  device_map="auto"
31
  )
32
 
33
+ # Load the specific Adapter
34
  model = PeftModel.from_pretrained(base_model, adapter_id)
35
  model.eval()
36
 
37
+ # Prepare Input - Using the direction as a prefix
38
+ prompt = f"translate {model_choice}: {text}"
 
39
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
40
 
 
41
  with torch.no_grad():
42
  outputs = model.generate(
43
  **inputs,
 
49
 
50
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
51
 
52
+ # Cleanup to free GPU for next user
53
  del model
54
  del base_model
55
  torch.cuda.empty_cache()
 
58
 
59
  # --- Gradio UI ---
60
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
+ gr.Markdown("# 🗣️ Angika Machine Translation")
62
+ gr.Markdown("Select your translation direction below. This uses a 13B parameter model.")
63
 
64
  with gr.Row():
65
  with gr.Column():
 
68
  value="English to Angika",
69
  label="Select Translation Mode"
70
  )
71
+ input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=5)
 
 
 
 
72
  submit_btn = gr.Button("Translate", variant="primary")
73
 
74
  with gr.Column():
75
+ output_text = gr.Textbox(label="Translated Text", lines=5, interactive=False)
 
 
 
 
76
 
77
+ submit_btn.click(fn=translate, inputs=[input_text, model_dropdown], outputs=output_text)
 
 
 
 
78
 
79
  gr.Examples(
80
  examples=[
 
84
  inputs=[input_text, model_dropdown]
85
  )
86
 
87
+ gr.Markdown("---")
88
+ with gr.Row():
89
+ with gr.Column():
90
+ gr.Markdown("### 🛠️ Help us improve!")
91
+ gr.Markdown("Share the correct version so we can retrain the model.")
92
+ feedback_btn = gr.Button("Submit Correct Translation", variant="secondary")
93
+ feedback_btn.click(fn=None, _js='() => { window.open("https://forms.gle/FXspX7DxXHh5En6c7", "_blank"); }')
94
+
95
+ with gr.Column():
96
+ gr.Markdown("### ⚡ Support our computation")
97
+ gr.Markdown("Your support helps us keep this service free and cover hosting costs.")
98
+ support_btn = gr.Button("Support Computation Costs ☕", variant="primary")
99
+ # ADD YOUR RAZORPAY/INSTAMOJO/KO-FI LINK HERE:
100
+ # support_btn.click(fn=None, _js='() => { window.open("https://ko-fi.com/YOUR_USERNAME", "_blank"); }')
101
+
102
  demo.launch()