Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,72 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
system_message,
|
9 |
-
max_tokens,
|
10 |
-
temperature,
|
11 |
-
top_p,
|
12 |
-
hf_token: gr.OAuthToken,
|
13 |
-
):
|
14 |
-
"""
|
15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
16 |
-
"""
|
17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
18 |
-
|
19 |
-
messages = [{"role": "system", "content": system_message}]
|
20 |
-
|
21 |
-
messages.extend(history)
|
22 |
-
|
23 |
messages.append({"role": "user", "content": message})
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
for message in client.chat_completion(
|
28 |
messages,
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
],
|
61 |
-
)
|
62 |
-
|
63 |
-
with gr.Blocks() as demo:
|
64 |
-
with gr.Sidebar():
|
65 |
-
gr.LoginButton()
|
66 |
-
chatbot.render()
|
67 |
-
|
68 |
-
|
69 |
-
if __name__ == "__main__":
|
70 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
+
from threading import Thread
|
6 |
|
7 |
+
# Remove GPU decorator since we are CPU-only
|
8 |
+
def predict(message, history):
|
9 |
+
# Load model and tokenizer on CPU
|
10 |
+
model_id = "kurakurai/Luth-0.6B-Instruct"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
model_id,
|
14 |
+
device_map="cpu", # CPU only
|
15 |
+
torch_dtype=torch.float16,
|
16 |
+
trust_remote_code=True,
|
17 |
+
load_in_4bit=False # 4-bit quantization not supported on CPU
|
18 |
+
)
|
19 |
|
20 |
+
# Format conversation history for chat template
|
21 |
+
messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
22 |
+
for conv in history for i, msg in enumerate(conv) if msg]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
messages.append({"role": "user", "content": message})
|
24 |
+
|
25 |
+
# Apply chat template
|
26 |
+
input_ids = tokenizer.apply_chat_template(
|
|
|
27 |
messages,
|
28 |
+
add_generation_prompt=True,
|
29 |
+
return_tensors="pt",
|
30 |
+
tokenize=True
|
31 |
+
).to('cpu') # CPU device
|
32 |
+
|
33 |
+
# Setup streamer for real-time output
|
34 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
35 |
+
|
36 |
+
# Generation parameters
|
37 |
+
generate_kwargs = dict(
|
38 |
+
input_ids=input_ids,
|
39 |
+
streamer=streamer,
|
40 |
+
max_new_tokens=256,
|
41 |
+
do_sample=True,
|
42 |
+
temperature=0.3,
|
43 |
+
min_p=0.15,
|
44 |
+
repetition_penalty=1.05,
|
45 |
+
pad_token_id=tokenizer.eos_token_id
|
46 |
+
)
|
47 |
+
|
48 |
+
# Start generation in separate thread
|
49 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
50 |
+
t.start()
|
51 |
+
|
52 |
+
# Stream tokens
|
53 |
+
partial_message = ""
|
54 |
+
for new_token in streamer:
|
55 |
+
partial_message += new_token
|
56 |
+
yield partial_message
|
57 |
|
58 |
+
# Setup Gradio interface
|
59 |
+
gr.ChatInterface(
|
60 |
+
predict,
|
61 |
+
description="""
|
62 |
+
<center><h2>Kurakura AI Luth-0.6B-Instruct Chat</h2></center>
|
63 |
+
|
64 |
+
Chat with [Luth-0.6B-Instruct](https://huggingface.co/kurakurai/Luth-0.6B-Instruct), a French-tuned version of Qwen3-0.6B.
|
65 |
+
""",
|
66 |
+
examples=[
|
67 |
+
"Peux-tu résoudre l'équation 3x - 7 = 11 pour x ?",
|
68 |
+
"Explique la photosynthèse en termes simples.",
|
69 |
+
"Écris un petit poème sur l'intelligence artificielle."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
],
|
71 |
+
theme=gr.themes.Soft(primary_hue="purple"),
|
72 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|