NightPrince commited on
Commit
d3fe2ce
·
verified ·
1 Parent(s): 94cc623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -4
app.py CHANGED
@@ -1,13 +1,34 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
3
  from peft import PeftModel
4
 
5
- # Load model + LoRA
6
  base = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
7
  model = PeftModel.from_pretrained(base, "NightPrince/peft-distilbert-sst2")
8
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
9
 
10
  pipe = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
11
 
12
- # Gradio interface
13
- gr.Interface(fn=pipe, inputs="text", outputs="label", title="LoRA Sentiment Tester").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
  from peft import PeftModel
4
 
5
+ # Load base + adapter
6
  base = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
7
  model = PeftModel.from_pretrained(base, "NightPrince/peft-distilbert-sst2")
8
  tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
9
 
10
  pipe = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
11
 
12
+ # Wrapper function to return result in {label: score} format
13
+ def classify(text):
14
+ result = pipe(text)[0]
15
+ return {result['label']: result['score']}
16
+
17
+ # Define example inputs
18
+ examples = [
19
+ ["I love this movie! It's fantastic."],
20
+ ["The product was terrible and broke after one use."],
21
+ ["Honestly, it was okay — not bad, not great."],
22
+ ["What a masterpiece, I was speechless."],
23
+ ["I wouldn't recommend this to anyone."],
24
+ ]
25
+
26
+ # Launch Gradio Interface
27
+ gr.Interface(
28
+ fn=classify,
29
+ inputs=gr.Textbox(placeholder="Enter a sentence...", label="Input Text"),
30
+ outputs=gr.Label(num_top_classes=2, label="Sentiment"),
31
+ examples=examples,
32
+ title="🧠 LoRA Sentiment Classifier",
33
+ description="Fine-tuned DistilBERT using PEFT (LoRA) on SST-2. Try it with your own sentences!"
34
+ ).launch()