File size: 1,309 Bytes
fc5a194
8ec7029
 
fc5a194
 
b403934
fc5a194
b403934
fc5a194
 
efc1c69
b403934
8ec7029
b403934
8ec7029
 
 
 
fc5a194
 
8ec7029
b403934
8ec7029
b403934
8ec7029
b403934
8ec7029
 
b403934
8ec7029
 
 
e618b15
8ec7029
b403934
8ec7029
 
b403934
fc5a194
 
 
b403934
fc5a194
 
 
b403934
fc5a194
b403934
 
fc5a194
b403934
fc5a194
b403934
 
 
 
 
 
 
 
ae20b70
fc5a194
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# =========================
# MODEL (CPU)
# =========================
torch.set_default_device("cpu")

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/phi-1",
    torch_dtype=torch.float32,   # important pour CPU
    trust_remote_code=True
)

tokenizer = AutoTokenizer.from_pretrained(
    "microsoft/phi-2",
    trust_remote_code=True
)

# =========================
# CHAT
# =========================
def chat(message):
    inputs = tokenizer(
        message,
        return_tensors="pt",
        return_attention_mask=False
    )

    outputs = model.generate(
        **inputs,
       
        do_sample=True,
        temperature=0.7
    )

    return tokenizer.batch_decode(outputs)[0]


# =========================
# UI + API
# =========================
with gr.Blocks() as demo:

    gr.Markdown("# 🤖 Phi-2 CPU Chat")

    inp = gr.Textbox(label="Message")
    out = gr.Textbox(label="Réponse")

    btn = gr.Button("Send")

    btn.click(chat, inp, out)

    # ✅ endpoint API auto
    gr.Interface(
        fn=chat,
        inputs=gr.Textbox(),
        outputs=gr.Textbox()
    )

# =========================
# RUN
# =========================
if __name__ == "__main__":
    demo.launch()