File size: 3,381 Bytes
8664dd7
 
 
 
 
562b88e
3e1e26d
 
e15ce69
3e1e26d
e15ce69
3e1e26d
 
07be303
8664dd7
 
 
07be303
8664dd7
562b88e
8664dd7
 
07be303
f33963b
562b88e
89adc1e
e15ce69
 
 
 
8664dd7
 
 
e15ce69
 
8664dd7
 
e15ce69
 
 
8664dd7
 
 
562b88e
8664dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e15ce69
07be303
e15ce69
 
07be303
ba83d70
43d2a93
 
 
 
a894a91
f2d2b8e
 
a894a91
43d2a93
07be303
43d2a93
07be303
ba83d70
 
f2d2b8e
 
 
 
a894a91
 
f2d2b8e
 
ba83d70
07be303
ba83d70
 
 
 
 
 
 
 
07be303
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
import numpy as np
import pandas as pd
import faiss
import torch
from transformers import AutoTokenizer, AutoModel, pipeline, AutoModelForCausalLM
from huggingface_hub import login
import os
from datetime import datetime

# Authenticate Hugging Face token securely
login(token=os.getenv("HF_TOKEN"))

# Load FAISS index and product data
index = faiss.read_index("deberta_faiss.index")
text_data = pd.read_csv("deberta_text_data.csv")["Retrieved Text"].tolist()

# Load DeBERTa for embedding
deberta_model_name = "microsoft/deberta-v3-base"
deberta_tokenizer = AutoTokenizer.from_pretrained(deberta_model_name)
deberta_model = AutoModel.from_pretrained(deberta_model_name).to("cpu")

# Load Falcon 1B for text generation
llm_model_name = "tiiuae/falcon-rw-1b"
llm_tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
llm_model = AutoModelForCausalLM.from_pretrained(llm_model_name)
llm_pipeline = pipeline("text-generation", model=llm_model, tokenizer=llm_tokenizer, device=-1)

log_path = "query_logs.txt"

def generate_embeddings(queries):
    tokens = deberta_tokenizer(queries, return_tensors="pt", padding=True, truncation=True).to("cpu")
    with torch.no_grad():
        embeddings = deberta_model(**tokens).last_hidden_state.mean(dim=1).cpu().numpy().astype("float32")
    return embeddings

def generate_response(user_query):
    with open(log_path, "a") as log_file:
        log_file.write(f"{datetime.now()} - {user_query}\n")

    query_embedding = generate_embeddings([user_query])
    faiss.normalize_L2(query_embedding)
    distances, indices = index.search(query_embedding, k=5)
    retrieved_docs = [text_data[i] for i in indices[0]]
    context = ", ".join(set(retrieved_docs))

    prompt = f"""
    Using the following product descriptions:
    {context}

    Carefully craft a well-structured response to the following question:

    **Question:** {user_query}

    **Instructions:**
    1. Incorporate **all** retrieved product descriptions.
    2. Use a **formal yet engaging** tone.
    3. Provide **practical & creative** luxury decor ideas.
    4. Ensure a **cohesive & detailed response.**

    **Your response:**
    """

    result = llm_pipeline(prompt, max_new_tokens=300, do_sample=True, truncation=True, pad_token_id=llm_tokenizer.eos_token_id)[0]["generated_text"]
    return result

# Gradio UI with branding
with gr.Blocks(css="""
#logo-img {
    display: block;
    margin-left: auto;
    margin-right: auto;
    margin-top: 5px;
    margin-bottom: 5px;
    border-radius: 12px;
    box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.05);
}
""", theme=gr.themes.Soft()) as demo:

    
    with gr.Column():
        with gr.Row():
            gr.Image(
    value="OnlineRetail.png",
    show_label=False,
    show_download_button=False,
    height=100,
    width=100,
    elem_id="logo-img"
)
        gr.Markdown("### 🛋️ Luxury Decor Assistant")
        gr.Markdown("Ask your decor questions based on real product descriptions. <br> Powered by **DeBERTa + FAISS + Falcon 1B**.")

        with gr.Row():
            input_box = gr.Textbox(label="Textbox", placeholder="e.g. Suggest cozy decor for a small bedroom")
            output_box = gr.Textbox(label="Response")

        submit_btn = gr.Button("✨ Generate Suggestion")
        submit_btn.click(fn=generate_response, inputs=input_box, outputs=output_box)

demo.launch()