Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,868 Bytes
0453eb7 ec0863c 0453eb7 ec0863c 0453eb7 910b9f4 0453eb7 84ca9c3 0453eb7 84ca9c3 0453eb7 ec0863c 0453eb7 |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import spaces
import gradio as gr
from transformers import pipeline, GPT2TokenizerFast
#model_id = "alakxender/dv-wiki-gpt2"
model_id = "alakxender/dv-articles-gpt2"
tokenizer = GPT2TokenizerFast.from_pretrained(model_id, model_max_length=128)
generator = pipeline("text-generation", model=model_id, tokenizer=tokenizer, pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id)
@spaces.GPU
def generate_text(prompt, max_length, temperature):
try:
generated = generator(
prompt,
max_length=max_length,
#num_beams=10,
#no_repeat_ngram_size=2,
temperature=temperature,
do_sample=True,
repetition_penalty=1.4
)
return generated[0]['generated_text']
except Exception as e:
return f"Something went wrong, try again. Error: {str(e)}"
styles = """
.thaana textarea {
font-size: 18px !important;
font-family: 'MV_Faseyha', 'Faruma', 'A_Faruma', 'Noto Sans Thaana', 'MV Boli';
line-height: 1.8 !important;
}
"""
def create_interface():
with gr.Blocks(css=styles) as demo:
gr.Markdown("# Dhivehi Text Generator (GPT-2, Wiki)")
gr.Markdown(
"This is a GPT-2 model trained from Dhivehi text data from wikipedia\n"
"Enter some text and generate a new text, adjust the parameters to generate text."
)
gr.Markdown("""
**Parameters:**
- **Temperature**: Controls the creativity of the output.
- Lower values (0.2) = More focused and predictable text
- Higher values (0.8) = More diverse and creative text
- **Maximum Length**: Controls the length of generated text.
- Higher values generate longer, more detailed results
- Note: Longer texts take more time to generate
""")
with gr.Row():
input_temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="Temperature",
)
input_max_length = gr.Slider(
minimum=10,
maximum=128,
value=60,
step=1,
label="Maximum Length",
)
with gr.Row():
with gr.Column(scale=1):
input_prompt = gr.Textbox(
label="Enter dhivehi text prompt",
placeholder="ދިވެހިން",
lines=5,
rtl=True,
elem_classes="thaana"
)
with gr.Column(scale=1):
output_text = gr.Textbox(
label="Generated Text",
lines=5,
interactive=True,
rtl=True,
elem_classes="thaana"
)
with gr.Row():
generate_btn = gr.Button("Generate", variant="primary")
clear_btn = gr.ClearButton([input_prompt, output_text])
generate_btn.click(
fn=generate_text,
inputs=[input_prompt, input_max_length, input_temperature],
outputs=output_text
)
gr.Examples(
examples=[
["ދިވެހިރާއްޖެ"],
["އެމެރިކާ އިންތިޚާބު"],
["ސަލާމް"],
["ދުނިޔޭގެ ސިއްޙަތު ޖަމްޢިއްޔާ"],
["ޤަދީމީ ސަގާފަތް"],
["ޑިމޮކްރަސީ"]
],
inputs=input_prompt
)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.queue().launch(show_api=False) |