Paul1966-2 commited on
Commit
9f9e362
Β·
verified Β·
1 Parent(s): 3114a71

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from llama_cpp import Llama
5
+
6
+ # πŸ”§ CONFIGURATION: Change these to swap models
7
+ MODEL_REPO = "bartowski/Qwen2.5-Coder-1.5B-Instruct-GGUF"
8
+ MODEL_FILE = "Qwen2.5-Coder-1.5B-Instruct-Q4_K_M.gguf"
9
+ # For 7B: MODEL_REPO = "bartowski/Qwen2.5-Coder-7B-Instruct-GGUF"
10
+ # MODEL_FILE = "Qwen2.5-Coder-7B-Instruct-Q4_K_M.gguf"
11
+
12
+ # 1️⃣ Download model on first boot (cached automatically)
13
+ print(f"⬇️ Downloading {MODEL_FILE} from {MODEL_REPO}...")
14
+ model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
15
+
16
+ # 2️⃣ Initialize CPU-optimized LLM
17
+ llm = Llama(
18
+ model_path=model_path,
19
+ n_ctx=4096, # Max context window
20
+ n_threads=2, # Matches HF free tier vCPU count
21
+ n_batch=512,
22
+ verbose=False,
23
+ use_mlock=True # Keep model in RAM (prevents swapping)
24
+ )
25
+
26
+ # 3️⃣ Generation function
27
+ def generate_python_code(user_prompt):
28
+ system_prompt = (
29
+ "You are an expert Python developer. Write clean, efficient, PEP-8 compliant code. "
30
+ "Include type hints, docstrings, and error handling where appropriate. "
31
+ "Output only the code block unless explicitly asked for explanations."
32
+ )
33
+
34
+ messages = [
35
+ {"role": "system", "content": system_prompt},
36
+ {"role": "user", "content": user_prompt}
37
+ ]
38
+
39
+ output = llm.create_chat_completion(
40
+ messages=messages,
41
+ max_tokens=1024,
42
+ temperature=0.2, # Low for deterministic code
43
+ top_p=0.9,
44
+ repeat_penalty=1.1,
45
+ stop=["</s>", "```"] # Prevent runaway generation
46
+ )
47
+
48
+ return output["choices"][0]["message"]["content"]
49
+
50
+ # 4️⃣ Gradio UI
51
+ demo = gr.Interface(
52
+ fn=generate_python_code,
53
+ inputs=gr.Textbox(
54
+ lines=4,
55
+ placeholder="e.g., Write an async function to fetch JSON from a URL, retry 3 times on failure, and parse specific fields...",
56
+ label="Python Task"
57
+ ),
58
+ outputs=gr.Code(language="python", label="Generated Code"),
59
+ title="🐍 Python Dev Assistant",
60
+ description=f"Running `{MODEL_FILE}` on HF Free CPU Tier. First load takes ~60s.",
61
+ examples=[
62
+ ["Write a FastAPI route that accepts a CSV file and returns summary statistics"],
63
+ ["Refactor this list comprehension into a more readable loop with logging: `results = [x**2 for x in data if x > 0]`"],
64
+ ["Create a Pydantic model for a user profile with email validation and a custom validator for age > 18"]
65
+ ]
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch()