Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +77 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "Amey9766/llama32-hotel-review-triage"
|
| 8 |
+
|
| 9 |
+
def load_model():
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
device_map="auto",
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
+
)
|
| 16 |
+
model.eval()
|
| 17 |
+
return tokenizer, model
|
| 18 |
+
|
| 19 |
+
tokenizer, model = load_model()
|
| 20 |
+
|
| 21 |
+
def extract_json(text):
|
| 22 |
+
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 23 |
+
return match.group(0) if match else text
|
| 24 |
+
|
| 25 |
+
def triage_review(review, max_tokens, temperature):
|
| 26 |
+
if not review.strip():
|
| 27 |
+
return "Please enter a hotel review."
|
| 28 |
+
|
| 29 |
+
system_prompt = "You are a hospitality review triage assistant. Output ONLY valid JSON."
|
| 30 |
+
|
| 31 |
+
messages = [
|
| 32 |
+
{"role": "system", "content": system_prompt},
|
| 33 |
+
{"role": "user", "content": review.strip()}
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 37 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 38 |
+
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
output = model.generate(
|
| 41 |
+
**inputs,
|
| 42 |
+
max_new_tokens=max_tokens,
|
| 43 |
+
do_sample=temperature > 0,
|
| 44 |
+
temperature=temperature if temperature > 0 else None,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 49 |
+
return extract_json(decoded)
|
| 50 |
+
|
| 51 |
+
with gr.Blocks(title="Hotel Review Triage") as demo:
|
| 52 |
+
gr.Markdown("## 🏨 Hotel Review Triage Demo")
|
| 53 |
+
gr.Markdown(
|
| 54 |
+
"Paste a hotel review below. The model will return **structured JSON** "
|
| 55 |
+
"for operational triage (department, severity, sentiment, summary)."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
review_input = gr.Textbox(
|
| 59 |
+
label="Hotel Review",
|
| 60 |
+
placeholder="The room was dirty and the AC didn’t work.",
|
| 61 |
+
lines=5
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
max_tokens = gr.Slider(64, 512, value=256, step=32, label="Max new tokens")
|
| 66 |
+
temperature = gr.Slider(0.0, 1.0, value=0.0, step=0.1, label="Temperature")
|
| 67 |
+
|
| 68 |
+
output = gr.Code(label="Model Output (JSON)", language="json")
|
| 69 |
+
|
| 70 |
+
run_btn = gr.Button("Generate JSON", variant="primary")
|
| 71 |
+
run_btn.click(
|
| 72 |
+
fn=triage_review,
|
| 73 |
+
inputs=[review_input, max_tokens, temperature],
|
| 74 |
+
outputs=output
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.41.0
|
| 2 |
+
torch
|
| 3 |
+
accelerate
|
| 4 |
+
sentencepiece
|
| 5 |
+
gradio
|