Bton commited on
Commit
4c88e80
·
verified ·
1 Parent(s): e81ac42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -1,62 +1,62 @@
1
- from huggingface_hub import hf_hub_download
2
- from llama_cpp import Llama
3
  import gradio as gr
 
 
 
 
 
 
 
 
4
 
5
- # Download the GGUF model from Hugging Face Hub
6
- model_path = hf_hub_download(
7
- repo_id="Bton/llama3-product-reviewer", # ✅ Make sure this matches your repo name
8
- filename="unsloth.Q4_K_M.gguf",
9
  local_dir="."
10
  )
11
 
12
- # Initialize the LLM
13
  llm = Llama(
14
- model_path=model_path,
 
15
  n_ctx=2048,
16
- n_threads=4,
17
- n_gpu_layers=35,
18
- use_mlock=True,
19
- verbose=False
20
  )
21
 
22
- # Prompt Template
23
- TEMPLATE = """<|im_start|>system
24
- You are a helpful product review generator. Given Amazon product details, write a convincing, natural-sounding review in JSON format with "title" and "review" fields.
25
- <|im_end|>
26
- <|im_start|>user
27
- Write a product review based on the following product details.
28
- Your input is:
29
- {input_json}
30
- <|im_end|>
31
- <|im_start|>assistant
32
- """
 
 
 
 
33
 
34
  # Inference function
35
- def generate_review(input_json):
36
- full_prompt = TEMPLATE.format(input_json=input_json.strip())
37
-
38
- output = llm(
39
- full_prompt,
40
- max_tokens=512,
41
- temperature=0.7,
42
- stop=["<|im_end|>", "<|end_of_text|>"]
43
- )
44
-
45
- try:
46
- response = output["choices"][0]["text"].strip()
47
- parsed = eval(response) # only safe here since you control the model format
48
- return parsed.get("title", ""), parsed.get("review", "")
49
- except Exception as e:
50
- return "Error parsing response", str(e)
51
 
52
  # Gradio UI
53
- with gr.Blocks() as demo:
54
- gr.Markdown("## 🛍️ Amazon Product Review Generator (GGUF on Llama.cpp)")
55
- input_json = gr.Textbox(label="Paste JSON product details here", lines=8, placeholder='{"product_title": "...", "price": "...", ... }')
56
- btn = gr.Button("Generate Review")
57
- title_out = gr.Textbox(label="Generated Title")
58
- review_out = gr.Textbox(label="Generated Review", lines=5)
59
-
60
- btn.click(generate_review, inputs=input_json, outputs=[title_out, review_out])
 
 
61
 
62
- demo.launch()
 
 
1
+ import json
 
2
  import gradio as gr
3
+ from llama_cpp import Llama
4
+ from huggingface_hub import hf_hub_download
5
+ import os
6
+ import re
7
+
8
+ # Download your GGUF file from your Hugging Face repo
9
+ MODEL_NAME = "llama3-pr-Q4_K_M.gguf"
10
+ MODEL_REPO = "Bton/llama3-pr-Q4_K_M.gguf"
11
 
12
+ local_model_path = hf_hub_download(
13
+ repo_id=MODEL_REPO,
14
+ filename=MODEL_NAME,
 
15
  local_dir="."
16
  )
17
 
18
+ # Load model with llama-cpp
19
  llm = Llama(
20
+ model_path=local_model_path,
21
+ n_gpu_layers=-1, # GPU offload if available
22
  n_ctx=2048,
 
 
 
 
23
  )
24
 
25
+ # Safe JSON extractor
26
+ def safe_parse_json(text):
27
+ try:
28
+ return json.loads(text.strip())
29
+ except json.JSONDecodeError:
30
+ try:
31
+ match = re.search(r'\{.*\}', text, re.DOTALL)
32
+ if match:
33
+ return json.loads(match.group())
34
+ except:
35
+ pass
36
+ return {
37
+ "title": "Generated Title",
38
+ "review": "Error parsing response"
39
+ }
40
 
41
  # Inference function
42
+ def generate_review(input_data):
43
+ prompt = f"{input_data}"
44
+ response = llm(prompt, max_tokens=512, stop=["<|eot_id|>"], stream=False)
45
+ full_output = response["choices"][0]["text"]
46
+ parsed = safe_parse_json(full_output)
47
+ return parsed["title"], parsed["review"]
 
 
 
 
 
 
 
 
 
 
48
 
49
  # Gradio UI
50
+ demo = gr.Interface(
51
+ fn=generate_review,
52
+ inputs=gr.Textbox(label="Product Info (JSON format)"),
53
+ outputs=[
54
+ gr.Textbox(label="Generated Title"),
55
+ gr.Textbox(label="Generated Review", lines=6)
56
+ ],
57
+ title="🛒 Product Review Generator",
58
+ description="Paste product info as JSON and generate a helpful review using your fine-tuned GGUF model.",
59
+ )
60
 
61
+ if __name__ == "__main__":
62
+ demo.launch()