File size: 2,663 Bytes
d10214a
539debe
d10214a
 
318834e
d10214a
 
 
 
 
7888e51
d10214a
ff2a72a
2e4a4ab
 
 
91a8143
d10214a
91a8143
d10214a
ff2a72a
 
2e4a4ab
d10214a
1940b48
d10214a
 
 
91a8143
 
d10214a
 
 
91a8143
d10214a
 
2e4a4ab
 
d10214a
 
ff2a72a
2e4a4ab
ff2a72a
 
 
2e4a4ab
d10214a
2e4a4ab
d10214a
 
ff2a72a
d10214a
 
 
2e4a4ab
 
 
 
 
 
ce2e0ae
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
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

hf_token= os.getenv("access_token")
tokenizer = AutoTokenizer.from_pretrained("afrizalha/Sasando-1-25M", token=hf_token)
tiny = AutoModelForCausalLM.from_pretrained("afrizalha/Sasando-1-25M", token=hf_token)
tinier = AutoModelForCausalLM.from_pretrained("afrizalha/Sasando-1-7M", token=hf_token)


desc = """Sasando-1 is a tiny, highly experimental text generator built using the Phi-3 architecture. It comes with two variations of microscopic sizes: 7M and 25M parameters. It is trained on a tightly-controlled Indo4B dataset filtered to only have 18000 unique words. The method is inspired by Microsoft's TinyStories paper which demonstrates that a tiny language model can produce fluent text when trained on tightly-controlled dataset.\n\nTry prompting with two simple words, and let the model continue. Fun examples provided below."""

def generate(starting_text=None, choice=None, temp=None, top_p=None):
    if info:
        return desc
    
    if choice == '7M':
        model = tinier
    elif choice == '25M':
        model = tiny
    elif choice == "Info":
        return desc
    
    results = []
    for i in range(5):
        inputs = tokenizer([starting_text], return_tensors="pt").to(model.device)
        outputs = model.generate(
            inputs=inputs.input_ids,
            max_new_tokens=32-len(inputs.input_ids[0]),
            do_sample=True,
            temperature=temp,
            top_p=top_p
        )
        outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
        outputs = outputs[:outputs.find(".")]
        results.append(outputs)
    return "\n\n".join(results)

with gr.Blocks(theme=gr.themes.Soft()) as app:
    starting_text = gr.Textbox(label="Starting text", value="cinta adalah")
    choice = gr.Radio(["7M", "25M", "Info"], label="Select model", info="Built with the Phi-3 architecture", value='Info')
    
    with gr.Row() as slider_options:
        temp = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.1, value=0.7)
        top_p = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, step=0.1, value=0.5)
    
    res = gr.Textbox(label="Continuation")
    
    gr.Interface(
        fn=generate,
        inputs=[starting_text, choice, slider_options],
        outputs=[res],
        allow_flagging="never",
        title="Sasando-1",
    )
    
    examples = gr.Examples([
        ["gue"], ["presiden"], ["cinta adalah"], ["allah, aku"], ["dia marah karena"],
        ["inflasi"], ["kolam renang"], ["messi"], ["jalan-jalan"], ["komputer itu"]
    ], [starting_text])
app.launch()