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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from tensorflow.keras import layers, Model
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from rembg import remove
|
6 |
+
import numpy as np
|
7 |
+
import warnings
|
8 |
+
warnings.filterwarnings("ignore")
|
9 |
+
from PIL import Image
|
10 |
+
from tensorflow.keras.utils import get_custom_objects
|
11 |
+
import os
|
12 |
+
from keras import backend as K
|
13 |
+
from keras.saving import register_keras_serializable
|
14 |
+
from deskew import determine_skew
|
15 |
+
import cv2
|
16 |
+
from googletrans import Translator
|
17 |
+
import torch
|
18 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
19 |
+
from transformers import LlamaForCausalLM, PreTrainedTokenizerFast, pipeline
|
20 |
+
from datasets import load_dataset
|
21 |
+
from peft import LoraConfig
|
22 |
+
from trl import SFTTrainer
|
23 |
+
from transformers import pipeline
|
24 |
+
from transformers import (
|
25 |
+
AutoModelForCausalLM,
|
26 |
+
AutoTokenizer,
|
27 |
+
TrainingArguments,
|
28 |
+
pipeline,
|
29 |
+
logging,
|
30 |
+
)
|
31 |
+
|
32 |
+
promt = None
|
33 |
+
|
34 |
+
i_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to('cpu')
|
35 |
+
torch.cuda.empty_cache()
|
36 |
+
|
37 |
+
|
38 |
+
base_model = "meta-llama/Llama-3.2-1B" # https://huggingface.co/meta-llama/Llama-3.2-1B
|
39 |
+
|
40 |
+
hf_dataset = "ahmeterdempmk/Llama-E-Commerce-Fine-Tune-Data" # https://huggingface.co/ahmeterdempmk/Llama-E-Commerce-Fine-Tune-Data
|
41 |
+
|
42 |
+
dataset = load_dataset(hf_dataset, split="train")
|
43 |
+
model = AutoModelForCausalLM.from_pretrained (
|
44 |
+
base_model,
|
45 |
+
device_map={"": 0}
|
46 |
+
)
|
47 |
+
model.config.use_cache = False
|
48 |
+
model.config.pretraining_tp = 1
|
49 |
+
model.low_cpu_mem_usage=True
|
50 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
|
51 |
+
tokenizer.pad_token = tokenizer.eos_token
|
52 |
+
tokenizer.padding_side = "right"
|
53 |
+
peft_params = LoraConfig (
|
54 |
+
lora_alpha=16, # the scaling factor for the low-rank matrices
|
55 |
+
lora_dropout=0.1, # the dropout probability of the LoRA layers
|
56 |
+
r=64, # the dimension of the low-rank matrices
|
57 |
+
bias="none",
|
58 |
+
task_type="CAUSAL_LM", # the task to train for (sequence-to-sequence language modeling in this case)
|
59 |
+
)
|
60 |
+
training_params = TrainingArguments (
|
61 |
+
output_dir="./LlamaResults",
|
62 |
+
num_train_epochs=5, # One training epoch.
|
63 |
+
per_device_train_batch_size=4, # Batch size per GPU for training.
|
64 |
+
gradient_accumulation_steps=1, # This refers to the number of steps required to accumulate the gradients during the update process.
|
65 |
+
optim="paged_adamw_32bit", # Model optimizer (AdamW optimizer).
|
66 |
+
save_steps=25,
|
67 |
+
logging_steps=25,
|
68 |
+
learning_rate=2e-4, # Initial learning rate. (Llama 3.1 8B ile hesaplandı)
|
69 |
+
weight_decay=0.001, # Weight decay is applied to all layers except bias/LayerNorm weights.
|
70 |
+
fp16=False, # Disable fp16/bf16 training.
|
71 |
+
bf16=False, # Disable fp16/bf16 training.
|
72 |
+
max_grad_norm=0.3, # Gradient clipping.
|
73 |
+
max_steps=-1,
|
74 |
+
warmup_ratio=0.03,
|
75 |
+
group_by_length=True,
|
76 |
+
lr_scheduler_type="constant",
|
77 |
+
report_to="tensorboard"
|
78 |
+
)
|
79 |
+
trainer = SFTTrainer(
|
80 |
+
model=model,
|
81 |
+
train_dataset=dataset,
|
82 |
+
peft_config=peft_params,
|
83 |
+
dataset_text_field="input",
|
84 |
+
max_seq_length=None,
|
85 |
+
tokenizer=tokenizer,
|
86 |
+
args=training_params,
|
87 |
+
packing=False,
|
88 |
+
)
|
89 |
+
train_output = trainer.train()
|
90 |
+
torch.cuda.empty_cache()
|
91 |
+
|
92 |
+
languages = {
|
93 |
+
"Türkçe": "tr",
|
94 |
+
"Azərbaycan dili": "az",
|
95 |
+
"Deutsch": "de",
|
96 |
+
"English": "en",
|
97 |
+
"Français": "fr",
|
98 |
+
"Español": "es",
|
99 |
+
"Italiano": "it",
|
100 |
+
"Nederlands": "nl",
|
101 |
+
"Português": "pt",
|
102 |
+
"Русский": "ru",
|
103 |
+
"中文": "zh",
|
104 |
+
"日本語": "ja",
|
105 |
+
"한국어": "ko",
|
106 |
+
"عربي": "ar",
|
107 |
+
"हिन्दी": "hi",
|
108 |
+
"ภาษาไทย": "th",
|
109 |
+
"Tiếng Việt": "vi",
|
110 |
+
"فارسی": "fa",
|
111 |
+
"Svenska": "sv",
|
112 |
+
"Norsk": "no",
|
113 |
+
"Dansk": "da",
|
114 |
+
"Čeština": "cs",
|
115 |
+
"Ελληνικά": "el",
|
116 |
+
"Bosanski": "bs",
|
117 |
+
"Hrvatski": "hr",
|
118 |
+
"Shqip": "sq",
|
119 |
+
"Slovenčina": "sk",
|
120 |
+
"Slovenščina": "sl",
|
121 |
+
"Türkmençe": "tk",
|
122 |
+
"български" : "bg",
|
123 |
+
"Кыргызча": "ky",
|
124 |
+
"Қазақша": "kk",
|
125 |
+
"Монгол": "mn",
|
126 |
+
"Українська": "uk",
|
127 |
+
"Cymraeg": "cy",
|
128 |
+
"Tatarça": "tt",
|
129 |
+
"Kiswahili": "sw",
|
130 |
+
"Hausa": "ha",
|
131 |
+
"አማርኛ": "am",
|
132 |
+
"Èdè Yorùbá": "yo",
|
133 |
+
"isiZulu": "zu",
|
134 |
+
"chiShona": "sn",
|
135 |
+
"isiXhosa": "xh"
|
136 |
+
}
|
137 |
+
|
138 |
+
|
139 |
+
tr_list = ["Lyra AI E-commerce Hackathon Project", "Select Model Sharpness", "Your Product", "Your Explanation About Your Product", "Generate", "Generated Image"]
|
140 |
+
tr_list_tr = []
|
141 |
+
@register_keras_serializable(package='Custom', name='mse')
|
142 |
+
def custom_mse(y_true, y_pred):
|
143 |
+
return K.mean(K.square(y_true - y_pred))
|
144 |
+
|
145 |
+
class STN(layers.Layer):
|
146 |
+
def __init__(self, **kwargs):
|
147 |
+
super(STN, self).__init__(**kwargs)
|
148 |
+
|
149 |
+
def build(self, input_shape):
|
150 |
+
self.localization = tf.keras.Sequential([
|
151 |
+
layers.Conv2D(16, (7, 7), activation='relu', input_shape=input_shape[1:]),
|
152 |
+
layers.MaxPooling2D(pool_size=(2, 2)),
|
153 |
+
layers.Conv2D(32, (5, 5), activation='relu'),
|
154 |
+
layers.MaxPooling2D(pool_size=(2, 2)),
|
155 |
+
layers.Flatten(),
|
156 |
+
layers.Dense(50, activation='relu'),
|
157 |
+
layers.Dense(6, activation='linear')
|
158 |
+
])
|
159 |
+
|
160 |
+
def call(self, inputs):
|
161 |
+
theta = self.localization(inputs)
|
162 |
+
theta = tf.reshape(theta, [-1, 2, 3])
|
163 |
+
grid = self.get_grid(tf.shape(inputs), theta)
|
164 |
+
return self.sampler(inputs, grid)
|
165 |
+
|
166 |
+
def get_grid(self, input_shape, theta):
|
167 |
+
batch_size, height, width = input_shape[0], input_shape[1], input_shape[2]
|
168 |
+
x_coords = tf.linspace(-1.0, 1.0, width)
|
169 |
+
y_coords = tf.linspace(-1.0, 1.0, height)
|
170 |
+
x_grid, y_grid = tf.meshgrid(x_coords, y_coords)
|
171 |
+
ones = tf.ones_like(x_grid)
|
172 |
+
grid = tf.stack([x_grid, y_grid, ones], axis=-1)
|
173 |
+
grid = tf.reshape(grid, [1, height * width, 3])
|
174 |
+
grid = tf.tile(grid, [batch_size, 1, 1])
|
175 |
+
grid = tf.matmul(grid, tf.transpose(theta, [0, 2, 1]))
|
176 |
+
return grid
|
177 |
+
|
178 |
+
def sampler(self, inputs, grid):
|
179 |
+
shape = tf.shape(inputs)
|
180 |
+
batch_size = shape[0]
|
181 |
+
height = shape[1]
|
182 |
+
width = shape[2]
|
183 |
+
channels = shape[3]
|
184 |
+
resized_inputs = tf.image.resize(inputs, size=(height, width))
|
185 |
+
return resized_inputs
|
186 |
+
|
187 |
+
get_custom_objects().update({'STN': STN})
|
188 |
+
|
189 |
+
###!!!Functions Should Be Here!!!###
|
190 |
+
|
191 |
+
def process_image(input_img):
|
192 |
+
input_img=input_img.resize((224,224))
|
193 |
+
input_img=np.array(input_img)
|
194 |
+
input_img=input_img/255.0
|
195 |
+
input_img=np.expand_dims(input_img,axis=0)
|
196 |
+
return input_img
|
197 |
+
def blur_level(image):
|
198 |
+
if isinstance(image, Image.Image):
|
199 |
+
image = np.array(image)
|
200 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
201 |
+
laplacian = cv2.Laplacian(gray_image, cv2.CV_64F)
|
202 |
+
variance = laplacian.var()
|
203 |
+
return variance
|
204 |
+
|
205 |
+
|
206 |
+
image_model = load_model("autoencoder.h5", custom_objects={'mse': custom_mse})
|
207 |
+
torch.cuda.empty_cache()
|
208 |
+
language = st.selectbox("Select Language", list(languages.keys()))
|
209 |
+
|
210 |
+
if language:
|
211 |
+
translator = Translator()
|
212 |
+
tr_list_tr = [translator.translate(text, dest=languages[language]).text for text in tr_list]
|
213 |
+
|
214 |
+
st.title(tr_list_tr[0])
|
215 |
+
|
216 |
+
threshold = st.slider(tr_list_tr[1], min_value = 50, max_value = 100, value = 75)
|
217 |
+
threshold=threshold*3
|
218 |
+
img = st.camera_input(tr_list_tr[2])
|
219 |
+
text = st.text_input(tr_list_tr[3])
|
220 |
+
if st.button(tr_list[4]):
|
221 |
+
|
222 |
+
if img and text is not None:
|
223 |
+
img=Image.open(img)
|
224 |
+
img1=remove(img)
|
225 |
+
if img1.mode == 'RGBA':
|
226 |
+
img1 = img1.convert('RGB')
|
227 |
+
input_img = process_image(img1)
|
228 |
+
torch.cuda.empty_cache()
|
229 |
+
prediction = image_model.predict(input_img)
|
230 |
+
pred_img = np.clip(prediction[0], 0, 1) * 255
|
231 |
+
pred_img = Image.fromarray(pred_img.astype('uint8'))
|
232 |
+
level = blur_level(pred_img)
|
233 |
+
#st.write(level, threshold)
|
234 |
+
prompt = f"""
|
235 |
+
You are extracting product title and description from given text and rewriting the description and enhancing it when necessary.
|
236 |
+
Always give response in the user's input language.
|
237 |
+
Always answer in the given json format. Do not use any other keywords. Do not make up anything.
|
238 |
+
Explanations should contain at least three sentences each.
|
239 |
+
|
240 |
+
Json Format:
|
241 |
+
{{
|
242 |
+
"title": "<title of the product>",
|
243 |
+
"description": "<description of the product>"
|
244 |
+
}}
|
245 |
+
|
246 |
+
Examples:
|
247 |
+
|
248 |
+
Product Information: Rosehip Marmalade, keep it cold
|
249 |
+
Answer: {{"title": "Rosehip Marmalade", "description": "You should store this delicisious roseship marmelade in cold conditions. You can use it in your breakfasts and meals."}}
|
250 |
+
|
251 |
+
Product Information: Blackberry jam spoils in the heat
|
252 |
+
Answer: {{"title": "Blackberry Jam", "description": "Please store it in cold conditions. Recommended to be consumed at breakfast. Very sweet."}}
|
253 |
+
|
254 |
+
Now answer this:
|
255 |
+
Product Information: {text}"""
|
256 |
+
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=10000)
|
257 |
+
torch.cuda.empty_cache()
|
258 |
+
if level < threshold:
|
259 |
+
if img.mode == 'RGB':
|
260 |
+
img = img.convert('RGB')
|
261 |
+
init_image = img.thumbnail((768, 768))
|
262 |
+
i_prompt = "Remove the background from the image and correct the perspective of the subject to ensure a straight and clear view."
|
263 |
+
images = i_pipe(prompt=i_prompt, image=init_image, strength=0.75, guidance_scale=7.5).images
|
264 |
+
images[0].save("output.png")
|
265 |
+
image = Image.open("./output.png")
|
266 |
+
st.image(image, caption=tr_list_tr[5], use_column_width=True)
|
267 |
+
result = pipe(f"Prompt: {prompt} \n Response:") # result = pipe(f"Prompt: {prompt} \n Response:")
|
268 |
+
generated_text = result[0]['generated_text']
|
269 |
+
st.write(generated_text)
|
270 |
+
|
271 |
+
else:
|
272 |
+
st.image(pred_img, caption=tr_list_tr[2], use_column_width=True)
|
273 |
+
result = pipe(f"Prompt: {prompt} \n Response:") # result = pipe(f"Prompt: {prompt} \n Response:")
|
274 |
+
generated_text = result[0]['generated_text']
|
275 |
+
st.write(generated_text)
|