tasal9 commited on
Commit
4e5a960
·
verified ·
1 Parent(s): 66ede10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -1,9 +1,8 @@
1
- import torch
2
  import gradio as gr
3
  from transformers import pipeline, AutoTokenizer
4
  from functools import lru_cache
5
 
6
- MODEL_ID = "tasal9/ZamAI-mT5-Pashto" # Must be a string
7
 
8
  SAMPLE_INSTRUCTIONS = [
9
  "په پښتو کې د خپل نوم او د عمر معلومات ولیکئ.",
@@ -17,53 +16,52 @@ SAMPLE_INSTRUCTIONS = [
17
 
18
  def generate_prompt(instruction, input_text=""):
19
  if input_text:
20
- return f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:"
21
  else:
22
- return f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:"
23
 
24
- # Cache the pipeline for performance
25
  @lru_cache(maxsize=1)
26
  def get_generator():
27
- model_id_str = str(MODEL_ID) # Ensure it's a string
28
- # Optional: verify tokenizer exists
29
- try:
30
- tokenizer = AutoTokenizer.from_pretrained(model_id_str, use_fast=False)
31
- except Exception as e:
32
- raise ValueError(f"Failed to load tokenizer for {model_id_str}: {e}")
33
-
34
- device = 0 if torch.cuda.is_available() else -1
35
  return pipeline(
36
  "text2text-generation",
37
- model=model_id_str,
38
- tokenizer=model_id_str,
39
- device=device,
 
40
  use_fast=False
41
  )
42
 
43
  def predict(instruction, input_text, max_length, num_beams, temperature, top_p):
44
  gen = get_generator()
45
  prompt = generate_prompt(instruction, input_text)
 
46
  outputs = gen(
47
  prompt,
48
  max_length=max_length,
49
  num_beams=num_beams,
50
  temperature=temperature,
51
  top_p=top_p,
 
52
  early_stopping=True
53
  )
54
- # Remove splitting on 'ځواب:' and just return the generated text after the prompt
55
  generated = outputs[0]["generated_text"].strip()
56
- # Remove the prompt from the generated text if present
57
- if generated.startswith(prompt):
58
- generated = generated[len(prompt):].strip()
59
- return generated if generated else "No response generated."
60
 
61
- # Build UI with Blocks
 
 
 
 
 
 
 
62
  with gr.Blocks() as demo:
63
  gr.Markdown(
64
  """
65
  # ZamAI mT5 Pashto Demo
66
- اپلیکیشن **ZamAI-mT5-Pashto** ماډل د پښتو لارښوونو لپاره .
67
  لاندې تنظیمات بدل کړئ او لارښوونه ولیکئ ترڅو ځواب ترلاسه کړئ.
68
  """
69
  )
@@ -72,7 +70,7 @@ with gr.Blocks() as demo:
72
  with gr.Column(scale=2):
73
  instruction_dropdown = gr.Dropdown(
74
  choices=SAMPLE_INSTRUCTIONS,
75
- label="نمونې لارښوونې (Auto-select)",
76
  value=SAMPLE_INSTRUCTIONS[0],
77
  interactive=True
78
  )
@@ -84,14 +82,17 @@ with gr.Blocks() as demo:
84
  input_text = gr.Textbox(lines=2, placeholder="اختیاري متن...", label="متن")
85
  output = gr.Textbox(label="ځواب", interactive=False, lines=5)
86
  generate_btn = gr.Button("جوړول", variant="primary")
 
87
  with gr.Column(scale=1):
88
- gr.Markdown("### د تولید تنظیمات")
89
- max_length = gr.Slider(32, 512, value=256, step=1, label="اعظمي اوږدوالی")
90
- num_beams = gr.Slider(1, 10, value=5, step=1, label="شمیر شعاعونه")
91
- temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="تودوخه")
92
- top_p = gr.Slider(0.1, 1.0, value=1.0, step=0.05, label="Top-p")
93
 
94
- instruction_dropdown.change(lambda x: x, inputs=instruction_dropdown, outputs=instruction_textbox)
 
 
95
 
96
  generate_btn.click(
97
  fn=predict,
@@ -100,4 +101,4 @@ with gr.Blocks() as demo:
100
  )
101
 
102
  if __name__ == "__main__":
103
- demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline, AutoTokenizer
3
  from functools import lru_cache
4
 
5
+ MODEL_ID = "tasal9/ZamAI-mT5-Pashto"
6
 
7
  SAMPLE_INSTRUCTIONS = [
8
  "په پښتو کې د خپل نوم او د عمر معلومات ولیکئ.",
 
16
 
17
  def generate_prompt(instruction, input_text=""):
18
  if input_text:
19
+ return f"### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:"
20
  else:
21
+ return f"### Instruction:\n{instruction}\n\n### Response:"
22
 
 
23
  @lru_cache(maxsize=1)
24
  def get_generator():
25
+ # Always CPU in ZeroGPU
26
+ AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
 
 
 
 
 
 
27
  return pipeline(
28
  "text2text-generation",
29
+ model=MODEL_ID,
30
+ tokenizer=MODEL_ID,
31
+ device=-1, # Force CPU
32
+ return_full_text=False, # Avoids prompt repetition
33
  use_fast=False
34
  )
35
 
36
  def predict(instruction, input_text, max_length, num_beams, temperature, top_p):
37
  gen = get_generator()
38
  prompt = generate_prompt(instruction, input_text)
39
+
40
  outputs = gen(
41
  prompt,
42
  max_length=max_length,
43
  num_beams=num_beams,
44
  temperature=temperature,
45
  top_p=top_p,
46
+ do_sample=True, # Sampling works better on CPU than beams
47
  early_stopping=True
48
  )
49
+
50
  generated = outputs[0]["generated_text"].strip()
 
 
 
 
51
 
52
+ # Clean output
53
+ for cut in [prompt, "### Instruction:", "### Response:", "ځواب:"]:
54
+ if generated.startswith(cut):
55
+ generated = generated[len(cut):].strip()
56
+
57
+ return generated if generated else "⚠️ No response generated."
58
+
59
+ # ---------------- Gradio UI ----------------
60
  with gr.Blocks() as demo:
61
  gr.Markdown(
62
  """
63
  # ZamAI mT5 Pashto Demo
64
+ اپلیکیشن **ZamAI-mT5-Pashto** د پښتو لارښوونو لپاره.
65
  لاندې تنظیمات بدل کړئ او لارښوونه ولیکئ ترڅو ځواب ترلاسه کړئ.
66
  """
67
  )
 
70
  with gr.Column(scale=2):
71
  instruction_dropdown = gr.Dropdown(
72
  choices=SAMPLE_INSTRUCTIONS,
73
+ label="نمونې لارښوونې",
74
  value=SAMPLE_INSTRUCTIONS[0],
75
  interactive=True
76
  )
 
82
  input_text = gr.Textbox(lines=2, placeholder="اختیاري متن...", label="متن")
83
  output = gr.Textbox(label="ځواب", interactive=False, lines=5)
84
  generate_btn = gr.Button("جوړول", variant="primary")
85
+
86
  with gr.Column(scale=1):
87
+ gr.Markdown("### د تولید تنظیمات (ZeroGPU)")
88
+ max_length = gr.Slider(32, 256, value=128, step=1, label="اعظمي اوږدوالی")
89
+ num_beams = gr.Slider(1, 3, value=2, step=1, label="شمیر شعاعونه")
90
+ temperature = gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="تودوخه")
91
+ top_p = gr.Slider(0.7, 1.0, value=0.9, step=0.05, label="Top-p")
92
 
93
+ instruction_dropdown.change(
94
+ lambda x: x, inputs=instruction_dropdown, outputs=instruction_textbox
95
+ )
96
 
97
  generate_btn.click(
98
  fn=predict,
 
101
  )
102
 
103
  if __name__ == "__main__":
104
+ demo.launch()