Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,70 @@
|
|
| 1 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
-
MODEL_ID = "Amey9766/
|
| 5 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
tok = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
-
MODEL_ID,
|
| 12 |
-
token=HF_TOKEN,
|
| 13 |
-
device_map="auto",
|
| 14 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
-
)
|
| 16 |
-
model.eval()
|
| 17 |
-
return tok, model
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def extract_json(text: str) -> str:
|
| 22 |
m = re.search(r"\{.*\}", text, flags=re.S)
|
| 23 |
return m.group(0) if m else text
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
if not review.strip():
|
| 27 |
return "Please enter a hotel review."
|
| 28 |
|
| 29 |
messages = [
|
| 30 |
-
{"role":"system", "content":"You are a hospitality review triage assistant. Output ONLY valid JSON."},
|
| 31 |
-
{"role":"user", "content": review.strip()}
|
| 32 |
]
|
|
|
|
| 33 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 34 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 35 |
|
| 36 |
with torch.no_grad():
|
| 37 |
-
|
| 38 |
**inputs,
|
| 39 |
max_new_tokens=int(max_new_tokens),
|
| 40 |
-
do_sample=temperature > 0,
|
| 41 |
-
temperature=temperature if temperature > 0 else None,
|
| 42 |
pad_token_id=tokenizer.eos_token_id
|
| 43 |
)
|
| 44 |
|
| 45 |
-
decoded = tokenizer.decode(
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
with gr.Blocks(title="
|
| 49 |
-
gr.Markdown("# 🏨
|
| 50 |
-
gr.Markdown("Paste a
|
| 51 |
|
| 52 |
review = gr.Textbox(label="Hotel Review", lines=5, placeholder="The room was dirty and the AC didn’t work.")
|
| 53 |
with gr.Row():
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
out = gr.Code(label="Output (JSON)", language="json")
|
| 58 |
btn = gr.Button("Generate JSON", variant="primary")
|
| 59 |
-
btn.click(
|
| 60 |
|
| 61 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
MODEL_ID = "Amey9766/llama32B-hospitality-review-triage"
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # optional secret
|
| 10 |
|
| 11 |
+
# ---- Load once at startup (works even on older Gradio) ----
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
MODEL_ID,
|
| 16 |
+
token=HF_TOKEN,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 19 |
+
)
|
| 20 |
+
model.eval()
|
| 21 |
|
| 22 |
def extract_json(text: str) -> str:
|
| 23 |
m = re.search(r"\{.*\}", text, flags=re.S)
|
| 24 |
return m.group(0) if m else text
|
| 25 |
|
| 26 |
+
def triage_review(review, max_new_tokens=256, temperature=0.0):
|
| 27 |
+
if not review or not review.strip():
|
| 28 |
return "Please enter a hotel review."
|
| 29 |
|
| 30 |
messages = [
|
| 31 |
+
{"role": "system", "content": "You are a hospitality review triage assistant. Output ONLY valid JSON."},
|
| 32 |
+
{"role": "user", "content": review.strip()},
|
| 33 |
]
|
| 34 |
+
|
| 35 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 36 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 37 |
|
| 38 |
with torch.no_grad():
|
| 39 |
+
output = model.generate(
|
| 40 |
**inputs,
|
| 41 |
max_new_tokens=int(max_new_tokens),
|
| 42 |
+
do_sample=float(temperature) > 0,
|
| 43 |
+
temperature=float(temperature) if float(temperature) > 0 else None,
|
| 44 |
pad_token_id=tokenizer.eos_token_id
|
| 45 |
)
|
| 46 |
|
| 47 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 48 |
+
json_text = extract_json(decoded)
|
| 49 |
+
|
| 50 |
+
# Try to pretty print JSON
|
| 51 |
+
try:
|
| 52 |
+
obj = json.loads(json_text)
|
| 53 |
+
return json.dumps(obj, indent=2)
|
| 54 |
+
except Exception:
|
| 55 |
+
return json_text
|
| 56 |
|
| 57 |
+
with gr.Blocks(title="Hospitality Review Triage") as demo:
|
| 58 |
+
gr.Markdown("# 🏨 Hospitality Review Triage Demo")
|
| 59 |
+
gr.Markdown("Paste a review and get **JSON-only** triage output (category, severity, department, sentiment).")
|
| 60 |
|
| 61 |
review = gr.Textbox(label="Hotel Review", lines=5, placeholder="The room was dirty and the AC didn’t work.")
|
| 62 |
with gr.Row():
|
| 63 |
+
max_tokens = gr.Slider(64, 512, value=256, step=32, label="Max new tokens")
|
| 64 |
+
temp = gr.Slider(0.0, 1.0, value=0.0, step=0.1, label="Temperature")
|
| 65 |
|
| 66 |
out = gr.Code(label="Output (JSON)", language="json")
|
| 67 |
btn = gr.Button("Generate JSON", variant="primary")
|
| 68 |
+
btn.click(triage_review, inputs=[review, max_tokens, temp], outputs=out)
|
| 69 |
|
| 70 |
demo.launch()
|