Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,105 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
stream=True,
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
yield
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Model paths
|
| 8 |
+
MODEL_REPO_ID = "LGAI-EXAONE/EXAONE-4.0-1.2B"
|
| 9 |
+
GGUF_REPO_ID = "lmstudio-community/LGAI-EXAONE-4.0-1.2B-GGUF"
|
| 10 |
+
GGUF_FILENAME = "A.X-4.0-Light-imatrix-IQ1_S.gguf"
|
| 11 |
+
TOKENIZER_DIR = "exaone-tokenizer"
|
| 12 |
+
TEMPLATE_PATH = os.path.join(TOKENIZER_DIR, "chat_template.jinja")
|
| 13 |
|
| 14 |
+
# Download GGUF model
|
| 15 |
+
print("π Downloading GGUF model...")
|
| 16 |
+
model_path = hf_hub_download(repo_id=GGUF_REPO_ID, filename=GGUF_FILENAME)
|
| 17 |
+
print(f"β
Model downloaded to: {model_path}")
|
| 18 |
|
| 19 |
+
# Load tokenizer
|
| 20 |
+
print("π Loading tokenizer...")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_DIR, trust_remote_code=True)
|
| 22 |
+
print("β
Tokenizer loaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
try:
|
| 25 |
+
with open(TEMPLATE_PATH, "r", encoding="utf-8") as f:
|
| 26 |
+
tokenizer.chat_template = f.read()
|
| 27 |
+
print("β
Chat template loaded.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Could not load chat_template.jinja: {e}")
|
| 30 |
+
tokenizer.chat_template = None
|
| 31 |
|
| 32 |
+
# Load model
|
| 33 |
+
llm = Llama(
|
| 34 |
+
model_path=model_path,
|
| 35 |
+
n_ctx=2048,
|
| 36 |
+
n_threads=os.cpu_count(),
|
| 37 |
+
n_gpu_layers=-1,
|
| 38 |
+
use_mlock=True,
|
| 39 |
+
verbose=False
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Streaming chat function
|
| 43 |
+
def format_prompt(messages):
|
| 44 |
+
formatted = ""
|
| 45 |
+
for m in messages:
|
| 46 |
+
role = m["role"].upper()
|
| 47 |
+
formatted += f"{role}: {m['content']}\n"
|
| 48 |
+
return formatted + "ASSISTANT:"
|
| 49 |
+
|
| 50 |
+
def user_input_handler(user_message, history):
|
| 51 |
+
return "", history + [[user_message, None]]
|
| 52 |
+
|
| 53 |
+
def bot_stream(history):
|
| 54 |
+
user_message = history[-1][0]
|
| 55 |
+
history[-1][1] = ""
|
| 56 |
+
|
| 57 |
+
# Convert chat history to OpenAI format
|
| 58 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 59 |
+
for human, assistant in history[:-1]:
|
| 60 |
+
messages.append({"role": "user", "content": human})
|
| 61 |
+
if assistant:
|
| 62 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 63 |
+
messages.append({"role": "user", "content": user_message})
|
| 64 |
|
| 65 |
+
try:
|
| 66 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 67 |
+
except Exception:
|
| 68 |
+
prompt = format_prompt(messages)
|
| 69 |
|
| 70 |
+
# Generate streaming output
|
| 71 |
+
generator = llm.create_completion(
|
| 72 |
+
prompt=prompt,
|
| 73 |
+
max_tokens=512,
|
| 74 |
+
temperature=0.7,
|
| 75 |
+
top_p=0.9,
|
| 76 |
stream=True,
|
| 77 |
+
stop=["</s>", "<|endoftext|>", "USER:", "ASSISTANT:"]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
for chunk in generator:
|
| 81 |
+
token = chunk["choices"][0]["text"]
|
| 82 |
+
history[-1][1] += token
|
| 83 |
+
yield history
|
| 84 |
+
|
| 85 |
+
# Gradio UI
|
| 86 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 87 |
+
gr.Markdown("## π€ EXAONE-4.0-1.2B Streaming")
|
| 88 |
+
chatbot = gr.Chatbot(label="Chat History", height=600)
|
| 89 |
+
with gr.Row():
|
| 90 |
+
msg = gr.Textbox(placeholder="Type a message...", label="Your Message", scale=8)
|
| 91 |
+
send_btn = gr.Button("Send", scale=1)
|
| 92 |
+
clear_btn = gr.Button("Clear Chat", scale=1, variant="secondary")
|
| 93 |
+
|
| 94 |
+
# Chat events
|
| 95 |
+
msg.submit(user_input_handler, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 96 |
+
bot_stream, chatbot, chatbot
|
| 97 |
+
)
|
| 98 |
+
send_btn.click(user_input_handler, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 99 |
+
bot_stream, chatbot, chatbot
|
| 100 |
+
)
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
clear_btn.click(lambda: [], None, chatbot)
|
| 103 |
|
| 104 |
+
demo.queue()
|
| 105 |
+
demo.launch()
|