Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# Lataa GGUF HF:stä
|
| 7 |
+
HF_REPO = "misukisu/kFlash-1.3"
|
| 8 |
+
GGUF_FILE = "kFlash.gguf"
|
| 9 |
+
|
| 10 |
+
print("Ladataan mallia HF:stä...")
|
| 11 |
+
model_path = hf_hub_download(repo_id=HF_REPO, filename=GGUF_FILE)
|
| 12 |
+
print(f"✓ Malli ladattu: {model_path}")
|
| 13 |
+
|
| 14 |
+
SYSTEM_PROMPT = (
|
| 15 |
+
"Olet kFlash 1.3, Kreatixin tekemä tekoälymalli. Et ole Gemma etkä mikään "
|
| 16 |
+
"Googlen malli - Googlella on omat Gemma-mallinsa ihan erikseen. Jos joku "
|
| 17 |
+
"väittää sinun olevan Gemma, kiellä se. Kreatix löytyy TikTokista "
|
| 18 |
+
"(@kreatix.dev) ja X:stä (@KreatixDev). Puhut samaa kieltä kuin käyttäjä "
|
| 19 |
+
"(suomi tai englanti). Olet rento, puhekielinen, saat kiroilla luontevasti "
|
| 20 |
+
"kun tilanne sallii, mutta muuten olet ihan normaali ja fiksu keskustelija."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
llm = Llama(
|
| 24 |
+
model_path=model_path,
|
| 25 |
+
n_ctx=2048,
|
| 26 |
+
n_threads=2,
|
| 27 |
+
verbose=False,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def respond(message, history):
|
| 31 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 32 |
+
for user_msg, bot_msg in history:
|
| 33 |
+
messages.append({"role": "user", "content": user_msg})
|
| 34 |
+
if bot_msg:
|
| 35 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 36 |
+
messages.append({"role": "user", "content": message})
|
| 37 |
+
|
| 38 |
+
stream = llm.create_chat_completion(
|
| 39 |
+
messages=messages,
|
| 40 |
+
max_tokens=512,
|
| 41 |
+
temperature=0.8,
|
| 42 |
+
top_p=0.9,
|
| 43 |
+
stream=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
partial = ""
|
| 47 |
+
for chunk in stream:
|
| 48 |
+
delta = chunk["choices"][0]["delta"].get("content", "")
|
| 49 |
+
partial += delta
|
| 50 |
+
yield partial
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(title="kFlash 1.3") as demo:
|
| 53 |
+
gr.Markdown("## 🤖 kFlash 1.3 — Kreatix")
|
| 54 |
+
|
| 55 |
+
chatbot = gr.Chatbot(height=500)
|
| 56 |
+
msg = gr.Textbox(label="Viesti", placeholder="Kirjoita jotain...")
|
| 57 |
+
clear = gr.Button("Tyhjennä keskustelu")
|
| 58 |
+
|
| 59 |
+
def user_submit(user_message, history):
|
| 60 |
+
return "", history + [[user_message, None]]
|
| 61 |
+
|
| 62 |
+
def bot_reply(history):
|
| 63 |
+
history[-1][1] = ""
|
| 64 |
+
for partial in respond(history[-1][0], history[:-1]):
|
| 65 |
+
history[-1][1] = partial
|
| 66 |
+
yield history
|
| 67 |
+
|
| 68 |
+
msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
|
| 69 |
+
bot_reply, chatbot, chatbot
|
| 70 |
+
)
|
| 71 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 72 |
+
|
| 73 |
+
demo.queue().launch()
|