alakxender commited on
Commit
d6fee57
·
1 Parent(s): 125e370
Files changed (2) hide show
  1. app.py +90 -0
  2. title_gen.py +57 -0
app.py CHANGED
@@ -69,6 +69,96 @@ with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
69
 
70
  Simply enter your Dhivehi text with typos, and the model will attempt to correct the common errors while preserving the meaning of your text.""")
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  # Launch the app
73
  if __name__ == "__main__":
 
74
  demo.launch()
 
69
 
70
  Simply enter your Dhivehi text with typos, and the model will attempt to correct the common errors while preserving the meaning of your text.""")
71
 
72
+ with gr.Tab("Title Generation"):
73
+ gr.Markdown("# <center>Dhivehi Article Title Generator</center>")
74
+ gr.Markdown("Enter Dhivehi article content to generate a title using a fine-tuned T5 model. Set the random seed for reproducibility. Enable sampling for creative/random results.")
75
+
76
+ import sys
77
+ sys.path.append('.')
78
+ from title_gen import generate_title
79
+
80
+ with gr.Row():
81
+ article_content = gr.Textbox(lines=10, label="Article Content", rtl=True, elem_classes="textbox1")
82
+ with gr.Row():
83
+ seed = gr.Slider(0, 10000, value=42, step=1, label="Random Seed")
84
+ use_sampling = gr.Checkbox(label="Use Sampling (Creative/Random)", value=False)
85
+ with gr.Row():
86
+ generated_title = gr.Textbox(label="Generated Title", rtl=True, elem_classes="textbox1" )
87
+ generate_btn = gr.Button("Generate Title")
88
+ generate_btn.click(
89
+ fn=generate_title,
90
+ inputs=[article_content, seed, use_sampling],
91
+ outputs=generated_title
92
+ )
93
+
94
+ gr.Examples(
95
+ examples=[
96
+ ["""އެހެންވެ ޕީކް ގަޑިތަކުގައި ކަރަންޓު ކެނޑޭ މައްސަލަ ބޮޑުވެ، އެންވަޔަރުމެންޓް މިނިސްޓަރު އިބްރާހިމް ތޯރިގު އޭރު ވިދާޅުވި ގޮތުގައި އެކަން ހައްލުކުރުމަށް 1.5 މެގަވޮޓުގެ އާ ޖެނަރޭޓަރެއް ގަނެފައިވެ އެވެ. އަދި މިދިޔަ މަހުގެ ތެރޭގައި އެ ޖެނަރޭޓަރު ބަހައްޓާނެކަމަށް ތޯރިގު ވިދާޅުވި އެވެ.
97
+
98
+ އައްޑޫ ސިޓީ ކައުންސިލުން އާންމުކުރި ވީޑިއޯ އިންޓަވިއުއެއްގައި މޭޔަރު އަލީ ނިޒާރު ވިދާޅުވީ، މި ފަހަރު ދިމާވ ގަމަށް ކަރަންޓު ދީފައިވާ ހައި ވޯލްޓޭޖް ކޭބަލްގެ މައްސަލައެއް ކަމަށެވެ.
99
+
100
+ އެ މައްސަލަ ހައްލުކުރުމަށް ފެނަކައިގެ އިންޖިނިއަރުންގެ ޓީމަކުން މަސައްކަތް ކުރަމުން އަންނަ ކަމަށާއި އެކަމަކު އަދިވެސް މައްސަލަ ދިމާވެފައިވާ ކޭބަލް އޮތް ސަރަހައްދު ސާފު ނުވާ ކަމަށް ނިޒާރު ވިދާޅުވި އެވެ.
101
+
102
+ އޭގެ ސަބަބުން އައްޑޫގެ އެކި ސަރަހައްދުތަކުން ކަރަންޓު ކެނޑުމުގެ މައްސަލަ ދިމާވާ ކަމަށް އޭނާ ވިދާޅުވި އެވެ.
103
+
104
+ "ކޮންމެވެސް ގޮތަކުން މަސައްކަތް ކުރުމުގެ ތެރެއިން ލިބިފައިވާ ގެއްލުމާ އެކު، އެކަން ރަނގަޅު ނުކުރެވި އޮތީ. ގަމަށް ކަރަންޓު ދޭ ކޮންމެ ފަހަރަކު ޕަވާ ސްޓޭޝަނުން ކަރަންޓު ފެއިލް ކޮށްލަނީ. އެހެންވެ، އައްޑޫގެ އެކި ތަންތަނުން ކަރަންޓު ކެނޑެނީ،" ނިޒާރު ވިދާޅުވި އެވެ.
105
+ """],
106
+ ["""މަސްތުވެގެން ހުރެ ނުރައްކާތެރިކޮށް ޓެކްސީ ކާރެއް ދުއްވި މީހަކު ހައްޔަރުކޮށްފި އެވެ.
107
+
108
+ އެ މައްސަލާގައި ފުލުހުން ހައްޔަރުކުރީ ކާރު ދުއްވި ހމ. ފްލެޓްނަންބަރު 07-1-4، މުހައްމަދު މާއިސް އެވެ.
109
+
110
+ ފުލުހުން ބުނީ މިދިޔަ ބުރާސްފަތި ދުވަހުގެ ރޭ ހުޅުމާލޭ ހައިވޭގައި ހެޑްލައިޓް ނުދިއްލާ، ހަތަރު ސިގްނަލް ޖައްސައިގެން ނުރައްކާތެރިކޮށް ބާރު ސްޕީޑްގައި ދުއްވާފައި ދިޔަ ކާރެއް ފުލުހުންގެ އެންގުމުން ވެސް މަޑުނުކޮށް ދިޔަ ކަމަށެވެ.
111
+
112
+ އެ ކާރު ހޯދުމުގެ މަސައްކަތް ފުލުހުން ކުރަމުން ގެންދަނިކޮށް، ހުޅުމާލޭ ހައިވަކަރު މަގުން ޓްރީޓޮޕް ހޮސްޕިޓަލާ ދިމާލަށް އެ ކާރު ދަތުރު ކުރަމުން ދަނިކޮށް ފުލުހުން މަޑުކުރަން އެންގުމުން ވެސް މަޑުނުކޮށް ދުއްވާފައި ދިޔަ ކަމަށް ފުލުހުން ބުންޏެވެ.
113
+
114
+ ފުލުހުން ބުނިގޮތުގައި ކާރު ދުއްވާފައި ދިޔައީ ބަމްޕަރުތަކަށާއި ގޯޅި އަޅާއިރު ވެސް ދުވެލި މަޑުނުކޮށެވެ.
115
+
116
+
117
+ ޑްރަގްސް ބޭނުން ކޮށްގެން ހުރެ ދުއްވި ކާރު.--
118
+
119
+ އެގޮތަށް ދުއްވަމުން ގޮސް ރަބަރުގަސްމަގު 97 ނަމްބަރު ފްލެޓް ކައިރިން ކާރު މަޑުކުރި އެ ކާރު ކައިރިއަށް ފުލުހުން ދިއުމުން އަނެއްކާވެސް ކާރު ދުއްވާލަން އުޅުނު ކަމަށް ވެސް ފުލުހުން ބުންޏެވެ.
120
+
121
+ ފުލުހުން ބުނީ ކާރު ހުއްޓުވިއިރު، ކާރުގެ ކަނާތްފަރާތު ފަހަތު ފުރޮޅުގެ ޓަޔަރު ނެތް ކަމަށާއި އެ ޓަޔަރު ފެނިފައިވަނީ 103 ނަމްބަރު ފްލެޓް ކައިރިން ކަމަށެވެ. އަދި ކާރަށް ވެސް އެކި ވަރުގެ ގެއްލުންތައް ލިިބިފައި ހުރި ކަމަށް ފުލުހުން ބުންޏެވެ.
122
+
123
+ ކާރު ދުއްވި މާއިސް ހައްޔަރު ކުރުމުން، ކޯޓުން ވަނީ އޭނާގެ ބަންދަށް ތިން ދުވަސް ޖަހައިފަ އެވެ.
124
+
125
+ ފުލުހުން ވަނީ އެ ކާރު ވެސް ޓޯކޮށްފަ އެވެ. ފުލުހުން އާންމުކުރި ފޮޓޯއިން ފެންނަ ގޮތުގައި އޭނާ ދުއްވ އަވަސް ރައިޑުގެ ޓެކްސީއެކެވެ.
126
+ """],
127
+
128
+ ],
129
+ inputs=article_content,
130
+ )
131
+
132
+ gr.Markdown("""## Notes:
133
+
134
+ ✨ **What does this do?**
135
+
136
+ Dhivehi article content and, with the help of a fine-tuned T5 model, conjures up a fitting title! Whether you want something straightforward or a bit more creative, just tweak the random seed or enable sampling for a dash of randomness.
137
+
138
+ - **Best for:** Quickly summarizing the essence of your article in a catchy title.
139
+ - **Creative mode:** Turn on sampling for surprising, out-of-the-box suggestions.
140
+ - **Consistent mode:** Keep sampling off for reliable, focused titles.
141
+
142
+ ⚠️ **Experimental Status**
143
+ This is an experimental model trained on a very small dataset of Dhivehi news articles. Results may vary significantly and the model is still in early stages of development.
144
+
145
+ ### Limitations
146
+
147
+ - The model shines with clear, well-written content. If your article is too short, vague, or off-topic, the title might be less impressive.
148
+ - Sometimes, creativity can go a little too wild—if you get a strange title, try a different seed or turn off sampling.
149
+ - Due to limited training data, the model may not handle all topics or writing styles equally well.
150
+ - Experimental nature means outputs should be reviewed carefully before use.
151
+
152
+ ### Tips for Best Results
153
+
154
+ - Use well-structured, news-style content similar to the training data
155
+ - Try multiple seeds to get different title options
156
+ - Keep content focused on a single main topic
157
+ - Review generated titles for accuracy and appropriateness
158
+
159
+ """)
160
+
161
  # Launch the app
