Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import config
|
| 6 |
+
import multiprocessing
|
| 7 |
+
|
| 8 |
+
print("Downloading model...")
|
| 9 |
+
|
| 10 |
+
model_path = hf_hub_download(
|
| 11 |
+
repo_id=config.MODEL_REPO,
|
| 12 |
+
filename=config.MODEL_FILE
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
print("Loading model...")
|
| 16 |
+
|
| 17 |
+
cpu_threads = multiprocessing.cpu_count()
|
| 18 |
+
|
| 19 |
+
llm = Llama(
|
| 20 |
+
model_path=model_path,
|
| 21 |
+
n_ctx=config.CTX_SIZE,
|
| 22 |
+
n_threads=cpu_threads,
|
| 23 |
+
n_batch=512,
|
| 24 |
+
use_mmap=True,
|
| 25 |
+
use_mlock=False,
|
| 26 |
+
verbose=False
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
SYSTEM_PROMPT = """You are DeepSeek Coder, an expert programming assistant.
|
| 30 |
+
You write clean, correct, efficient code.
|
| 31 |
+
Always return only code unless explanation is requested.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def format_prompt(message, history):
|
| 35 |
+
|
| 36 |
+
prompt = SYSTEM_PROMPT + "\n\n"
|
| 37 |
+
|
| 38 |
+
for user, assistant in history:
|
| 39 |
+
prompt += f"User: {user}\nAssistant: {assistant}\n"
|
| 40 |
+
|
| 41 |
+
prompt += f"User: {message}\nAssistant:"
|
| 42 |
+
|
| 43 |
+
return prompt
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def generate(message, history):
|
| 47 |
+
|
| 48 |
+
prompt = format_prompt(message, history)
|
| 49 |
+
|
| 50 |
+
output = ""
|
| 51 |
+
|
| 52 |
+
for token in llm(
|
| 53 |
+
prompt,
|
| 54 |
+
max_tokens=config.MAX_TOKENS,
|
| 55 |
+
temperature=config.TEMPERATURE,
|
| 56 |
+
stream=True
|
| 57 |
+
):
|
| 58 |
+
text = token["choices"][0]["text"]
|
| 59 |
+
output += text
|
| 60 |
+
yield output
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 64 |
+
|
| 65 |
+
gr.Markdown("# DeepSeek Coder 1.3B (Production GGUF)")
|
| 66 |
+
|
| 67 |
+
chatbot = gr.Chatbot(height=500)
|
| 68 |
+
|
| 69 |
+
msg = gr.Textbox(
|
| 70 |
+
placeholder="Ask coding question...",
|
| 71 |
+
container=False
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
clear = gr.Button("Clear")
|
| 75 |
+
|
| 76 |
+
def user(user_message, history):
|
| 77 |
+
return "", history + [[user_message, ""]]
|
| 78 |
+
|
| 79 |
+
def bot(history):
|
| 80 |
+
|
| 81 |
+
user_message = history[-1][0]
|
| 82 |
+
|
| 83 |
+
for response in generate(user_message, history[:-1]):
|
| 84 |
+
history[-1][1] = response
|
| 85 |
+
yield history
|
| 86 |
+
|
| 87 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=True).then(
|
| 88 |
+
bot, chatbot, chatbot
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
demo.queue()
|
| 95 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|