mickey61305 commited on
Commit
8178916
·
verified ·
1 Parent(s): 5524ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -1,51 +1,46 @@
1
  import gradio as gr
2
- from unsloth import FastLanguageModel
3
  import torch
 
 
4
 
5
- # 1. Load the model and adapters from the current folder
6
- # We use device_map="cpu" because the free Hugging Face tier doesn't have a GPU
7
- model, tokenizer = FastLanguageModel.from_pretrained(
8
- model_name = ".",
9
- load_in_4bit = True,
10
- device_map = "cpu"
 
 
 
 
 
 
 
11
  )
12
 
13
- # Enable fast inference mode
14
- FastLanguageModel.for_inference(model)
15
 
16
- # 2. Define the reasoning logic
17
  def legal_summarizer(legal_text):
18
- # This template must match what you used during training
19
  prompt = f"Analyze the following legal text and provide a grounded summary.\n\nInput:\n{legal_text}\n\nResponse:\n"
 
20
 
21
- # Tokenize and move to CPU
22
- inputs = tokenizer([prompt], return_tensors = "pt").to("cpu")
23
-
24
- # Generate the response
25
- outputs = model.generate(
26
- **inputs,
27
- max_new_tokens = 256,
28
- use_cache = True
29
- )
30
-
31
- # Decode and clean the output to show ONLY the response
32
- decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
33
 
34
- # Split by the 'Response:' tag to isolate the AI's answer
35
  if "Response:" in decoded:
36
  return decoded.split("Response:")[-1].strip()
37
  return decoded.strip()
38
 
39
- # 3. Setup the Gradio UI
40
  demo = gr.Interface(
41
  fn=legal_summarizer,
42
- inputs=gr.Textbox(lines=10, label="Paste Legal Text/Contract", placeholder="Enter legal jargon here..."),
43
  outputs=gr.Textbox(label="LexGuard AI Summary"),
44
- title="⚖️ LexGuard AI: Legal Reasoning Tool",
45
- description="A fine-tuned Llama-3 model optimized for clarifying complex legal documents.",
46
- theme="soft"
47
  )
48
 
49
- # Launch the app
50
  if __name__ == "__main__":
51
  demo.launch()
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from peft import PeftModel
5
 
6
+ # 1. Load the Base Model and your Adapters using standard Transformers
7
+ base_model_name = "unsloth/Llama-3.2-3B-bnb-4bit" # The base model
8
+ adapter_path = "." # Your uploaded adapter files
9
+
10
+ # Load tokenizer
11
+ tokenizer = AutoTokenizer.from_pretrained(adapter_path)
12
+
13
+ # Load the base model in 4-bit (CPU compatible)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ base_model_name,
16
+ torch_dtype=torch.float32,
17
+ device_map="cpu",
18
+ low_cpu_mem_usage=True
19
  )
20
 
21
+ # Merge your adapters onto the base model
22
+ model = PeftModel.from_pretrained(model, adapter_path)
23
 
24
+ # 2. Reasoning Logic
25
  def legal_summarizer(legal_text):
 
26
  prompt = f"Analyze the following legal text and provide a grounded summary.\n\nInput:\n{legal_text}\n\nResponse:\n"
27
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
28
 
29
+ with torch.no_grad():
30
+ outputs = model.generate(**inputs, max_new_tokens=256)
 
 
 
 
 
 
 
 
 
 
31
 
32
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
  if "Response:" in decoded:
34
  return decoded.split("Response:")[-1].strip()
35
  return decoded.strip()
36
 
37
+ # 3. UI Setup
38
  demo = gr.Interface(
39
  fn=legal_summarizer,
40
+ inputs=gr.Textbox(lines=10, label="Paste Legal Text"),
41
  outputs=gr.Textbox(label="LexGuard AI Summary"),
42
+ title="⚖️ LexGuard AI (CPU Edition)"
 
 
43
  )
44
 
 
45
  if __name__ == "__main__":
46
  demo.launch()