SameerJugno commited on
Commit
43b2eda
·
verified ·
1 Parent(s): aa1686b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
+ # from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
+ # from peft import PeftModel, PeftConfig
4
+
5
+ # # Load tokenizer
6
+ # tokenizer = AutoTokenizer.from_pretrained(".")
7
+
8
+ # # Load base model with quantization
9
+ # bnb_config = BitsAndBytesConfig(load_in_4bit=True)
10
+ # base_model = AutoModelForCausalLM.from_pretrained(
11
+ # "unsloth/Meta-Llama-3.1-8B-bnb-4bit", # same base you fine-tuned
12
+ # quantization_config=bnb_config,
13
+ # device_map="auto"
14
+ # )
15
+
16
+ # # Load LoRA adapters
17
+ # model = PeftModel.from_pretrained(base_model, ".")
18
+
19
+ # # Create Gradio Interface
20
+ # def generate_response(prompt):
21
+ # inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
22
+ # outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7)
23
+ # return tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+
25
+ # gr.Interface(
26
+ # fn=generate_response,
27
+ # inputs=gr.Textbox(label="Enter your instruction"),
28
+ # outputs=gr.Textbox(label="Model response"),
29
+ # title="LLaMA 3 - Fine-tuned Model"
30
+ # ).launch()
31
+
32
+ from transformers import AutoTokenizer, AutoModelForCausalLM
33
+ from peft import PeftModel
34
+ import torch
35
+ import gradio as gr
36
+
37
+ # Load base model from HF Hub
38
+ base_model_name = "unsloth/Llama-3.2-1B"
39
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
40
+
41
+ # Load base model (set torch_dtype if needed)
42
+ model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.float16)
43
+
44
+ # Load LoRA adapters from local files in Space
45
+ adapter_path = "./" # If adapter files are in root or specify folder name
46
+ model = PeftModel.from_pretrained(model, adapter_path)
47
+
48
+ model.eval()
49
+
50
+ def predict(text):
51
+ inputs = tokenizer(text, return_tensors="pt").to("cpu") # Use "cuda" if GPU available
52
+ outputs = model.generate(**inputs, max_new_tokens=100)
53
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
54
+
55
+ iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="LoRA Model Demo")
56
+ iface.launch()