Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,72 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import hf_hub_download
|
3 |
from llama_cpp import Llama
|
4 |
-
import
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=GGUF_FILENAME)
|
13 |
-
print("✅ Download complete:", model_path)
|
14 |
|
15 |
-
#
|
16 |
llm = Llama(
|
17 |
model_path=model_path,
|
18 |
-
n_ctx=
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
)
|
23 |
|
24 |
-
#
|
25 |
-
def generate_review(
|
26 |
-
|
27 |
-
"product_title":
|
28 |
-
"rating": rating,
|
29 |
"price": price,
|
30 |
-
"
|
|
|
31 |
}
|
32 |
|
33 |
prompt = (
|
34 |
"Write a helpful and natural-sounding customer review in JSON format with two fields: "
|
35 |
-
"\"title\" and \"review\" for the
|
36 |
-
"
|
37 |
-
f"{json.dumps(input_data, indent=2)}"
|
38 |
)
|
39 |
|
40 |
-
response = llm(
|
41 |
-
|
42 |
-
stop=["</s>"],
|
43 |
-
max_tokens=512,
|
44 |
-
temperature=0.7,
|
45 |
-
top_p=0.9,
|
46 |
-
)
|
47 |
|
48 |
try:
|
49 |
-
#
|
50 |
-
json_start =
|
51 |
-
|
52 |
-
review_data
|
53 |
-
return review_data.get("title", ""), review_data.get("review", "")
|
54 |
except Exception as e:
|
55 |
-
return "
|
56 |
|
57 |
-
#
|
58 |
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("
|
60 |
|
61 |
with gr.Row():
|
62 |
-
|
63 |
-
rating = gr.Textbox(label="Rating", placeholder="4.6 out of 5 stars")
|
64 |
price = gr.Textbox(label="Price", placeholder="$129.99")
|
|
|
65 |
|
66 |
-
|
67 |
label="About This Item",
|
68 |
-
placeholder="• Breathable mesh back
|
69 |
lines=4
|
70 |
)
|
71 |
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
fn=generate_review,
|
79 |
-
inputs=[product_title, rating, price, about_this_item],
|
80 |
-
outputs=[review_title, review_body]
|
81 |
-
)
|
82 |
|
83 |
-
# --- Launch app ---
|
84 |
demo.launch()
|
|
|
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 |
|
7 |
+
# Hugging Face model repo + filename
|
8 |
+
REPO_ID = "Bton/llama3-product-reviewer"
|
9 |
+
FILENAME = "llama3-pr-Q4_K_M.gguf"
|
10 |
|
11 |
+
# Download model from Hugging Face Hub (if not cached)
|
12 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, local_dir=".")
|
|
|
|
|
13 |
|
14 |
+
# Load model with llama-cpp-python
|
15 |
llm = Llama(
|
16 |
model_path=model_path,
|
17 |
+
n_ctx=1024,
|
18 |
+
n_batch=8,
|
19 |
+
n_threads=os.cpu_count(),
|
20 |
+
use_mlock=False,
|
21 |
+
verbose=False
|
22 |
)
|
23 |
|
24 |
+
# Prompt wrapper (finetuned prompt must remain unchanged!)
|
25 |
+
def generate_review(title, price, rating, about):
|
26 |
+
product_data = {
|
27 |
+
"product_title": title,
|
|
|
28 |
"price": price,
|
29 |
+
"rating": rating,
|
30 |
+
"about_this_item": about
|
31 |
}
|
32 |
|
33 |
prompt = (
|
34 |
"Write a helpful and natural-sounding customer review in JSON format with two fields: "
|
35 |
+
"\"title\" and \"review\" for the product below.\n\n"
|
36 |
+
f"{json.dumps(product_data, ensure_ascii=False)}"
|
|
|
37 |
)
|
38 |
|
39 |
+
response = llm(prompt, max_tokens=512)
|
40 |
+
raw = response["choices"][0]["text"]
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
try:
|
43 |
+
# Find and parse JSON
|
44 |
+
json_start = raw.find("{")
|
45 |
+
review_data = json.loads(raw[json_start:])
|
46 |
+
return review_data.get("title", "Untitled"), review_data.get("review", raw.strip())
|
|
|
47 |
except Exception as e:
|
48 |
+
return "Error", raw.strip()
|
49 |
|
50 |
+
# Gradio interface
|
51 |
with gr.Blocks() as demo:
|
52 |
+
gr.Markdown("## 📝 LLaMA3 Product Review Generator (CPU Only - ZeroGPU Space)")
|
53 |
|
54 |
with gr.Row():
|
55 |
+
title = gr.Textbox(label="Product Title", placeholder="Ergonomic Office Chair")
|
|
|
56 |
price = gr.Textbox(label="Price", placeholder="$129.99")
|
57 |
+
rating = gr.Textbox(label="Rating", placeholder="4.6 out of 5 stars")
|
58 |
|
59 |
+
about = gr.Textbox(
|
60 |
label="About This Item",
|
61 |
+
placeholder="• Breathable mesh back\n• Adjustable lumbar support\n• Height-adjustable armrests",
|
62 |
lines=4
|
63 |
)
|
64 |
|
65 |
+
btn = gr.Button("Generate Review")
|
66 |
|
67 |
+
out_title = gr.Textbox(label="Generated Title")
|
68 |
+
out_review = gr.Textbox(label="Generated Review", lines=5)
|
69 |
|
70 |
+
btn.click(generate_review, inputs=[title, price, rating, about], outputs=[out_title, out_review])
|
|
|
|
|
|
|
|
|
71 |
|
|
|
72 |
demo.launch()
|