codinglabsong commited on
Commit
cdb4f63
·
verified ·
1 Parent(s): 06d9177

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  import torch
2
  import gradio as gr
3
  from transformers import AutoTokenizer
@@ -5,9 +10,14 @@ from transformers import BartForConditionalGeneration
5
  from peft import PeftModel
6
 
7
 
8
- def load_model():
9
  """
10
- Load tokenizer, and the fine-tuned LoRA model.
 
 
 
 
 
11
  """
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
 
@@ -18,7 +28,9 @@ def load_model():
18
  base_model.config.attn_implementation = "sdpa"
19
 
20
  # Load PEFT (LoRA) model for inference
21
- model = PeftModel.from_pretrained(base_model, "outputs/bart-base-reddit-lora").eval()
 
 
22
  model.to(device)
23
  model.eval()
24
 
@@ -31,7 +43,13 @@ tokenizer, model, device = load_model()
31
 
32
  def predict(text: str) -> str:
33
  """
34
- Generate a response for a single input text.
 
 
 
 
 
 
35
  """
36
  # Tokenize and move inputs to device
37
  inputs = tokenizer(
@@ -44,8 +62,8 @@ def predict(text: str) -> str:
44
  # Generate with both beam search and sampling for diversity
45
  outputs = model.generate(
46
  **inputs,
47
- max_length=500,
48
- num_beams=5,
49
  do_sample=True,
50
  length_penalty=1.2,
51
  repetition_penalty=1.3,
@@ -60,13 +78,16 @@ def predict(text: str) -> str:
60
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
61
 
62
 
63
- def main():
 
 
 
64
  interface = gr.Interface(
65
  fn=predict,
66
- inputs=gr.Textbox(lines=5, placeholder="Ask a Question", label="Your Question"),
67
- outputs=gr.Textbox(label="Model Output"),
68
- title="Bart-Reddit-LoRA Inference",
69
- description="Enter your prompt and click Submit to get the model's response.",
70
  allow_flagging="never",
71
  )
72
  interface.launch()
 
1
+ """
2
+ Module for loading a LoRA fine-tuned BART model and serving
3
+ an interactive Gradio interface for text generation.
4
+ """
5
+
6
  import torch
7
  import gradio as gr
8
  from transformers import AutoTokenizer
 
10
  from peft import PeftModel
11
 
12
 
13
+ def load_model() -> tuple[AutoTokenizer, PeftModel, torch.device]:
14
  """
15
+ Load tokenizer and LoRA-enhanced model onto available device.
16
+
17
+ Returns:
18
+ tokenizer (AutoTokenizer): Tokenizer for text processing.
19
+ model (PeftModel): Fine-tuned LoRA BART model in eval mode.
20
+ device (torch.device): Computation device (GPU if available, else CPU).
21
  """
22
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
 
 
28
  base_model.config.attn_implementation = "sdpa"
29
 
30
  # Load PEFT (LoRA) model for inference
31
+ model = PeftModel.from_pretrained(
32
+ base_model, "outputs/bart-base-reddit-lora"
33
+ ).eval()
34
  model.to(device)
35
  model.eval()
36
 
 
43
 
44
  def predict(text: str) -> str:
45
  """
46
+ Generate a text response given an input prompt.
47
+
48
+ Args:
49
+ text (str): The input prompt string.
50
+
51
+ Returns:
52
+ str: The decoded model output.
53
  """
54
  # Tokenize and move inputs to device
55
  inputs = tokenizer(
 
62
  # Generate with both beam search and sampling for diversity
63
  outputs = model.generate(
64
  **inputs,
65
+ max_length=128,
66
+ num_beams=10,
67
  do_sample=True,
68
  length_penalty=1.2,
69
  repetition_penalty=1.3,
 
78
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
79
 
80
 
81
+ def main() -> None:
82
+ """
83
+ Launch Gradio web interface for interactive model inference.
84
+ """
85
  interface = gr.Interface(
86
  fn=predict,
87
+ inputs=gr.Textbox(lines=5, placeholder="Broad questions often have better results.", label="Your Question"),
88
+ outputs=gr.Textbox(label="Mimic Bot's Comment"),
89
+ title="Reddit-User-Mimic-Bot Inference (Bart-LoRA)",
90
+ description="Enter a question you would ask on reddit, and our Mimic Bot would comment back! Have fun.",
91
  allow_flagging="never",
92
  )
93
  interface.launch()