Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,67 @@
|
|
| 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
|
| 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 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
|
|
|
| 15 |
|
| 16 |
-
# 2οΈβ£
|
|
|
|
| 17 |
llm = Llama(
|
| 18 |
model_path=model_path,
|
| 19 |
-
n_ctx=4096,
|
| 20 |
-
n_threads=2,
|
| 21 |
n_batch=512,
|
| 22 |
verbose=False,
|
| 23 |
-
use_mlock=True
|
| 24 |
)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# 3οΈβ£ Generation function
|
| 27 |
def generate_python_code(user_prompt):
|
| 28 |
-
|
| 29 |
-
|
| 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":
|
| 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,
|
| 43 |
top_p=0.9,
|
| 44 |
repeat_penalty=1.1,
|
| 45 |
-
stop=["</s>", "```"]
|
| 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 |
-
|
| 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"
|
| 61 |
examples=[
|
| 62 |
-
["Write a
|
| 63 |
-
["
|
| 64 |
-
["Create a Pydantic model for a user profile with email validation and a custom validator for age > 18"]
|
| 65 |
]
|
| 66 |
)
|
| 67 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
| 6 |
|
| 7 |
+
# π§ CONFIGURATION
|
| 8 |
MODEL_REPO = "bartowski/Qwen2.5-Coder-1.5B-Instruct-GGUF"
|
| 9 |
MODEL_FILE = "Qwen2.5-Coder-1.5B-Instruct-Q4_K_M.gguf"
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
print("β³ Starting Python Dev Assistant Space...")
|
| 12 |
+
START_TIME = time.time()
|
| 13 |
+
|
| 14 |
+
# 1οΈβ£ Download (only happens on first boot or cache miss)
|
| 15 |
+
print(f"π¦ Checking cache for {MODEL_FILE}...")
|
| 16 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 17 |
+
print(f"β
Model cached at: {model_path}")
|
| 18 |
|
| 19 |
+
# 2οΈβ£ Load into RAM (runs ONCE per Space startup)
|
| 20 |
+
print("π§ Loading model into memory...")
|
| 21 |
llm = Llama(
|
| 22 |
model_path=model_path,
|
| 23 |
+
n_ctx=4096,
|
| 24 |
+
n_threads=2,
|
| 25 |
n_batch=512,
|
| 26 |
verbose=False,
|
| 27 |
+
use_mlock=True
|
| 28 |
)
|
| 29 |
+
LOAD_TIME = round(time.time() - START_TIME, 1)
|
| 30 |
+
print(f"π Model loaded in {LOAD_TIME}s. Ready for prompts!")
|
| 31 |
|
| 32 |
+
# 3οΈβ£ Generation function (reuses `llm` every time)
|
| 33 |
def generate_python_code(user_prompt):
|
| 34 |
+
inference_start = time.time()
|
| 35 |
+
print(f"πΉ Processing prompt at {time.strftime('%H:%M:%S')}")
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
messages = [
|
| 38 |
+
{"role": "system", "content": "You are an expert Python developer. Write clean, PEP-8 compliant code with type hints. Output only code unless asked otherwise."},
|
| 39 |
{"role": "user", "content": user_prompt}
|
| 40 |
]
|
| 41 |
|
| 42 |
output = llm.create_chat_completion(
|
| 43 |
messages=messages,
|
| 44 |
max_tokens=1024,
|
| 45 |
+
temperature=0.2,
|
| 46 |
top_p=0.9,
|
| 47 |
repeat_penalty=1.1,
|
| 48 |
+
stop=["</s>", "```"]
|
| 49 |
)
|
| 50 |
|
| 51 |
+
inference_time = round(time.time() - inference_start, 2)
|
| 52 |
+
print(f"β
Done in {inference_time}s")
|
| 53 |
return output["choices"][0]["message"]["content"]
|
| 54 |
|
| 55 |
# 4οΈβ£ Gradio UI
|
| 56 |
demo = gr.Interface(
|
| 57 |
fn=generate_python_code,
|
| 58 |
+
inputs=gr.Textbox(lines=4, placeholder="Describe your Python task..."),
|
| 59 |
+
outputs=gr.Code(language="python"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
title="π Python Dev Assistant",
|
| 61 |
+
description=f"Loaded `{MODEL_FILE}` in {LOAD_TIME}s. Model stays in RAM between prompts.",
|
| 62 |
examples=[
|
| 63 |
+
["Write a Pydantic v2 model for a User with email validation"],
|
| 64 |
+
["Create an async retry wrapper for HTTP requests using aiohttp"]
|
|
|
|
| 65 |
]
|
| 66 |
)
|
| 67 |
|