Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
# --- Configuration ---
|
| 7 |
+
# 1. Base Model ID: Llama-2-7b-chat-hf is typically used as the base
|
| 8 |
+
base_model_id = "meta-llama/Llama-2-7b-chat-hf"
|
| 9 |
+
|
| 10 |
+
# 2. LoRA Path: IMPORTANT! Replace this with the path to your fine-tuned model
|
| 11 |
+
# This should be the Hugging Face repo ID (e.g., "your-username/llama2-dockerfile-lora")
|
| 12 |
+
# or a local directory path where the adapter weights are stored.
|
| 13 |
+
lora_path = "Arsh014/lora-llama2-finetuned"
|
| 14 |
+
|
| 15 |
+
# Check for CUDA availability
|
| 16 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 17 |
+
|
| 18 |
+
print(f"Loading tokenizer from: {base_model_id}")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
| 20 |
+
|
| 21 |
+
# 3. Load the base model with 8-bit quantization for efficiency
|
| 22 |
+
print(f"Loading base model (8-bit) from: {base_model_id}")
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
base_model_id,
|
| 25 |
+
load_in_8bit=True,
|
| 26 |
+
torch_dtype=torch.float16,
|
| 27 |
+
device_map="auto"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# 4. Apply the PEFT (LoRA) adapters to the base model
|
| 31 |
+
print(f"Applying LoRA adapter from: {lora_path}")
|
| 32 |
+
try:
|
| 33 |
+
model = PeftModel.from_pretrained(model, lora_path)
|
| 34 |
+
model.eval() # Set model to evaluation mode
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error loading LoRA adapter from {lora_path}. Ensure it exists and is correct.")
|
| 37 |
+
print(f"Error: {e}")
|
| 38 |
+
# The app will likely fail if the LoRA path is incorrect.
|
| 39 |
+
# We proceed with the base model, but generation quality will be poor for the task.
|
| 40 |
+
|
| 41 |
+
# 5. Create a text-generation pipeline
|
| 42 |
+
print("Creating text-generation pipeline.")
|
| 43 |
+
pipe = pipeline(
|
| 44 |
+
"text-generation",
|
| 45 |
+
model=model,
|
| 46 |
+
tokenizer=tokenizer,
|
| 47 |
+
device=device
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
def format_prompt(instruction, code):
|
| 51 |
+
"""Formats the instruction and input code into the required prompt template."""
|
| 52 |
+
return f"""### Instruction:
|
| 53 |
+
{instruction}
|
| 54 |
+
|
| 55 |
+
### Input:
|
| 56 |
+
{code}
|
| 57 |
+
|
| 58 |
+
### Response:"""
|
| 59 |
+
|
| 60 |
+
def explain_dockerfile(instruction, code):
|
| 61 |
+
"""Generates the explanation using the text-generation pipeline."""
|
| 62 |
+
if not instruction or not code:
|
| 63 |
+
return "Please provide both an instruction and the Dockerfile code."
|
| 64 |
+
|
| 65 |
+
prompt = format_prompt(instruction, code)
|
| 66 |
+
|
| 67 |
+
# Generate response
|
| 68 |
+
response = pipe(
|
| 69 |
+
prompt,
|
| 70 |
+
max_new_tokens=256,
|
| 71 |
+
do_sample=True,
|
| 72 |
+
temperature=0.7,
|
| 73 |
+
return_full_text=False # We want only the new tokens generated after the prompt
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# The pipeline's output can be complex, extract the text and clean up
|
| 77 |
+
generated_text = response[0]["generated_text"].strip()
|
| 78 |
+
|
| 79 |
+
# Clean up the output to remove the initial prompt if return_full_text=False
|
| 80 |
+
# didn't perfectly handle it (it's good practice to split/strip again)
|
| 81 |
+
if "### Response:" in generated_text:
|
| 82 |
+
return generated_text.split("### Response:")[-1].strip()
|
| 83 |
+
|
| 84 |
+
return generated_text
|
| 85 |
+
|
| 86 |
+
# 6. Gradio Interface
|
| 87 |
+
print("Launching Gradio Interface...")
|
| 88 |
+
iface = gr.Interface(
|
| 89 |
+
fn=explain_dockerfile,
|
| 90 |
+
inputs=[
|
| 91 |
+
gr.Textbox(
|
| 92 |
+
label="Instruction",
|
| 93 |
+
placeholder="e.g., Explain the function of each line and the overall goal of this Dockerfile.",
|
| 94 |
+
value="Explain this Dockerfile in detail and suggest one security improvement.",
|
| 95 |
+
lines=2
|
| 96 |
+
),
|
| 97 |
+
gr.Textbox(
|
| 98 |
+
label="Dockerfile Code",
|
| 99 |
+
lines=10,
|
| 100 |
+
placeholder="Paste your Dockerfile here, e.g., \nFROM python:3.9-slim\nWORKDIR /app\nCOPY requirements.txt .\nRUN pip install -r requirements.txt\nCOPY . .\nCMD [\"python\", \"app.py\"]",
|
| 101 |
+
value="FROM node:18-alpine\nWORKDIR /usr/src/app\nCOPY package*.json ./ \nRUN npm install\nCOPY . .\nEXPOSE 3000\nCMD [ \"npm\", \"start\" ]"
|
| 102 |
+
)
|
| 103 |
+
],
|
| 104 |
+
outputs=gr.Textbox(
|
| 105 |
+
label="Explanation (Generated by LoRA Model)",
|
| 106 |
+
lines=15
|
| 107 |
+
),
|
| 108 |
+
title="LoRA-Tuned Llama-2 Dockerfile Explainer",
|
| 109 |
+
description="A simple application to explain complex Dockerfiles using a fine-tuned Llama-2 model (via LoRA).",
|
| 110 |
+
live=False
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
iface.launch()
|