Spaces:
Runtime error
Runtime error
Commit
·
fef6972
1
Parent(s):
3e1685d
Add code and requirements
Browse files- app.py +54 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from unsloth import FastLanguageModel
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Configuration
|
6 |
+
max_seq_length = 2048
|
7 |
+
dtype = None # Auto detection of dtype
|
8 |
+
load_in_4bit = True # Use 4-bit quantization to reduce memory usage
|
9 |
+
|
10 |
+
peft_model_name = "limitedonly41/website_mistral7b_v02_1200_finetuned_5_big"
|
11 |
+
|
12 |
+
# Load the model and tokenizer
|
13 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
14 |
+
model_name=peft_model_name, # YOUR MODEL YOU USED FOR TRAINING
|
15 |
+
max_seq_length=max_seq_length,
|
16 |
+
dtype=dtype,
|
17 |
+
load_in_4bit=load_in_4bit,
|
18 |
+
)
|
19 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
20 |
+
|
21 |
+
def return_prediction(prompt):
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
23 |
+
outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
24 |
+
ans = tokenizer.batch_decode(outputs)[0]
|
25 |
+
ans_pred = ans.split('### Response:')[1].split('<')[0]
|
26 |
+
return ans_pred
|
27 |
+
|
28 |
+
def classify_website(site_text):
|
29 |
+
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.
|
30 |
+
|
31 |
+
### Instruction:
|
32 |
+
Categorize the website into one of the 3 categories:
|
33 |
+
|
34 |
+
1) OTHER
|
35 |
+
2) NEWS/BLOG
|
36 |
+
3) E-commerce
|
37 |
+
|
38 |
+
### Input:
|
39 |
+
{site_text}
|
40 |
+
|
41 |
+
### Response:"""
|
42 |
+
return return_prediction(prompt)
|
43 |
+
|
44 |
+
# Create a Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=classify_website,
|
47 |
+
inputs="text",
|
48 |
+
outputs="text",
|
49 |
+
title="Website Categorization",
|
50 |
+
description="Categorize a website into one of the 3 categories: OTHER, NEWS/BLOG, or E-commerce."
|
51 |
+
)
|
52 |
+
|
53 |
+
# Launch the interface
|
54 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
unsloth
|
4 |
+
transformers
|
5 |
+
tqdm
|