Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
b4cd3cb
1
Parent(s):
82b0ab8
cg
Browse files- app.py +43 -3
- content_gen.py +57 -0
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
|
3 |
from typo_check import css, process_input,MODEL_OPTIONS_TYPO
|
4 |
from title_gen import generate_title, MODEL_OPTIONS_TITLE
|
|
|
5 |
|
6 |
|
7 |
# Create Gradio interface using the latest syntax
|
@@ -127,11 +128,10 @@ with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
|
|
127 |
|
128 |
"ޔޫރޮޕާ ލީގުން މޮޅުވެގެން އަހަރެމެންގެ މައްސަލަތަކެއް ހައްލެއް ނުވާނެ. މޮޅުވެގެން ލިބޭނީ ޗެމްޕިއަންސް ލީގުގެ ޖާގައަކާއި ހަރަދު ކުރާނެ ފައިސާ. އެކަމަކު އަސްލު މައްސަލަތައް އަދިވެސް ހުރީ. އަހަރެމެންގެ ކޮންސިސްޓެންޓް ވާން ޖެހޭ. ރަނގަޅު ނިންމުންތައް ނިންމައި، ރަނގަޅު ރެކްރޫޓްމަންޓް ސިސްޓަމަކާއި ރަނގަޅު އެކަޑަމީއެއް ބޭނުންވޭ. މިއީ މި ކުލަބްގެ އަސްލު މަގާމަށް ދިޔުމަށްޓަކައި ހައްލު ކުރަން ޖެހޭ ކަންކަން."
|
129 |
|
130 |
-
އިނގިރޭސި ޕްރިމިއާ ލީގުގެ 14 ވަނައިގައި އޮތް މެންޗެސްޓާ ޔުނައިޓެޑަށް، އަންނަ ސީޒަންގައި ޔޫރަޕްގެ މުބާރާތެއް ކުޅެވެން އޮތް ހަމައެކަނި
|
131 |
|
132 |
-
ޔޫރޮޕާ ލީގުގެ ސެމީ ފައިނަލްގައި އެތުލެޓިކް ބިލްބާއޯ އާއި ޔުނައިޓެޑް މިރޭ ވާދަ ކުރާއިރު، ދެ ވަނަ ސެމީގައި
|
133 |
"""],
|
134 |
-
|
135 |
],
|
136 |
inputs=article_content,
|
137 |
)
|
@@ -156,6 +156,46 @@ This is an experimental model trained on a very small dataset of Dhivehi news ar
|
|
156 |
- Due to limited training data, the model may not handle all topics or writing styles equally well.
|
157 |
- Experimental nature means outputs should be reviewed carefully before use.
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
""")
|
160 |
|
161 |
# Launch the app
|
|
|
2 |
|
3 |
from typo_check import css, process_input,MODEL_OPTIONS_TYPO
|
4 |
from title_gen import generate_title, MODEL_OPTIONS_TITLE
|
5 |
+
from content_gen import generate_content, MODEL_OPTIONS_CONTENT, get_default_prompt
|
6 |
|
7 |
|
8 |
# Create Gradio interface using the latest syntax
|
|
|
128 |
|
129 |
"ޔޫރޮޕާ ލީގުން މޮޅުވެގެން އަހަރެމެންގެ މައްސަލަތަކެއް ހައްލެއް ނުވާނެ. މޮޅުވެގެން ލިބޭނީ ޗެމްޕިއަންސް ލީގުގެ ޖާގައަކާއި ހަރަދު ކުރާނެ ފައިސާ. އެކަމަކު އަސްލު މައްސަލަތައް އަދިވެސް ހުރީ. އަހަރެމެންގެ ކޮންސިސްޓެންޓް ވާން ޖެހޭ. ރަނގަޅު ނިންމުންތައް ނިންމައި، ރަނގަޅު ރެކްރޫޓްމަންޓް ސިސްޓަމަކާއި ރަނގަޅު އެކަޑަމީއެއް ބޭނުންވޭ. މިއީ މި ކުލަބްގެ އަސްލު މަގާމަށް ދިޔުމަށްޓަކައި ހައްލު ކުރަން ޖެހޭ ކަންކަން."
|
130 |
|
131 |
+
އިނގިރޭސި ޕްރިމިއާ ލީގުގެ 14 ވަނައިގައި އޮތް މެންޗެސްޓާ ޔުނައިޓެޑަށް، އަންނަ ސީޒަންގައި ޔޫރަޕްގެ މުބާރާތެއް ކުޅެވެން އޮތް ހަމައެކަނި ގޮތަކ ޔޫރޮޕާ ލީގު ކާމިޔާބު ކުރުމެވެ.
|
132 |
|
133 |
+
ޔޫރޮޕާ ލީގުގެ ސެމީ ފައިނަލްގައި އެތުލެޓިކް ބިލްބާއޯ އާއި ޔުނައިޓެޑް މިރޭ ވާދަ ކުރާއިރު، ދެ ވަނަ ސެމީގައި ބައްދަލުކުރާނ ޓޮޓެންހަމް ހޮޓްސްޕާ އާއި ނޯވޭގެ ބޯޑޯ/ގްލިމްޓް އެވެ.
|
134 |
"""],
|
|
|
135 |
],
|
136 |
inputs=article_content,
|
137 |
)
|
|
|
156 |
- Due to limited training data, the model may not handle all topics or writing styles equally well.
|
157 |
- Experimental nature means outputs should be reviewed carefully before use.
|
158 |
|
159 |
+
""")
|
160 |
+
|
161 |
+
with gr.Tab("Content Generation"):
|
162 |
+
gr.Markdown("# <center>Dhivehi Content Generator</center>")
|
163 |
+
gr.Markdown("Generate Dhivehi news articles or content using a fine-tuned T5 model. Select your model and tweak generation parameters.")
|
164 |
+
with gr.Row():
|
165 |
+
model_choice = gr.Dropdown(choices=list(MODEL_OPTIONS_CONTENT.keys()), value=list(MODEL_OPTIONS_CONTENT.keys())[0], label="Model")
|
166 |
+
with gr.Row():
|
167 |
+
prompt_input = gr.Textbox(label="Input Prompt", lines=1, elem_classes="textbox1")
|
168 |
+
def update_prompt(model_choice):
|
169 |
+
return get_default_prompt(model_choice)
|
170 |
+
model_choice.change(fn=update_prompt, inputs=model_choice, outputs=prompt_input)
|
171 |
+
gr.Examples(
|
172 |
+
examples=[
|
173 |
+
["ގުޅިފަޅުގެ ލޭންޑް ޔޫޒް ޕްލޭނަށް ބަދަލު ގެނެސްފައިވާތީ މިއަދު ގުރުއަތުލުން ބާއްވަނީ"],
|
174 |
+
["ދެމަފިރިންގެ ގުޅުންތަކާއި ޒިންމާތައް ބިނާކުރަން ޖެހޭ އަސާސްތައް"],
|
175 |
+
["ކެފެއިންގެ ސަބަބުން އިންސާނާގެ ހަށިގަނޑަށް ކުރާ ޖިސްމާނީ އަދި ނަފްސާނީ އަސަރުތައް ބަޔާންކޮށްދެއްވާށެވެ."],
|
176 |
+
],
|
177 |
+
inputs=prompt_input,
|
178 |
+
)
|
179 |
+
with gr.Row():
|
180 |
+
max_tokens_slider = gr.Slider(10, 300, value=150, label="Max New Tokens")
|
181 |
+
num_beams_slider = gr.Slider(1, 10, value=4, step=1, label="Beam Size (num_beams)")
|
182 |
+
with gr.Row():
|
183 |
+
rep_penalty_slider = gr.Slider(1.0, 1.9, value=1.2, step=0.1, label="Repetition Penalty")
|
184 |
+
ngram_slider = gr.Slider(0, 10, value=3, step=1, label="No Repeat Ngram Size")
|
185 |
+
with gr.Row():
|
186 |
+
do_sample_checkbox = gr.Checkbox(label="Do Sample", value=True)
|
187 |
+
with gr.Row():
|
188 |
+
output_text = gr.Textbox(label="Generated Output", lines=10, elem_classes="textbox1")
|
189 |
+
generate_button = gr.Button("Generate Content")
|
190 |
+
generate_button.click(
|
191 |
+
fn=generate_content,
|
192 |
+
inputs=[prompt_input, max_tokens_slider, num_beams_slider, rep_penalty_slider, ngram_slider, do_sample_checkbox, model_choice],
|
193 |
+
outputs=output_text
|
194 |
+
)
|
195 |
+
gr.Markdown("""
|
196 |
+
**Notice:**
|
197 |
+
|
198 |
+
All outputs generated are synthetic, created using fine-tuned models for experimental and educational evaluation. Accuracy is not guaranteed, and the content should not be considered a source of truth. Please avoid applying these results to production environments, critical systems, or real-world decision-making without proper validation.
|
199 |
""")
|
200 |
|
201 |
# Launch the app
|
content_gen.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Available models for content generation
|
8 |
+
MODEL_OPTIONS_CONTENT = {
|
9 |
+
"MX02 (mixed)": {
|
10 |
+
"model_id": "alakxender/flan-t5-corpora-mixed",
|
11 |
+
"default_prompt": "Tell me about: "
|
12 |
+
},
|
13 |
+
"MX01 (articles)": {
|
14 |
+
"model_id": "alakxender/flan-t5-news-articles",
|
15 |
+
"default_prompt": "Create an article about: "
|
16 |
+
}
|
17 |
+
}
|
18 |
+
|
19 |
+
# Cache for loaded models/tokenizers
|
20 |
+
MODEL_CACHE = {}
|
21 |
+
|
22 |
+
def get_model_and_tokenizer(model_choice):
|
23 |
+
model_dir = MODEL_OPTIONS_CONTENT[model_choice]["model_id"]
|
24 |
+
if model_dir not in MODEL_CACHE:
|
25 |
+
print(f"Loading model: {model_dir}")
|
26 |
+
tokenizer = T5Tokenizer.from_pretrained(model_dir)
|
27 |
+
model = T5ForConditionalGeneration.from_pretrained(model_dir)
|
28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
+
print(f"Moving model to device: {device}")
|
30 |
+
model.to(device)
|
31 |
+
MODEL_CACHE[model_dir] = (tokenizer, model)
|
32 |
+
return MODEL_CACHE[model_dir]
|
33 |
+
|
34 |
+
def get_default_prompt(model_choice):
|
35 |
+
return MODEL_OPTIONS_CONTENT[model_choice]["default_prompt"]
|
36 |
+
|
37 |
+
@spaces.GPU()
|
38 |
+
def generate_content(prompt, max_new_tokens, num_beams, repetition_penalty, no_repeat_ngram_size, do_sample, model_choice):
|
39 |
+
|
40 |
+
tokenizer, model = get_model_and_tokenizer(model_choice)
|
41 |
+
prompt = get_default_prompt(model_choice) + prompt
|
42 |
+
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
44 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
45 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
46 |
+
|
47 |
+
outputs = model.generate(
|
48 |
+
**inputs,
|
49 |
+
max_new_tokens=max_new_tokens,
|
50 |
+
num_beams=num_beams,
|
51 |
+
repetition_penalty=repetition_penalty,
|
52 |
+
no_repeat_ngram_size=no_repeat_ngram_size,
|
53 |
+
do_sample=do_sample,
|
54 |
+
early_stopping=True
|
55 |
+
)
|
56 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
57 |
+
|