Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,62 @@
|
|
1 |
-
|
2 |
-
from llama_cpp import Llama
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
filename="unsloth.Q4_K_M.gguf",
|
9 |
local_dir="."
|
10 |
)
|
11 |
|
12 |
-
#
|
13 |
llm = Llama(
|
14 |
-
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 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Inference function
|
35 |
-
def generate_review(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
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 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
|
|
|
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()
|