AA65327 commited on
Commit
ddd8865
·
verified ·
1 Parent(s): f25cc8a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3
+ from peft import PeftModel
4
+ import torch
5
+
6
+ print("Loading model...")
7
+
8
+ bnb_config = BitsAndBytesConfig(
9
+ load_in_4bit=True,
10
+ bnb_4bit_quant_type="nf4",
11
+ bnb_4bit_compute_dtype=torch.float16,
12
+ bnb_4bit_use_double_quant=True
13
+ )
14
+
15
+ base_model_name = "unsloth/Llama-3.2-1B-Instruct"
16
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
17
+ base_model = AutoModelForCausalLM.from_pretrained(
18
+ base_model_name,
19
+ quantization_config=bnb_config,
20
+ device_map="auto",
21
+ trust_remote_code=True
22
+ )
23
+
24
+ model = PeftModel.from_pretrained(base_model, "AA65327/lora_model")
25
+
26
+ print("Model loaded!")
27
+
28
+ def classify_emotion(text):
29
+ prompt = f"Classify the emotion in this text: {text}\n\nEmotion:"
30
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31
+ outputs = model.generate(**inputs, max_new_tokens=50, temperature=0.3, pad_token_id=tokenizer.eos_token_id)
32
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+ emotion = response.split("Emotion:")[-1].strip().split()[0]
34
+ return emotion
35
+
36
+ demo = gr.Interface(
37
+ fn=classify_emotion,
38
+ inputs=gr.Textbox(label="Enter text to classify", placeholder="I am so happy today!"),
39
+ outputs=gr.Textbox(label="Detected Emotion"),
40
+ title="Emotion Classifier",
41
+ description="Classify emotions in text using a fine-tuned Llama model"
42
+ )
43
+
44
+ demo.launch()