Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
return "Model merged and saved successfully!"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
output = gr.Textbox(label="Merge Status")
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
demo.launch(share=True)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from peft import PeftModel
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
# Use GPU if available
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 8 |
|
| 9 |
+
# Base model and adapter paths
|
| 10 |
+
base_model_name = "microsoft/phi-2" # Pull from HF Hub directly
|
| 11 |
+
adapter_path = "Shriti09/Microsoft-Phi-QLora" # Update with your Hugging Face repo path
|
|
|
|
| 12 |
|
| 13 |
+
print("🔧 Loading base model...")
|
| 14 |
+
# Load the base model
|
| 15 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
base_model_name,
|
| 17 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 18 |
+
)
|
| 19 |
|
| 20 |
+
print("🔧 Loading LoRA adapter...")
|
| 21 |
+
# Load the LoRA adapter
|
| 22 |
+
adapter_model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 23 |
+
|
| 24 |
+
print("🔗 Merging adapter into base model...")
|
| 25 |
+
# Merge adapter into the base model
|
| 26 |
+
merged_model = adapter_model.merge_and_unload()
|
| 27 |
+
merged_model.eval()
|
| 28 |
+
|
| 29 |
+
# Load tokenizer
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 31 |
+
print("✅ Model ready for inference!")
|
| 32 |
+
|
| 33 |
+
# Text generation function
|
| 34 |
+
def generate_text(prompt):
|
| 35 |
+
# Tokenize the input
|
| 36 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 37 |
+
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
outputs = merged_model.generate(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_new_tokens=150,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
temperature=0.7,
|
| 44 |
+
top_p=0.9,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Decode and return the generated response
|
| 49 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
+
return response
|
| 51 |
+
|
| 52 |
+
# Gradio UI
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
+
gr.Markdown("<h1>🧠 Phi-2 QLoRA Text Generator</h1>")
|
| 55 |
+
|
| 56 |
+
# Textbox for user input
|
| 57 |
+
prompt = gr.Textbox(label="Enter your prompt:", lines=2)
|
| 58 |
+
|
| 59 |
+
# Output textbox for generated text
|
| 60 |
+
output = gr.Textbox(label="Generated text:", lines=5)
|
| 61 |
+
|
| 62 |
+
# Button to trigger text generation
|
| 63 |
+
generate_button = gr.Button("Generate Text")
|
| 64 |
+
|
| 65 |
+
# Set the button action to generate text
|
| 66 |
+
generate_button.click(generate_text, inputs=prompt, outputs=output)
|
| 67 |
+
|
| 68 |
+
# Launch the app
|
| 69 |
demo.launch(share=True)
|