Bton commited on
Commit
ca3cfbc
·
verified ·
1 Parent(s): 17d8965

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -51
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 json
5
 
6
- # --- Config ---
7
- MODEL_REPO = "Bton/llama3-product-reviewer"
8
- GGUF_FILENAME = "unsloth.Q4_K_M.gguf"
9
 
10
- # --- Download GGUF model from Hugging Face ---
11
- print("📦 Downloading model from Hugging Face Hub...")
12
- model_path = hf_hub_download(repo_id=MODEL_REPO, filename=GGUF_FILENAME)
13
- print("✅ Download complete:", model_path)
14
 
15
- # --- Load LLaMA model ---
16
  llm = Llama(
17
  model_path=model_path,
18
- n_ctx=2048,
19
- n_threads=8, # adjust for Space CPU
20
- n_batch=16,
21
- verbose=True
 
22
  )
23
 
24
- # --- Gradio function ---
25
- def generate_review(product_title, rating, price, about_this_item):
26
- input_data = {
27
- "product_title": product_title,
28
- "rating": rating,
29
  "price": price,
30
- "about_this_item": about_this_item,
 
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 following product.\n\n"
36
- "Product:\n"
37
- f"{json.dumps(input_data, indent=2)}"
38
  )
39
 
40
- response = llm(
41
- prompt,
42
- stop=["</s>"],
43
- max_tokens=512,
44
- temperature=0.7,
45
- top_p=0.9,
46
- )
47
 
48
  try:
49
- # Extract JSON from model response
50
- json_start = response["choices"][0]["text"].find("{")
51
- json_str = response["choices"][0]["text"][json_start:]
52
- review_data = json.loads(json_str)
53
- return review_data.get("title", ""), review_data.get("review", "")
54
  except Exception as e:
55
- return "Review Title Generation Failed", response["choices"][0]["text"]
56
 
57
- # --- Gradio UI ---
58
  with gr.Blocks() as demo:
59
- gr.Markdown("🛍️ **LLaMA3 Product Review Generator**")
60
 
61
  with gr.Row():
62
- product_title = gr.Textbox(label="Product Title", placeholder="Ergonomic Mesh Office Chair")
63
- rating = gr.Textbox(label="Rating", placeholder="4.6 out of 5 stars")
64
  price = gr.Textbox(label="Price", placeholder="$129.99")
 
65
 
66
- about_this_item = gr.Textbox(
67
  label="About This Item",
68
- placeholder="• Breathable mesh back • Adjustable lumbar support Smooth-rolling casters • Height-adjustable armrests",
69
  lines=4
70
  )
71
 
72
- submit_btn = gr.Button("✍️ Generate Review")
73
 
74
- review_title = gr.Textbox(label="Generated Review Title")
75
- review_body = gr.Textbox(label="Generated Review Body", lines=5)
76
 
77
- submit_btn.click(
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()