code5ecure commited on
Commit
ffa11fa
·
verified ·
1 Parent(s): a6f3ff8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  from opacus import PrivacyEngine
4
  import gradio as gr
5
 
@@ -8,9 +8,13 @@ import gradio as gr
8
  # ===========================
9
  model_name = "HooshvareLab/bert-base-parsbert-uncased"
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
- tokenizer.pad_token = tokenizer.eos_token # حل ارور padding
12
 
13
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
 
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
  model.to(device)
16
 
@@ -50,16 +54,20 @@ def train_step():
50
  return loss.item()
51
 
52
  # ===========================
53
- # تابع تولید پاسخ
54
  # ===========================
55
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
56
-
57
- def chat(prompt):
58
- # آموزش incremental روی داده داخلی قبل از تولید پاسخ
59
  loss = train_step()
60
- # تولید پاسخ مدل
61
- result = generator(prompt, max_length=128, do_sample=True, temperature=0.8)
62
- return f"{result[0]['generated_text']}\n\n(مدل به صورت incremental روی داده‌های داخلی آموزش داده شد. DP فعال است)"
 
 
 
 
63
 
64
  # ===========================
65
  # رابط کاربری Gradio (RTL)
@@ -69,6 +77,6 @@ with gr.Blocks() as demo:
69
  prompt_input = gr.Textbox(label="متن خود را وارد کنید:", placeholder="اینجا بنویسید...", lines=3)
70
  output = gr.Textbox(label="پاسخ مدل:", lines=8)
71
  btn = gr.Button("ارسال")
72
- btn.click(chat, inputs=prompt_input, outputs=output)
73
 
74
  demo.launch()
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline
3
  from opacus import PrivacyEngine
4
  import gradio as gr
5
 
 
8
  # ===========================
9
  model_name = "HooshvareLab/bert-base-parsbert-uncased"
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
11
 
12
+ # حل ارور padding
13
+ if tokenizer.pad_token is None:
14
+ tokenizer.add_special_tokens({'pad_token': '[PAD]'})
15
+ model = AutoModelForMaskedLM.from_pretrained(model_name)
16
+ model.resize_token_embeddings(len(tokenizer))
17
+
18
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
  model.to(device)
20
 
 
54
  return loss.item()
55
 
56
  # ===========================
57
+ # تابع تولید پاسخ (با masked LM)
58
  # ===========================
59
+ def generate_text(prompt, max_length=50):
60
+ # توکنایز prompt
61
+ input_ids_prompt = tokenizer.encode(prompt, return_tensors="pt").to(device)
62
+ # آموزش incremental روی داده داخلی
63
  loss = train_step()
64
+ # تولید متن ساده با روش greedy
65
+ model.eval()
66
+ with torch.no_grad():
67
+ outputs = model(input_ids_prompt)
68
+ predictions = torch.argmax(outputs.logits, dim=-1)
69
+ generated_text = tokenizer.decode(predictions[0], skip_special_tokens=True)
70
+ return f"{generated_text}\n\n(مدل به صورت incremental روی داده‌های داخلی آموزش داده شد. DP فعال است)"
71
 
72
  # ===========================
73
  # رابط کاربری Gradio (RTL)
 
77
  prompt_input = gr.Textbox(label="متن خود را وارد کنید:", placeholder="اینجا بنویسید...", lines=3)
78
  output = gr.Textbox(label="پاسخ مدل:", lines=8)
79
  btn = gr.Button("ارسال")
80
+ btn.click(generate_text, inputs=prompt_input, outputs=output)
81
 
82
  demo.launch()