cyirr commited on
Commit
3d1320a
·
verified ·
1 Parent(s): 90ad06d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
+ import torch
4
+
5
+ model_id = "Lunar2441/deepseek-coder-mine"
6
+
7
+ print("Loading model...")
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_id,
11
+ torch_dtype=torch.float32,
12
+ device_map="cpu",
13
+ low_cpu_mem_usage=True,
14
+ )
15
+ print("Model ready!")
16
+
17
+ SYS = "You are an expert Python developer. You write clean, efficient, well-commented Python code. You follow PEP8, use Pythonic patterns, and always explain your reasoning."
18
+
19
+ def chat(message, history):
20
+ prompt = "<|im_start|>system\n" + SYS + "\n<|im_end|>\n"
21
+ for user, assistant in history:
22
+ prompt += "<|im_start|>user\n" + user + "\n<|im_end|>\n"
23
+ prompt += "<|im_start|>assistant\n" + assistant + "\n<|im_end|>\n"
24
+ prompt += "<|im_start|>user\n" + message + "\n<|im_end|>\n<|im_start|>assistant\n"
25
+
26
+ inputs = tokenizer(prompt, return_tensors="pt")
27
+ with torch.no_grad():
28
+ outputs = model.generate(
29
+ **inputs,
30
+ max_new_tokens=256,
31
+ temperature=0.7,
32
+ do_sample=True,
33
+ pad_token_id=tokenizer.eos_token_id,
34
+ )
35
+ response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
36
+ return response
37
+
38
+ gr.ChatInterface(
39
+ fn=chat,
40
+ title="DeepSeek Python Pro",
41
+ description="Fine-tuned Python coding assistant by Lunar2441",
42
+ examples=[
43
+ "Write a Python function to reverse a linked list",
44
+ "Explain decorators with an example",
45
+ "Write a binary search in Python",
46
+ ],
47
+ ).launch()