nihardon commited on
Commit
fb8c0a1
·
verified ·
1 Parent(s): 497b284

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from peft import PeftModel
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+
6
+ # --- CONFIGURATION ---
7
+ # The base model
8
+ BASE_MODEL = "unsloth/llama-3-8b"
9
+ # Fine-tuned adapter
10
+ ADAPTER_MODEL = "nihardon/fine-tuned-unit-test-generator"
11
+
12
+ print(f"Loading {ADAPTER_MODEL} on CPU... (This might take a minute)")
13
+
14
+ # Load tokenizer
15
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
16
+
17
+ # Load model (CPU optimized loading)
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ BASE_MODEL,
20
+ device_map="cpu",
21
+ torch_dtype=torch.float32,
22
+ low_cpu_mem_usage=True
23
+ )
24
+
25
+ # Apply fine-tuned adapters
26
+ model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
27
+
28
+ def generate_test(user_code):
29
+ alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
30
+
31
+ ### Instruction:
32
+ You are an expert Python QA engineer. Write a pytest unit test for the following function.
33
+
34
+ ### Input:
35
+ {}
36
+
37
+ ### Response:
38
+ """
39
+ # Format and tokenize
40
+ prompt = alpaca_prompt.format(user_code)
41
+ inputs = tokenizer(prompt, return_tensors="pt")
42
+
43
+ # Generate
44
+ with torch.no_grad():
45
+ outputs = model.generate(
46
+ **inputs,
47
+ max_new_tokens=256,
48
+ use_cache=True,
49
+ temperature=0.1
50
+ )
51
+
52
+ # Decode
53
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
54
+ return response.split("### Response:")[-1].strip()
55
+
56
+ # UI
57
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
+ gr.Markdown("# 🧪 AI Unit Test Generator")
59
+ gr.Markdown(f"**Model:** {ADAPTER_MODEL} (Llama-3 Fine-Tune) | **Status:** Running on CPU")
60
+ gr.Markdown("Paste your Python function below, and the AI will write a Pytest case for it.")
61
+
62
+ with gr.Row():
63
+ with gr.Column():
64
+ input_box = gr.Code(language="python", label="Paste Python Function Here", value="def add(a, b):\n return a + b")
65
+ btn = gr.Button("Generate Pytest", variant="primary")
66
+ with gr.Column():
67
+ output_box = gr.Code(language="python", label="Generated Test Case")
68
+
69
+ btn.click(generate_test, inputs=input_box, outputs=output_box)
70
+
71
+ demo.launch()