Update app.py
Browse files
app.py
CHANGED
|
@@ -102,31 +102,31 @@ def main(urls):
|
|
| 102 |
def classify_website(url):
|
| 103 |
global model, tokenizer # Declare model and tokenizer as global variables
|
| 104 |
|
| 105 |
-
try:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
### Instruction:
|
| 132 |
Categorize the website into one of the 3 categories:
|
|
@@ -140,23 +140,23 @@ Categorize the website into one of the 3 categories:
|
|
| 140 |
|
| 141 |
### Response:"""
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
except Exception as e:
|
| 158 |
-
|
| 159 |
-
|
| 160 |
|
| 161 |
# Create a Gradio interface
|
| 162 |
iface = gr.Interface(
|
|
|
|
| 102 |
def classify_website(url):
|
| 103 |
global model, tokenizer # Declare model and tokenizer as global variables
|
| 104 |
|
| 105 |
+
# try:
|
| 106 |
+
# Load the model and tokenizer if they are not already loaded
|
| 107 |
+
if model is None or tokenizer is None:
|
| 108 |
+
from unsloth import FastLanguageModel
|
| 109 |
+
|
| 110 |
+
# Load the model and tokenizer
|
| 111 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 112 |
+
model_name=peft_model_name, # Model used for training
|
| 113 |
+
max_seq_length=max_seq_length,
|
| 114 |
+
dtype=dtype,
|
| 115 |
+
load_in_4bit=load_in_4bit,
|
| 116 |
+
)
|
| 117 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
urls = [url]
|
| 121 |
+
results_shop = main(urls)
|
| 122 |
+
|
| 123 |
+
# Convert results to DataFrame
|
| 124 |
+
df_result_train_more = pd.DataFrame(results_shop)
|
| 125 |
+
text = df_result_train_more['text'][0]
|
| 126 |
+
translated = GoogleTranslator(source='auto', target='en').translate(text[:4990])
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 130 |
|
| 131 |
### Instruction:
|
| 132 |
Categorize the website into one of the 3 categories:
|
|
|
|
| 140 |
|
| 141 |
### Response:"""
|
| 142 |
|
| 143 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 144 |
+
outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
| 145 |
+
ans = tokenizer.batch_decode(outputs)[0]
|
| 146 |
+
ans_pred = ans.split('### Response:')[1].split('<')[0]
|
| 147 |
|
| 148 |
+
if 'OTHER' in ans_pred:
|
| 149 |
+
ans_pred = 'OTHER'
|
| 150 |
+
elif 'NEWS/BLOG' in ans_pred:
|
| 151 |
+
ans_pred = 'NEWS/BLOG'
|
| 152 |
+
elif 'E-commerce' in ans_pred:
|
| 153 |
+
ans_pred = 'E-commerce'
|
| 154 |
|
| 155 |
+
return ans_pred
|
| 156 |
|
| 157 |
+
# except Exception as e:
|
| 158 |
+
# logging.exception(e)
|
| 159 |
+
# return str(e)
|
| 160 |
|
| 161 |
# Create a Gradio interface
|
| 162 |
iface = gr.Interface(
|