dine24 commited on
Commit
07be303
Β·
verified Β·
1 Parent(s): 61e52e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -11,22 +11,21 @@ from datetime import datetime
11
  # Authenticate Hugging Face token securely
12
  login(token=os.getenv("HF_TOKEN"))
13
 
14
- # βœ… Load FAISS index and product text
15
  index = faiss.read_index("deberta_faiss.index")
16
  text_data = pd.read_csv("deberta_text_data.csv")["Retrieved Text"].tolist()
17
 
18
- # βœ… Load DeBERTa model for embeddings
19
  deberta_model_name = "microsoft/deberta-v3-base"
20
  deberta_tokenizer = AutoTokenizer.from_pretrained(deberta_model_name)
21
  deberta_model = AutoModel.from_pretrained(deberta_model_name).to("cpu")
22
 
23
- # βœ… Load Falcon 1B model for generation (fast + public)
24
  llm_model_name = "tiiuae/falcon-rw-1b"
25
  llm_tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
26
  llm_model = AutoModelForCausalLM.from_pretrained(llm_model_name)
27
  llm_pipeline = pipeline("text-generation", model=llm_model, tokenizer=llm_tokenizer, device=-1)
28
 
29
- # βœ… Log file
30
  log_path = "query_logs.txt"
31
 
32
  def generate_embeddings(queries):
@@ -36,18 +35,15 @@ def generate_embeddings(queries):
36
  return embeddings
37
 
38
  def generate_response(user_query):
39
- # πŸ”Ή Log the user query
40
  with open(log_path, "a") as log_file:
41
  log_file.write(f"{datetime.now()} - {user_query}\n")
42
 
43
- # πŸ”Ή Embed and retrieve
44
  query_embedding = generate_embeddings([user_query])
45
  faiss.normalize_L2(query_embedding)
46
  distances, indices = index.search(query_embedding, k=5)
47
  retrieved_docs = [text_data[i] for i in indices[0]]
48
  context = ", ".join(set(retrieved_docs))
49
 
50
- # πŸ”Ή Prompt
51
  prompt = f"""
52
  Using the following product descriptions:
53
  {context}
@@ -65,11 +61,10 @@ def generate_response(user_query):
65
  **Your response:**
66
  """
67
 
68
- # πŸ”Ή Generate response
69
- result = llm_pipeline(prompt, max_length=512, do_sample=True, truncation=True)[0]["generated_text"]
70
  return result
71
 
72
- # βœ… Gradio Blocks UI with logo, branding
73
  with gr.Blocks(css="""
74
  .logo-container {
75
  display: flex;
@@ -77,25 +72,23 @@ with gr.Blocks(css="""
77
  margin-bottom: -40px;
78
  }
79
  .logo-container img {
80
- max-width: 300px;
81
  height: auto;
82
- border-radius: 10px;
83
  }
84
- """) as demo:
 
85
  with gr.Column():
86
  with gr.Row():
87
- gr.HTML("<div class='logo-container'><img src='OnlineRetail.png'></div>")
88
  gr.Markdown("### πŸ›‹οΈ Luxury Decor Assistant")
89
- gr.Markdown("Ask your decor questions based on real product descriptions. Powered by DeBERTa + FAISS + Falcon 1B.")
90
 
91
  with gr.Row():
92
  input_box = gr.Textbox(label="Textbox", placeholder="e.g. Suggest cozy decor for a small bedroom")
93
  output_box = gr.Textbox(label="Response")
94
 
95
  submit_btn = gr.Button("✨ Generate Suggestion")
96
-
97
  submit_btn.click(fn=generate_response, inputs=input_box, outputs=output_box)
98
 
99
-
100
- # βœ… Launch the app
101
- demo.launch()
 
11
  # Authenticate Hugging Face token securely
12
  login(token=os.getenv("HF_TOKEN"))
13
 
14
+ # Load FAISS index and product data
15
  index = faiss.read_index("deberta_faiss.index")
16
  text_data = pd.read_csv("deberta_text_data.csv")["Retrieved Text"].tolist()
17
 
18
+ # Load DeBERTa for embedding
19
  deberta_model_name = "microsoft/deberta-v3-base"
20
  deberta_tokenizer = AutoTokenizer.from_pretrained(deberta_model_name)
21
  deberta_model = AutoModel.from_pretrained(deberta_model_name).to("cpu")
22
 
23
+ # Load Falcon 1B for text generation
24
  llm_model_name = "tiiuae/falcon-rw-1b"
25
  llm_tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
26
  llm_model = AutoModelForCausalLM.from_pretrained(llm_model_name)
27
  llm_pipeline = pipeline("text-generation", model=llm_model, tokenizer=llm_tokenizer, device=-1)
28
 
 
29
  log_path = "query_logs.txt"
30
 
31
  def generate_embeddings(queries):
 
35
  return embeddings
36
 
37
  def generate_response(user_query):
 
38
  with open(log_path, "a") as log_file:
39
  log_file.write(f"{datetime.now()} - {user_query}\n")
40
 
 
41
  query_embedding = generate_embeddings([user_query])
42
  faiss.normalize_L2(query_embedding)
43
  distances, indices = index.search(query_embedding, k=5)
44
  retrieved_docs = [text_data[i] for i in indices[0]]
45
  context = ", ".join(set(retrieved_docs))
46
 
 
47
  prompt = f"""
48
  Using the following product descriptions:
49
  {context}
 
61
  **Your response:**
62
  """
63
 
64
+ result = llm_pipeline(prompt, max_new_tokens=300, do_sample=True, truncation=True, pad_token_id=llm_tokenizer.eos_token_id)[0]["generated_text"]
 
65
  return result
66
 
67
+ # Gradio UI with branding
68
  with gr.Blocks(css="""
69
  .logo-container {
70
  display: flex;
 
72
  margin-bottom: -40px;
73
  }
74
  .logo-container img {
75
+ max-width: 280px;
76
  height: auto;
77
+ border-radius: 8px;
78
  }
79
+ """, theme=gr.themes.Soft()) as demo:
80
+
81
  with gr.Column():
82
  with gr.Row():
83
+ gr.HTML("<div class='logo-container'><img src='OnlineRetail.png' alt='Luxury Logo'></div>")
84
  gr.Markdown("### πŸ›‹οΈ Luxury Decor Assistant")
85
+ gr.Markdown("Ask your decor questions based on real product descriptions. <br> Powered by **DeBERTa + FAISS + Falcon 1B**.")
86
 
87
  with gr.Row():
88
  input_box = gr.Textbox(label="Textbox", placeholder="e.g. Suggest cozy decor for a small bedroom")
89
  output_box = gr.Textbox(label="Response")
90
 
91
  submit_btn = gr.Button("✨ Generate Suggestion")
 
92
  submit_btn.click(fn=generate_response, inputs=input_box, outputs=output_box)
93
 
94
+ demo.launch()