Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,110 @@
|
|
| 1 |
import torch
|
| 2 |
import gc
|
| 3 |
import json
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from unsloth import FastLanguageModel
|
| 8 |
from transformers import TextIteratorStreamer
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
torch.cuda.empty_cache()
|
| 15 |
gc.collect()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 21 |
-
model_name=
|
| 22 |
-
max_seq_length=
|
| 23 |
-
dtype=
|
| 24 |
-
load_in_4bit=True,
|
| 25 |
-
|
| 26 |
)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# -------------------------------------------------------------------
|
| 32 |
-
# Chat function with streaming
|
| 33 |
-
# -------------------------------------------------------------------
|
| 34 |
def stream_chat(message, history):
|
| 35 |
-
#
|
| 36 |
messages = [
|
| 37 |
-
{"role": "
|
| 38 |
-
{"role": "assistant", "content": "Ok, I go dey reply for Pidgin."},
|
| 39 |
]
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
messages.append({"role": "user", "content": message})
|
| 46 |
|
|
|
|
| 47 |
inputs = tokenizer.apply_chat_template(
|
| 48 |
messages,
|
| 49 |
-
add_generation_prompt=True,
|
| 50 |
-
return_tensors="pt"
|
| 51 |
).to(model.device)
|
| 52 |
|
| 53 |
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
| 54 |
|
| 55 |
generate_kwargs = dict(
|
| 56 |
-
input_ids=inputs,
|
| 57 |
-
streamer=streamer,
|
| 58 |
-
max_new_tokens=
|
| 59 |
-
temperature=0.8,
|
| 60 |
-
|
| 61 |
-
|
| 62 |
)
|
| 63 |
|
| 64 |
-
thread
|
|
|
|
| 65 |
thread.start()
|
| 66 |
|
| 67 |
-
|
| 68 |
-
for
|
| 69 |
-
|
| 70 |
-
yield
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
# Save chat
|
| 74 |
-
#
|
| 75 |
def save_chat(history):
|
| 76 |
-
|
| 77 |
for human, bot in history:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return
|
| 86 |
-
|
| 87 |
-
#
|
| 88 |
-
# Gradio
|
| 89 |
-
#
|
| 90 |
-
with gr.Blocks(title="π³π¬
|
| 91 |
-
gr.HTML("<h1
|
| 92 |
-
|
| 93 |
-
chatbot = gr.Chatbot(height=400, show_label=False)
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
-
msg = gr.Textbox(placeholder="Type your message...", scale=4)
|
| 97 |
-
send = gr.Button("Send", variant="primary", scale=1)
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
-
clear = gr.Button("Clear Chat")
|
| 101 |
-
save_btn = gr.Button("πΎ Save Conversation")
|
| 102 |
download_file = gr.File()
|
| 103 |
|
| 104 |
def respond(message, history):
|
|
@@ -116,8 +122,5 @@ with gr.Blocks(title="π³π¬ Pidgin English Chatbot") as demo:
|
|
| 116 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 117 |
save_btn.click(save_chat, inputs=[chatbot], outputs=[download_file])
|
| 118 |
|
| 119 |
-
# -------------------------------------------------------------------
|
| 120 |
-
# Launch
|
| 121 |
-
# -------------------------------------------------------------------
|
| 122 |
if __name__ == "__main__":
|
| 123 |
demo.launch(share=True, debug=True)
|
|
|
|
| 1 |
import torch
|
| 2 |
import gc
|
| 3 |
import json
|
| 4 |
+
import time
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from unsloth import FastLanguageModel
|
| 9 |
from transformers import TextIteratorStreamer
|
| 10 |
|
| 11 |
+
# ---------------------
|
| 12 |
+
# Setup + Model Load
|
| 13 |
+
# ---------------------
|
| 14 |
+
|
| 15 |
+
# Clear out memory before loading
|
| 16 |
torch.cuda.empty_cache()
|
| 17 |
gc.collect()
|
| 18 |
|
| 19 |
+
MODEL_ID = "Ephraimmm/PIDGIN_gemma-3"
|
| 20 |
+
CONTEXT_LEN = 128000 # Gemma-3 default context window as per blog
|
| 21 |
+
|
| 22 |
+
print("Using Unsloth Gemma-3 model with 128K context window...")
|
| 23 |
+
|
| 24 |
+
# Make sure your environment has updated versions:
|
| 25 |
+
# pip install -U unsloth unsloth_zoo transformers
|
| 26 |
|
| 27 |
+
# Load the quantized model with Unsloth
|
| 28 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 29 |
+
model_name = MODEL_ID,
|
| 30 |
+
max_seq_length = CONTEXT_LEN,
|
| 31 |
+
dtype = None, # Let Unsloth pick appropriate dtype
|
| 32 |
+
load_in_4bit = True,
|
| 33 |
+
trust_remote_code = True,
|
| 34 |
)
|
| 35 |
+
FastLanguageModel.for_inference(model)
|
| 36 |
+
print("β
Model loaded (4-bit dynamic if available)")
|
| 37 |
|
| 38 |
+
# ---------------------
|
| 39 |
+
# Chat Streaming Function
|
| 40 |
+
# ---------------------
|
|
|
|
|
|
|
|
|
|
| 41 |
def stream_chat(message, history):
|
| 42 |
+
# Build message list as required by Unsloth
|
| 43 |
messages = [
|
| 44 |
+
{"role": "system", "content": "You be Naija assistant. You must always reply for Pidgin English."}
|
|
|
|
| 45 |
]
|
| 46 |
+
if history:
|
| 47 |
+
for human, bot in history:
|
| 48 |
+
messages.append({"role": "user", "content": human})
|
| 49 |
+
messages.append({"role": "assistant", "content": bot})
|
|
|
|
| 50 |
messages.append({"role": "user", "content": message})
|
| 51 |
|
| 52 |
+
# Using apply_chat_template (supported by Unsloth) to handle the formatting
|
| 53 |
inputs = tokenizer.apply_chat_template(
|
| 54 |
messages,
|
| 55 |
+
add_generation_prompt = True,
|
| 56 |
+
return_tensors = "pt"
|
| 57 |
).to(model.device)
|
| 58 |
|
| 59 |
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
| 60 |
|
| 61 |
generate_kwargs = dict(
|
| 62 |
+
input_ids = inputs,
|
| 63 |
+
streamer = streamer,
|
| 64 |
+
max_new_tokens = 512,
|
| 65 |
+
temperature = 0.8,
|
| 66 |
+
do_sample = True,
|
| 67 |
+
top_p = 0.9,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Run in background thread to stream
|
| 71 |
+
thread = Thread(target = model.generate, kwargs = generate_kwargs)
|
| 72 |
thread.start()
|
| 73 |
|
| 74 |
+
output = ""
|
| 75 |
+
for partial in streamer:
|
| 76 |
+
output += partial
|
| 77 |
+
yield output
|
| 78 |
|
| 79 |
+
# ---------------------
|
| 80 |
+
# Save chat to file (JSON format)
|
| 81 |
+
# ---------------------
|
| 82 |
def save_chat(history):
|
| 83 |
+
export = []
|
| 84 |
for human, bot in history:
|
| 85 |
+
export.append({"role": "user", "content": human})
|
| 86 |
+
export.append({"role": "assistant", "content": bot})
|
| 87 |
+
|
| 88 |
+
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 89 |
+
fname = f"conversation_{timestamp}.json"
|
| 90 |
+
with open(fname, "w", encoding="utf-8") as f:
|
| 91 |
+
json.dump(export, f, ensure_ascii=False, indent=2)
|
| 92 |
+
return fname
|
| 93 |
+
|
| 94 |
+
# ---------------------
|
| 95 |
+
# UI with Gradio
|
| 96 |
+
# ---------------------
|
| 97 |
+
with gr.Blocks(title="π³π¬ PIDGIN Gemma-3 Chatbot") as demo:
|
| 98 |
+
gr.HTML("<h1><center>π³π¬ PIDGIN Gemma-3 Chatbot</center></h1>")
|
| 99 |
+
chatbot = gr.Chatbot(height=450, show_label=False)
|
|
|
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
+
msg = gr.Textbox(placeholder="Type your message here...", lines=2, scale=4)
|
| 103 |
+
send = gr.Button("Send", variant="primary", scale=1, size="lg")
|
| 104 |
|
| 105 |
with gr.Row():
|
| 106 |
+
clear = gr.Button("Clear Chat", variant="secondary", scale=1)
|
| 107 |
+
save_btn = gr.Button("πΎ Save Conversation", variant="secondary", scale=1)
|
| 108 |
download_file = gr.File()
|
| 109 |
|
| 110 |
def respond(message, history):
|
|
|
|
| 122 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 123 |
save_btn.click(save_chat, inputs=[chatbot], outputs=[download_file])
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch(share=True, debug=True)
|