Spaces:
Paused
Paused
Commit ·
9613ebd
1
Parent(s): 18df803
Add space
Browse files- app.py +39 -4
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
demo.launch()
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
|
| 3 |
+
# def greet(name):
|
| 4 |
+
# return "Hello " + name + "!!"
|
| 5 |
+
|
| 6 |
+
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
+
# demo.launch()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 12 |
+
from peft import PeftModel
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
+
# Load base model
|
| 16 |
+
base_model = "microsoft/Phi-4-mini-instruct"
|
| 17 |
+
lora_path = "./phi4-lora-finetuned-10k" # or use HF repo name if pushed
|
| 18 |
+
|
| 19 |
+
bnb_config = BitsAndBytesConfig(
|
| 20 |
+
load_in_4bit=True,
|
| 21 |
+
bnb_4bit_quant_type="nf4",
|
| 22 |
+
bnb_4bit_use_double_quant=True,
|
| 23 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, use_fast=True)
|
| 27 |
+
|
| 28 |
+
base = AutoModelForCausalLM.from_pretrained(
|
| 29 |
+
base_model,
|
| 30 |
+
quantization_config=bnb_config,
|
| 31 |
+
device_map="auto",
|
| 32 |
+
trust_remote_code=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
model = PeftModel.from_pretrained(base, lora_path)
|
| 36 |
+
|
| 37 |
+
def generate(prompt):
|
| 38 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 39 |
+
output = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 40 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 41 |
|
| 42 |
+
gr.Interface(fn=generate, inputs="text", outputs="text").launch()
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.41.0
|
| 2 |
+
peft>=0.10.0
|
| 3 |
+
bitsandbytes
|
| 4 |
+
accelerate
|
| 5 |
+
gradio
|
| 6 |
+
torch>=2.1
|