Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,41 @@
|
|
| 1 |
-
print("starting")
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def generate_test(user_code):
|
|
|
|
|
|
|
|
|
|
| 22 |
prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 23 |
|
| 24 |
### Instruction:
|
|
@@ -29,21 +46,33 @@ You are an expert Python QA engineer. Write a pytest unit test for the following
|
|
| 29 |
|
| 30 |
### Response:
|
| 31 |
"""
|
| 32 |
-
output = llm(
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# UI
|
|
|
|
| 36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown("# 🧪 AI Unit Test Generator")
|
| 38 |
-
gr.Markdown("
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
with gr.Column():
|
| 42 |
-
input_box = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
btn = gr.Button("Generate Pytest", variant="primary")
|
| 44 |
with gr.Column():
|
| 45 |
output_box = gr.Code(language="python", label="Generated Test Case")
|
| 46 |
|
| 47 |
btn.click(generate_test, inputs=input_box, outputs=output_box)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# Global variable to hold the model (starts empty)
|
| 6 |
+
llm_model = None
|
| 7 |
+
|
| 8 |
+
def load_model():
|
| 9 |
+
"""Loads the model only when needed."""
|
| 10 |
+
global llm_model
|
| 11 |
+
|
| 12 |
+
# If already loaded, just return it
|
| 13 |
+
if llm_model is not None:
|
| 14 |
+
return llm_model
|
| 15 |
+
|
| 16 |
+
print("⏳ First-time load: Downloading/Loading model...")
|
| 17 |
+
|
| 18 |
+
# 1. Download (Cached)
|
| 19 |
+
model_path = hf_hub_download(
|
| 20 |
+
repo_id="nihardon/fine-tuned-unit-test-generator",
|
| 21 |
+
filename="llama-3-8b.Q4_K_M.gguf",
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# 2. Load into RAM
|
| 25 |
+
llm_model = Llama(
|
| 26 |
+
model_path=model_path,
|
| 27 |
+
n_ctx=1024, # Context window
|
| 28 |
+
n_threads=2, # Use 2 threads for better speed
|
| 29 |
+
verbose=False, # Reduce logs to prevent buffer lag
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
print("✅ Model loaded!")
|
| 33 |
+
return llm_model
|
| 34 |
+
|
| 35 |
def generate_test(user_code):
|
| 36 |
+
# Load model (only happens once)
|
| 37 |
+
llm = load_model()
|
| 38 |
+
|
| 39 |
prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 40 |
|
| 41 |
### Instruction:
|
|
|
|
| 46 |
|
| 47 |
### Response:
|
| 48 |
"""
|
| 49 |
+
output = llm(
|
| 50 |
+
prompt,
|
| 51 |
+
max_tokens=512,
|
| 52 |
+
stop=["### Instruction:", "### Input:"],
|
| 53 |
+
echo=False
|
| 54 |
+
)
|
| 55 |
+
return output["choices"][0]["text"].strip()
|
| 56 |
|
| 57 |
+
# --- The UI ---
|
| 58 |
+
# This part runs instantly, so the Health Check passes immediately!
|
| 59 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 60 |
gr.Markdown("# 🧪 AI Unit Test Generator")
|
| 61 |
+
gr.Markdown("*Note: The first request will take ~1 minute to initialize the model.*")
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
with gr.Column():
|
| 65 |
+
input_box = gr.Code(
|
| 66 |
+
language="python",
|
| 67 |
+
value="def add(a, b):\n return a + b",
|
| 68 |
+
label="Function"
|
| 69 |
+
)
|
| 70 |
btn = gr.Button("Generate Pytest", variant="primary")
|
| 71 |
with gr.Column():
|
| 72 |
output_box = gr.Code(language="python", label="Generated Test Case")
|
| 73 |
|
| 74 |
btn.click(generate_test, inputs=input_box, outputs=output_box)
|
| 75 |
|
| 76 |
+
# Launch
|
| 77 |
+
print("🚀 Server starting...")
|
| 78 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|