Spaces:
Sleeping
Sleeping
Create app.py
Browse filesAdd Gradio app for intent detection
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
BASE_MODEL = "NousResearch/Llama-2-7b-chat-hf"
|
| 7 |
+
ADAPTER = "Suramya/Llama-2-7b-CloudLex-Intent-Detection" # your HF model repo (adapters)
|
| 8 |
+
|
| 9 |
+
# Load base + adapters
|
| 10 |
+
base_model = AutoModelForSequenceClassification.from_pretrained(
|
| 11 |
+
BASE_MODEL,
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
device_map="auto"
|
| 14 |
+
)
|
| 15 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER)
|
| 16 |
+
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(ADAPTER)
|
| 18 |
+
|
| 19 |
+
clf = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 20 |
+
|
| 21 |
+
def predict_intent(message: str):
|
| 22 |
+
if not message or not message.strip():
|
| 23 |
+
return {"error": "Please enter a message."}
|
| 24 |
+
out = clf(message, top_k=6)
|
| 25 |
+
# return top prediction + scores
|
| 26 |
+
return {item["label"]: float(item["score"]) for item in out}
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=predict_intent,
|
| 30 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a CloudLex-related message..."),
|
| 31 |
+
outputs=gr.Label(num_top_classes=6),
|
| 32 |
+
title="CloudLex Intent Detection (Llama-2 + QLoRA LoRA Adapters)",
|
| 33 |
+
description="Predicts intent: Buying / Support / Careers / Partnership / Explore / Others"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|