Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,23 @@
|
|
| 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")
|
| 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 |
-
|
| 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()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
|
| 5 |
|
| 6 |
MODEL_ID = "Amey9766/llama32B-hospitality-review-triage"
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 10 |
|
| 11 |
+
# Load config and REMOVE any quantization config
|
| 12 |
+
config = AutoConfig.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 13 |
+
if hasattr(config, "quantization_config"):
|
| 14 |
+
config.quantization_config = None
|
| 15 |
+
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
MODEL_ID,
|
| 18 |
token=HF_TOKEN,
|
| 19 |
+
config=config, # <-- important
|
| 20 |
device_map="auto",
|
| 21 |
+
dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 22 |
)
|
| 23 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|