alakxender commited on
Commit
423fa16
·
1 Parent(s): 61d39b8
Files changed (2) hide show
  1. app.py +37 -0
  2. instruct_dv.py +77 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
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
@@ -198,6 +199,42 @@ This is an experimental model trained on a very small dataset of Dhivehi news ar
198
  **Notice:**
199
 
200
  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.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  """)
202
 
203
  # Launch the app
 
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
+ from instruct_dv import generate_response, MODEL_OPTIONS_INSTRUCT
7
 
8
 
9
  # Create Gradio interface using the latest syntax
 
199
  **Notice:**
200
 
201
  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.
202
+ """)
203
+
204
+ with gr.Tab("Instruction Following"):
205
+ gr.Markdown("# <center>Dhivehi Instruction Following</center>")
206
+ gr.Markdown("Enter an instruction and (optionally) input text. The model will generate a response following your instruction. Set the random seed for reproducibility. Enable sampling for creative/random results.")
207
+ with gr.Row():
208
+ instruction = gr.Textbox(lines=2, label="Instruction", rtl=True, elem_classes="textbox1")
209
+ with gr.Row():
210
+ input_text = gr.Textbox(lines=2, label="Input Text (optional)", rtl=True, elem_classes="textbox1")
211
+ with gr.Row():
212
+ model_choice = gr.Dropdown(choices=list(MODEL_OPTIONS_INSTRUCT.keys()), value=list(MODEL_OPTIONS_INSTRUCT.keys())[0], label="Model")
213
+ with gr.Row():
214
+ seed = gr.Slider(0, 10000, value=42, step=1, label="Random Seed")
215
+ use_sampling = gr.Checkbox(label="Use Sampling (Creative/Random)", value=True)
216
+ with gr.Row():
217
+ generated_response = gr.Textbox(label="Model Response", rtl=True, elem_classes="textbox1")
218
+ generate_btn = gr.Button("Generate Response")
219
+ generate_btn.click(
220
+ fn=generate_response,
221
+ inputs=[instruction, input_text, seed, use_sampling, model_choice],
222
+ outputs=generated_response
223
+ )
224
+ gr.Examples(
225
+ examples=[
226
+ ["ދީފައިވާ މައުޟޫޢާ ބެހޭގޮތުން ކުރު ޕެރެގްރާފެއް ލިޔެލާށެވެ.","އިއާދަކުރަނިވި ހަކަތަ ބޭނުންކުރުމުގެ މުހިންމުކަން"],
227
+ ["އާ މޯބައިލް އެޕް ޕްރޮމޯޓް ކުރުމަށް މާކެޓިންގ ސްޓްރެޓެޖީތަކުގެ ލިސްޓެއް އުފެއްދުން.",""],
228
+ ["ދިގުމިނުގައި 10ސެންޓިމީޓަރު އަދި ފުޅާމިނަކީ 5ސެންޓިމީޓަރު ހުންނަ ރެކްޓަންގްލަރެއްގެ ސަރަހައްދު ހިސާބުކުރުން.",""],
229
+ ["ތިރީގައިވާ ބަސްފުޅު ތެދެއް ނުވަތަ ދޮގުގެ ގޮތުގައި ގިންތިކުރުން.","ސުޕްރީމް ކޯޓަކީ އެމެރިކާގެ އެންމެ މަތީ ކޯޓެވެ."],
230
+ ],
231
+ inputs=[instruction, input_text],
232
+ )
233
+ gr.Markdown("""\
234
+ **Notes:**
235
+ - This tab allows you to give instructions to the model, optionally with input text, for general-purpose generation or task following in Dhivehi.
236
+ - Try different seeds or enable sampling for more creative outputs.
237
+ - The model is experimental and may not always follow instructions perfectly.
238
  """)
239
 
240
  # Launch the app
instruct_dv.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import numpy as np
3
+ import torch
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+ import spaces
6
+
7
+
8
+ # Available models
9
+ MODEL_OPTIONS_INSTRUCT = {
10
+ "A2 Model": "alakxender/flan-t5-base-alpaca-dv5",
11
+ "A1 Model": "alakxender/flan-t5-base-alpaca-dv"
12
+ }
13
+
14
+ # Cache for loaded models/tokenizers
15
+ MODEL_CACHE = {}
16
+
17
+ def get_model_and_tokenizer(model_dir):
18
+ if model_dir not in MODEL_CACHE:
19
+ print(f"Loading model: {model_dir}")
20
+ tokenizer = T5Tokenizer.from_pretrained(model_dir)
21
+ model = T5ForConditionalGeneration.from_pretrained(model_dir)
22
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
+ print(f"Moving model to device: {device}")
24
+ model.to(device)
25
+ MODEL_CACHE[model_dir] = (tokenizer, model)
26
+ return MODEL_CACHE[model_dir]
27
+
28
+ max_input_length = 256
29
+ max_output_length = 256
30
+
31
+ @spaces.GPU()
32
+ def generate_response(instruction, input_text, seed, use_sampling, model_choice):
33
+ random.seed(seed)
34
+ np.random.seed(seed)
35
+ torch.manual_seed(seed)
36
+ if torch.cuda.is_available():
37
+ torch.cuda.manual_seed_all(seed)
38
+
39
+ model_dir = MODEL_OPTIONS_INSTRUCT[model_choice]
40
+ tokenizer, model = get_model_and_tokenizer(model_dir)
41
+
42
+ combined_input = f"{instruction.strip()} {input_text.strip()}" if input_text else instruction.strip()
43
+ inputs = tokenizer(
44
+ combined_input,
45
+ return_tensors="pt",
46
+ truncation=True,
47
+ max_length=max_input_length
48
+ )
49
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
50
+ inputs = {k: v.to(device) for k, v in inputs.items()}
51
+
52
+ gen_kwargs = {
53
+ "input_ids": inputs["input_ids"],
54
+ "attention_mask": inputs["attention_mask"],
55
+ "max_length": max_output_length,
56
+ "no_repeat_ngram_size": 3,
57
+ "repetition_penalty": 1.5,
58
+ }
59
+
60
+ if use_sampling:
61
+ gen_kwargs.update({
62
+ "do_sample": True,
63
+ "temperature": 0.1,
64
+ "num_return_sequences": 1,
65
+ "num_beams": 1,
66
+ })
67
+ else:
68
+ gen_kwargs.update({
69
+ "num_beams": 8,
70
+ "do_sample": False,
71
+ "early_stopping": True,
72
+ })
73
+
74
+ with torch.no_grad():
75
+ outputs = model.generate(**gen_kwargs)
76
+ decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
77
+ return decoded_output