File size: 2,287 Bytes
c9e3606
4aad9aa
143bd1b
770edf5
ac4bdc5
 
c9e3606
ac4bdc5
 
 
 
c9e3606
ac4bdc5
c9e3606
ac4bdc5
c9e3606
ac4bdc5
 
 
 
 
 
 
 
 
770edf5
ac4bdc5
 
 
 
 
c9e3606
 
ac4bdc5
 
c9e3606
 
ac4bdc5
 
 
 
 
c9e3606
 
 
 
 
 
 
 
 
143bd1b
c9e3606
 
 
 
 
 
4aad9aa
 
143bd1b
 
 
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
from operator import ge
from xml.dom.expatbuilder import theDOMImplementation
import gradio as gr
import os 
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Optional: cache loaded models to avoid reloading every time
model_cache = {}

def generate(model_name, text, max_new_tokens, top_k):    
    if model_name == "Medium-GPTNeo":
        model_id = "tniranjan/finetuned_gptneo-base-tinystories-ta_v3"
    elif model_name == "Small-GPTNeo":
        model_id = "tniranjan/finetuned_tinystories_33M_tinystories_ta"
    elif model_name == "Small-LLaMA":
        model_id = "tniranjan/finetuned_Llama_tinystories_tinystories_ta"

    # Load model and tokenizer (from cache if available)
    if model_id not in model_cache:
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        model = AutoModelForCausalLM.from_pretrained(model_id)
        model_cache[model_id] = (tokenizer, model)
    else:
        tokenizer, model = model_cache[model_id]

    inputs = tokenizer(text, return_tensors="pt")
    
    # Generate text
    output = model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        top_k=top_k,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id,
    )

    # Decode generated tokens
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)

    return generated_text
    
demo = gr.Interface(
    generate,
    title="Kurunkathai: Tinystories in Tamil",
    description="Generate Tamil stories for toddlers using Kurunkathai. Write the first line or so and click 'Submit' to generate a story.",
    inputs=[
        gr.Dropdown(
            choices=["Medium-GPTNeo","Small-GPTNeo", "Small-LLaMA"],
            label="Model",
            value="Small-GPTNeo",
        ),
        gr.Textbox(value="சிறிய குட்டி செல்லி, ஒரு அழகான நாய்க்குட்டியைக் கண்டாள்.", label="Text"),
        gr.Number(minimum=25, maximum=250, value=100, step=1, label="Max new tokens"),
        gr.Number(minimum=1,  maximum=150, value=35, step=1, label="Top-k"),   
    ], 
    outputs=[
        gr.Textbox(label="Generated Story"),
    ], 
    theme = "Monochrome",)

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