162
  if __name__ == "__main__":
163
+ #demo.launch(server_name="0.0.0.0", server_port=7811)
164
  demo.launch()
title_gen.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import numpy as np
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ MODEL_DIR = "alakxender/t5-dhivehi-title-generation-xs"
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
8
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
9
+
10
+ prefix = "2title: "
11
+
12
+ max_input_length = 512
13
+ max_target_length = 32
14
+
15
+ def generate_title(content, seed, use_sampling):
16
+ random.seed(seed)
17
+ np.random.seed(seed)
18
+ torch.manual_seed(seed)
19
+ if torch.cuda.is_available():
20
+ torch.cuda.manual_seed_all(seed)
21
+
22
+ input_text = prefix + content.strip()
23
+ inputs = tokenizer(
24
+ input_text,
25
+ max_length=max_input_length,
26
+ truncation=True,
27
+ return_tensors="pt"
28
+ )
29
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
+ model.to(device)
31
+ inputs = {k: v.to(device) for k, v in inputs.items()}
32
+
33
+ gen_kwargs = {
34
+ "input_ids": inputs["input_ids"],
35
+ "attention_mask": inputs["attention_mask"],
36
+ "max_length": max_target_length,
37
+ "no_repeat_ngram_size": 2,
38
+ }
39
+
40
+ if use_sampling:
41
+ gen_kwargs.update({
42
+ "do_sample": True,
43
+ "temperature": 1.0,
44
+ "top_p": 0.95,
45
+ "num_return_sequences": 1,
46
+ })
47
+ else:
48
+ gen_kwargs.update({
49
+ "num_beams": 4,
50
+ "do_sample": False,
51
+ "early_stopping": True,
52
+ })
53
+
54
+ with torch.no_grad():
55
+ outputs = model.generate(**gen_kwargs)
56
+ title = tokenizer.decode(outputs[0], skip_special_tokens=True)
57
+ return title