Tesneem commited on
Commit
4473ff3
·
verified ·
1 Parent(s): a7542ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -10
app.py CHANGED
@@ -1,6 +1,44 @@
 
 
1
  import numpy as np
2
  import pandas as pd
3
  from sklearn.metrics.pairwise import cosine_similarity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Load embeddings and metadata
6
  embeddings = np.load("netflix_embeddings.npy") #created using sentence_transformers on kaggle
@@ -17,23 +55,28 @@ def vector_search(query, model):
17
  # Format results for display
18
  result_text = "\n".join(f"Title: {row['title']}, Description: {row['description']}, Genre: {row['listed_in']}" for _, row in results.iterrows())
19
  return result_text
20
-
21
- # Gradio Interface
22
- import gradio as gr
23
- from sentence_transformers import SentenceTransformer
24
-
25
- model = SentenceTransformer("all-MiniLM-L6-v2")
26
  with gr.Blocks() as demo:
27
  gr.Markdown("# Netflix Recommendation System")
28
  gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")
29
- query = gr.Textbox(label="Enter your query")
30
- output = gr.Textbox(label="Recommendations")
31
- submit_button = gr.Button("Submit")
32
 
33
- submit_button.click(fn=lambda q: vector_search(q, model), inputs=query, outputs=output)
 
 
 
34
 
35
  demo.launch()
36
 
 
 
 
 
 
 
 
 
37
  # import gradio as gr
38
 
39
  # # def greet(name):
 
1
+ # Gradio Interface
2
+ import gradio as gr
3
  import numpy as np
4
  import pandas as pd
5
  from sklearn.metrics.pairwise import cosine_similarity
6
+ from sentence_transformers import SentenceTransformer
7
+ import requests
8
+ from PIL import Image
9
+ from transformers import BlipProcessor, BlipForConditionalGeneration
10
+ sentence_model = SentenceTransformer("all-MiniLM-L6-v2")
11
+
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(image=None, text=None, model):
16
+ # If an image is provided
17
+ if image:
18
+ inputs = processor(images=image, return_tensors="pt")
19
+ out = image_model.generate(**inputs)
20
+ image_caption = processor.decode(out[0], skip_special_tokens=True)
21
+ else:
22
+ image_caption = None
23
+
24
+ # If text is provided, you can process it accordingly
25
+ if text:
26
+ text_input = text
27
+ else:
28
+ text_input = "No text provided."
29
+
30
+ # Combine image caption and text output
31
+ if image_caption and text_input!="No text provided.":
32
+ # input = f"Image Caption: {image_caption}\nText Query: {text_output}"
33
+ input = image_caption+" "+text_input
34
+ elif image_caption:
35
+ input = image_caption
36
+ elif text:
37
+ input = text_input
38
+ else:
39
+ input = "No input provided."
40
+
41
+ vector_search(input, model)
42
 
43
  # Load embeddings and metadata
44
  embeddings = np.load("netflix_embeddings.npy") #created using sentence_transformers on kaggle
 
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
+ with gr.Row():
62
+ image_input = gr.Image(label="Upload Image", type="pil") # Image input
63
+ text_input = gr.Textbox(label="Enter Text Query", placeholder="Enter a description or query here") # Text input
64
 
65
+ submit_button = gr.Button("Submit")
66
+ output = gr.Textbox(label="Recommendations")
67
+
68
+ submit_button.click(fn=generate_output, inputs=[image_input, text_input, sentence_model], outputs=output)
69
 
70
  demo.launch()
71
 
72
+ # with gr.Blocks() as demo:
73
+ # gr.Markdown("# Netflix Recommendation System")
74
+ # gr.Markdown("Enter a query to receive Netflix show recommendations based on title, description, and genre.")
75
+ # query = gr.Textbox(label="Enter your query")
76
+ # output = gr.Textbox(label="Recommendations")
77
+ # submit_button = gr.Button("Submit")
78
+
79
+ # submit_button.click(fn=lambda q: vector_search(q, model), inputs=query, outputs=output)
80
  # import gradio as gr
81
 
82
  # # def greet(name):