File size: 2,649 Bytes
4929bc6
 
fa951d7
 
4929bc6
 
 
 
fa951d7
4929bc6
 
 
 
 
 
a103a7f
 
 
4929bc6
 
a103a7f
 
 
 
 
 
 
 
 
 
 
 
 
4929bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa951d7
4929bc6
 
 
fa951d7
4929bc6
 
 
 
 
 
 
fa951d7
4929bc6
 
fa951d7
4929bc6
 
 
 
fa951d7
 
4929bc6
fa951d7
4929bc6
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import gradio as gr

# Load pre-trained model and tokenizer
model_name = "PleIAs/OCRonos-Vintage"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# Set the device to GPU if available, otherwise use CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

def historical_generation(prompt, max_new_tokens=600):
    prompt = f"### Text ###\n{prompt}"
    inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
    input_ids = inputs["input_ids"].to(device)
    attention_mask = inputs["attention_mask"].to(device)

    # Generate text
    output = model.generate(
        input_ids,
        attention_mask=attention_mask,
        max_new_tokens=max_new_tokens,
        pad_token_id=tokenizer.eos_token_id,
        top_k=50,
        temperature=0.3,
        top_p=0.95,
        do_sample=True,
        repetition_penalty=1.5,
        bos_token_id=tokenizer.bos_token_id,
        eos_token_id=tokenizer.eos_token_id
    )

    # Decode the generated text
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    
    # Remove the prompt from the generated text
    generated_text = generated_text.replace("### Text ###\n", "").strip()

    # Tokenize the generated text
    tokens = tokenizer.tokenize(generated_text)
    
    # Create highlighted text output
    highlighted_text = []
    for token in tokens:
        # Remove special tokens and get the token type
        clean_token = token.replace("Δ ", "").replace("</w>", "")
        token_type = tokenizer.convert_ids_to_tokens([tokenizer.convert_tokens_to_ids(token)])[0]
        
        highlighted_text.append((clean_token, token_type))
    
    return highlighted_text

# Create Gradio interface
iface = gr.Interface(
    fn=historical_generation,
    inputs=[
        gr.Textbox(
            label="Prompt",
            placeholder="Enter a prompt for historical text generation...",
            lines=3
        ),
        gr.Slider(
            label="Max New Tokens",
            minimum=50,
            maximum=1000,
            step=50,
            value=600
        )
    ],
    outputs=gr.HighlightedText(
        label="Generated Historical Text",
        combine_adjacent=True,
        show_legend=True
    ),
    title="Historical Text Generation with OCRonos-Vintage",
    description="Generate historical-style text using the OCRonos-Vintage model. The output shows token types as highlights.",
    theme=gr.themes.Base()
)

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