Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadjusting strat
app.py
CHANGED
@@ -1,74 +1,58 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
import gradio as gr
|
4 |
from llama_cpp import Llama
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
-
import spaces
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# ✅ Download model once (cached)
|
13 |
-
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, local_dir=".")
|
14 |
-
|
15 |
-
# ✅ GPU-accelerated review generation
|
16 |
-
@spaces.GPU
|
17 |
-
def generate_review(title, price, rating, about):
|
18 |
-
# 🧠 Load model on GPU only when function is called
|
19 |
-
llm = Llama(
|
20 |
-
model_path=model_path,
|
21 |
-
n_ctx=1024,
|
22 |
-
n_batch=64,
|
23 |
-
n_gpu_layers=-1, # Offload all to GPU
|
24 |
-
use_mlock=False,
|
25 |
-
verbose=False
|
26 |
-
)
|
27 |
-
|
28 |
-
product_data = {
|
29 |
-
"product_title": title,
|
30 |
-
"price": price,
|
31 |
-
"rating": rating,
|
32 |
-
"about_this_item": about
|
33 |
-
}
|
34 |
-
|
35 |
-
# ⚠️ DO NOT MODIFY PROMPT FORMAT – it's finetuned
|
36 |
-
prompt = (
|
37 |
-
"Write a helpful and natural-sounding customer review in JSON format with two fields: "
|
38 |
-
"\"title\" and \"review\" for the product below.\n\n"
|
39 |
-
f"{json.dumps(product_data, ensure_ascii=False)}"
|
40 |
-
)
|
41 |
-
|
42 |
-
response = llm(prompt, max_tokens=512)
|
43 |
-
raw = response["choices"][0]["text"]
|
44 |
-
|
45 |
-
try:
|
46 |
-
json_start = raw.find("{")
|
47 |
-
review_data = json.loads(raw[json_start:])
|
48 |
-
return review_data.get("title", "Untitled"), review_data.get("review", raw.strip())
|
49 |
-
except Exception:
|
50 |
-
return "Error", raw.strip()
|
51 |
-
|
52 |
-
# 🖥️ Gradio Interface
|
53 |
with gr.Blocks() as demo:
|
54 |
-
gr.Markdown("##
|
55 |
-
|
56 |
-
|
57 |
-
title = gr.Textbox(label="Product Title", placeholder="Ergonomic Office Chair")
|
58 |
-
price = gr.Textbox(label="Price", placeholder="$129.99")
|
59 |
-
rating = gr.Textbox(label="Rating", placeholder="4.6 out of 5 stars")
|
60 |
-
|
61 |
-
about = gr.Textbox(
|
62 |
-
label="About This Item",
|
63 |
-
placeholder="• Breathable mesh back\n• Adjustable lumbar support\n• Height-adjustable armrests",
|
64 |
-
lines=4
|
65 |
-
)
|
66 |
-
|
67 |
-
btn = gr.Button("Generate Review")
|
68 |
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
|
72 |
-
|
|
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
from huggingface_hub import hf_hub_download
|
|
|
4 |
|
5 |
+
# Download your GGUF model
|
6 |
+
model_path = hf_hub_download(
|
7 |
+
repo_id="Bton/llama3-product-reviewer",
|
8 |
+
filename="unsloth.Q4_K_M.gguf",
|
9 |
+
local_dir="."
|
10 |
+
)
|
11 |
+
|
12 |
+
# Load model with chatml formatting
|
13 |
+
llm = Llama(
|
14 |
+
model_path=model_path,
|
15 |
+
chat_format="chatml",
|
16 |
+
n_ctx=4096,
|
17 |
+
n_threads=4,
|
18 |
+
n_gpu_layers=-1,
|
19 |
+
use_mlock=False,
|
20 |
+
verbose=False
|
21 |
+
)
|
22 |
+
|
23 |
+
def generate_response(message, history, system_message, max_tokens, temperature, top_p):
|
24 |
+
messages = []
|
25 |
+
if system_message.strip():
|
26 |
+
messages.append({"role": "system", "content": system_message})
|
27 |
+
if history:
|
28 |
+
messages += history
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
response_text = ""
|
32 |
+
for chunk in llm.create_chat_completion(
|
33 |
+
messages=messages,
|
34 |
+
stream=True,
|
35 |
+
max_tokens=max_tokens,
|
36 |
+
temperature=temperature,
|
37 |
+
top_p=top_p,
|
38 |
+
):
|
39 |
+
if "content" in chunk["choices"][0]["delta"]:
|
40 |
+
response_text += chunk["choices"][0]["delta"]["content"]
|
41 |
+
yield history + [{"role": "user", "content": message}, {"role": "assistant", "content": response_text}], ""
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("## Bton/llama3-product-reviewer")
|
45 |
+
chatbot = gr.Chatbot(label="Chat")
|
46 |
+
msg = gr.Textbox(placeholder="Type your message...", label="Message")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
with gr.Accordion("⚙️ Advanced", open=False):
|
49 |
+
system_msg = gr.Textbox(value="You are a helpful assistant.", label="System Message")
|
50 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, label="Max tokens")
|
51 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, label="Temperature")
|
52 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p")
|
53 |
|
54 |
+
def chat_submit(message, chat_history, system_message, max_tokens, temperature, top_p):
|
55 |
+
yield from generate_response(message, chat_history, system_message, max_tokens, temperature, top_p)
|
56 |
|
57 |
+
msg.submit(chat_submit, [msg, chatbot, system_msg, max_tokens, temperature, top_p], [chatbot, msg])
|
58 |
+
demo.launch()
|