dv-gf-textgen / app.py
alakxender's picture
init
cb39169
raw
history blame
1.76 kB
# Import necessary libraries
import torch
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, pipeline
import gradio as gr
# Load Goldfish model for Dhivehi
language = 'Dhivehi'
# Load model
goldfish_model = 'goldfish-models/div_thaa_full'
config = AutoConfig.from_pretrained(goldfish_model)
tokenizer = AutoTokenizer.from_pretrained(goldfish_model)
model = AutoModelForCausalLM.from_pretrained(goldfish_model, config=config)
if torch.cuda.is_available():
model = model.cuda() # Load onto GPU
# Create text generation pipeline
text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer)
# Function to generate text
def generate_text(input_text):
output = text_generator(input_text, max_new_tokens=25, add_special_tokens=False, do_sample=True)
return output[0]['generated_text']
# Create Gradio interface
styles = """
.thaana textarea {
font-size: 18px !important;
font-family: 'MV_Faseyha', 'Faruma', 'A_Faruma', 'Noto Sans Thaana', 'MV Boli';
line-height: 1.8 !important;
}
"""
demo = gr.Interface(
fn=generate_text,
css=styles,
inputs=gr.Textbox(lines=2, label="Enter Dhivehi Text", rtl=True, elem_classes="thaana"),
outputs=gr.Textbox(lines=2, rtl=True, elem_classes="thaana"),
title="Demo Dhivehi Text Generator",
description="Generate text in Dhivehi language. This model is trained to generate coherent text based on the input prompt.",
article="<p>Model: Goldfish is a suite of monolingual language models trained for 350 languages. This model is the Dhivehi (Thaana script). For more details, visit the <a href='https://github.com/tylerachang/goldfish' target='_blank'>Goldfish Models GitHub repository</a>.</p>"
)
# Launch the app
demo.launch()