Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
from threading import Thread
|
| 6 |
+
|
| 7 |
+
BASE_MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 8 |
+
LORA_REPO = "alxstuff/Lumen-7b"
|
| 9 |
+
|
| 10 |
+
print("Loading tokenizer...")
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 12 |
+
|
| 13 |
+
print("Loading base model...")
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
BASE_MODEL,
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
trust_remote_code=True,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
print("Loading LoRA adapter...")
|
| 22 |
+
model = PeftModel.from_pretrained(model, LORA_REPO)
|
| 23 |
+
model.eval()
|
| 24 |
+
print("✅ Lumen ready!")
|
| 25 |
+
|
| 26 |
+
def chat(message, history):
|
| 27 |
+
prompt = "<|im_start|>system\nYou are Lumen, an expert AI coding assistant built by TheAlxLabs. You write clean, efficient code and explain it clearly.<|im_end|>\n"
|
| 28 |
+
for user, assistant in history:
|
| 29 |
+
prompt += f"<|im_start|>user\n{user}<|im_end|>\n<|im_start|>assistant\n{assistant}<|im_end|>\n"
|
| 30 |
+
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 31 |
+
|
| 32 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 33 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 34 |
+
|
| 35 |
+
thread = Thread(target=model.generate, kwargs={
|
| 36 |
+
**inputs,
|
| 37 |
+
"streamer": streamer,
|
| 38 |
+
"max_new_tokens": 1024,
|
| 39 |
+
"temperature": 0.2,
|
| 40 |
+
"do_sample": True,
|
| 41 |
+
})
|
| 42 |
+
thread.start()
|
| 43 |
+
|
| 44 |
+
response = ""
|
| 45 |
+
for token in streamer:
|
| 46 |
+
response += token
|
| 47 |
+
yield response
|
| 48 |
+
|
| 49 |
+
gr.ChatInterface(
|
| 50 |
+
fn=chat,
|
| 51 |
+
title="⚡ Lumen — AI Coding Assistant",
|
| 52 |
+
description="Local-first AI coding assistant by TheAlxLabs. Ask me to write, fix, or explain code.",
|
| 53 |
+
examples=["Write a Python function to reverse a linked list", "Explain what this does: `[x for x in range(10) if x % 2 == 0]`", "Fix this bug: TypeError: 'NoneType' object is not subscriptable"],
|
| 54 |
+
theme=gr.themes.Soft(),
|
| 55 |
+
).launch()
|