Tesneem commited on
Commit
98b94f2
1 Parent(s): e958180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -12,7 +12,7 @@ sentence_model = SentenceTransformer("all-MiniLM-L6-v2")
12
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
13
  image_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
14
 
15
- def generate_input(input_type, image=None, text=None):
16
  # Initialize the input variable
17
  combined_input = ""
18
 
@@ -38,49 +38,61 @@ def generate_input(input_type, image=None, text=None):
38
  if not combined_input:
39
  combined_input = "No input provided."
40
 
41
- return vector_search(combined_input)
42
 
43
  # Load embeddings and metadata
44
  embeddings = np.load("netflix_embeddings.npy") #created using sentence_transformers on kaggle
45
  metadata = pd.read_csv("netflix_metadata.csv") #created using sentence_transformers on kaggle
46
 
47
  # Vector search function
48
- def vector_search(query):
49
  query_embedding = sentence_model.encode(query)
50
  similarities = cosine_similarity([query_embedding], embeddings)[0]
51
- top_n = 3
52
  top_indices = similarities.argsort()[-top_n:][::-1]
53
  results = metadata.iloc[top_indices]
54
-
55
- # Format results for display
56
- result_text = "\n".join(f"Title: {row['title']}, Description: {row['description']}, Genre: {row['listed_in']}" for _, row in results.iterrows())
 
 
 
57
  return result_text
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# Netflix Recommendation System")
60
  gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")
61
 
62
  input_type = gr.Radio(["Image", "Text", "Both"], label="Select Input Type", type="value")
 
63
  image_input = gr.Image(label="Upload Image", type="pil", visible=False) # Hidden initially
64
  text_input = gr.Textbox(label="Enter Text Query", placeholder="Enter a description or query here", visible=False) # Hidden initially
65
-
66
- # Based on the selected input type, make the appropriate input visible
67
- def update_inputs(input_type):
68
- if input_type == "Image":
69
- return gr.update(visible=True), gr.update(visible=False)
70
- elif input_type == "Text":
71
- return gr.update(visible=False), gr.update(visible=True)
72
- elif input_type == "Both":
73
- return gr.update(visible=True), gr.update(visible=True)
74
-
75
- input_type.change(fn=update_inputs, inputs=input_type, outputs=[image_input, text_input])
76
 
77
  submit_button = gr.Button("Submit")
78
  output = gr.Textbox(label="Recommendations")
79
 
80
- submit_button.click(fn=generate_input, inputs=[input_type,image_input, text_input], outputs=output)
81
-
82
  demo.launch()
83
 
 
84
  # with gr.Blocks() as demo:
85
  # gr.Markdown("# Netflix Recommendation System")
86
  # gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")
 
12
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
13
  image_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
14
 
15
+ def generate_input(input_type, image=None, text=None, response_amount=3):
16
  # Initialize the input variable
17
  combined_input = ""
18
 
 
38
  if not combined_input:
39
  combined_input = "No input provided."
40
 
41
+ return vector_search(combined_input,response_amount)
42
 
43
  # Load embeddings and metadata
44
  embeddings = np.load("netflix_embeddings.npy") #created using sentence_transformers on kaggle
45
  metadata = pd.read_csv("netflix_metadata.csv") #created using sentence_transformers on kaggle
46
 
47
  # Vector search function
48
+ def vector_search(query,top_n=3):
49
  query_embedding = sentence_model.encode(query)
50
  similarities = cosine_similarity([query_embedding], embeddings)[0]
 
51
  top_indices = similarities.argsort()[-top_n:][::-1]
52
  results = metadata.iloc[top_indices]
53
+ result_text=""
54
+ for index,row in results.iterrows():
55
+ if index!=top_n-1:
56
+ result_text+=f"Title: {row['title']} Description: {row['description']} Genre: {row['listed_in']}\n\n"
57
+ else:
58
+ result_text+=f"Title: {row['title']} Description: {row['description']} Genre: {row['listed_in']}"
59
  return result_text
60
+
61
+
62
+ def set_response_amount(response_amount):
63
+ return response_amount
64
+
65
+ # Based on the selected input type, make the appropriate input visible
66
+ def update_inputs(input_type):
67
+ if input_type == "Image":
68
+ return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
69
+ elif input_type == "Text":
70
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
71
+ elif input_type == "Both":
72
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# Netflix Recommendation System")
75
  gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")
76
 
77
  input_type = gr.Radio(["Image", "Text", "Both"], label="Select Input Type", type="value")
78
+ response_type=gr.Dropdown(choices=[3,5,10,25], type="value", label="Select Response Amount", visible=False)
79
  image_input = gr.Image(label="Upload Image", type="pil", visible=False) # Hidden initially
80
  text_input = gr.Textbox(label="Enter Text Query", placeholder="Enter a description or query here", visible=False) # Hidden initially
81
+
82
+ input_type.change(fn=update_inputs, inputs=input_type, outputs=[image_input, text_input, response_type])
83
+ # State variable to store the selected response amount
84
+ selected_response_amount = gr.State()
85
+
86
+ # Capture response amount immediately when dropdown changes
87
+ response_type.change(fn=set_response_amount, inputs=response_type, outputs=selected_response_amount)
 
 
 
 
88
 
89
  submit_button = gr.Button("Submit")
90
  output = gr.Textbox(label="Recommendations")
91
 
92
+ submit_button.click(fn=generate_input, inputs=[input_type,image_input, text_input,selected_response_amount], outputs=output)
 
93
  demo.launch()
94
 
95
+
96
  # with gr.Blocks() as demo:
97
  # gr.Markdown("# Netflix Recommendation System")
98
  # gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")