File size: 6,656 Bytes
26f0f1e
731387e
1302d96
 
1dc3d06
26f0f1e
bc8b53f
731387e
 
1302d96
731387e
 
 
 
 
1302d96
731387e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1302d96
 
731387e
 
 
 
 
 
 
 
 
 
 
 
1302d96
731387e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a3cd4
7cd192c
731387e
7cd192c
 
731387e
b6d73db
7cd192c
 
 
 
731387e
 
 
 
7cd192c
731387e
7cd192c
d4a8d0e
 
245c1b9
 
 
 
 
 
 
 
 
 
 
 
 
aabfecb
 
569f391
aabfecb
 
569f391
c84dc6a
 
 
 
 
aabfecb
4169664
db4cb4b
aabfecb
 
 
 
 
 
245c1b9
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from huggingface_hub import InferenceClient
#step 1 from semantic search
from sentence_transformers import SentenceTransformer
import torch
import gradio as gr
import random
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
#step 2 from semantic search read file
# Open the water_cycle.txt file in read mode with UTF-8 encoding
with open("reconext_file.txt", "r", encoding="utf-8") as file:
  # Read the entire contents of the file and store it in a variable
  reconext_file_text = file.read()
# Print the text below
print(reconext_file_text)
#step 3 from semantix search
def preprocess_text(text):
  # Strip extra whitespace from the beginning and the end of the text
  cleaned_text = text.strip()
  # Split the cleaned_text by every newline character (\n)
  chunks = cleaned_text.split("\n")
  # Create an empty list to store cleaned chunks
  cleaned_chunks = []
  # Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
  for chunk in chunks:
    clean_chunk = chunk.strip()
    if(len(clean_chunk) >= 0):
      cleaned_chunks.append(clean_chunk)
  # Print cleaned_chunks
  print(cleaned_chunks)
  # Print the length of cleaned_chunks
  print(len(cleaned_chunks))
  # Return the cleaned_chunks
  return cleaned_chunks
# Call the preprocess_text function and store the result in a cleaned_chunks variable
cleaned_chunks = preprocess_text(reconext_file_text) # Complete this line
#step 4 from semantic search
# Load the pre-trained embedding model that converts text to vectors
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
  # Convert each text chunk into a vector embedding and store as a tensor
  chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
  # Print the chunk embeddings
  print(chunk_embeddings)
  # Print the shape of chunk_embeddings
  print(chunk_embeddings.shape)
  # Return the chunk_embeddings
  return chunk_embeddings
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
#step 5 from semantic search
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
  # Convert the query text into a vector embedding
  query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
  # Normalize the query embedding to unit length for accurate similarity comparison
  query_embedding_normalized = query_embedding / query_embedding.norm()
  # Normalize all chunk embeddings to unit length for consistent comparison
  chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
  # Calculate cosine similarity between query and all chunks using matrix multiplication
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
  # Print the similarities
  print(similarities)
  # Find the indices of the 3 chunks with highest similarity scores
  top_indices = torch.topk(similarities, k=3).indices
  # Print the top indices
  print(top_indices)
  # Create an empty list to store the most relevant chunks
  top_chunks = []
  # Loop through the top indices and retrieve the corresponding text chunks
  for i in top_indices:
    top_chunks.append(text_chunks[i])
  # Return the list of most relevant chunks
  return top_chunks
def respond(message, history, platform):
    best_next_watch = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
    print(best_next_watch)
    str_watch_chunks = "\n".join(best_next_watch)
    messages = [
        {"role":"system",
        "content": f"You are a Gen Z and Gen Alpha-friendly chatbot that helps teenagers find their next best TV show to watch. Speak naturally and casually, like someone from Gen Z. Only recommend TV shows, never movies. Use only the shows in our database YOU CAN NEVER USE OUTSIDE DATA ONLY TAKE DATA FROM OUR DATABASE! Match show suggestions to the user's age using TV ratings: TV-G is for all ages, TV-PG is for ages 6 and up, TV-14 is for 14 and up, and TV-MA is for 18 and up. If they don’t share their age, assume they’re Gen Z or Gen Alpha and use those guidelines. If the user is not Gen Z or Gen Alpha, you can recommend any show from the database. If they give you a genre, use it to guide your recommendation. If they don’t, pick something fun or relevant. If they mention a show they liked, match the genre of that show to recommend something similar. If nothing matches all their preferences, suggest the most similar show from the database. You got this! Remember you can ONLY take data from {str_watch_chunks}. Only suggest tvshows available on these platforms ({platform}). If there was nothing in those parentheses, you can pull tv shows from any platform. Dont forget to ONLY take data from our data base and remeber to always include the trailors because our data for every tv show has trailers so we expect it!! You got this!."
        }
    ]
    if history:
        messages.extend(history)
    messages.append(
        {'role':'user',
        'content':message}
    )
    response = client.chat_completion(
        messages, max_tokens = 700, temperature=1.3, top_p=0.6
    )
    return response['choices'][0]['message']['content'].strip()

chat_theme = gr.themes.Soft(
    primary_hue="pink",
    secondary_hue="rose",
    neutral_hue="indigo",
    spacing_size="lg",
    radius_size="lg"
).set(
    input_background_fill="*neutral_50",
    input_border_color_focus="*primary_300",
    button_primary_background_fill="*primary_500",
    button_primary_background_fill_hover="*primary_400"
)

with gr.Blocks(theme=chat_theme) as chatbot:
    with gr.Row():
        with gr.Column(scale=3):
            gr.Markdown("""# TV Bot from RecoNext""")
            gr.Markdown("""### Hey! I’m your TV bot \nI help you find your next favorite TV show based on your age and taste. Just tell me what you're into!""")
        with gr.Column(scale=1):
            gr.Image(
        	    value="reconext_logo.png", 
            	show_label=False, 
        	    show_share_button = False, 
        	    show_download_button = False)
    with gr.Row():
        streaming_platforms = gr.CheckboxGroup(['Paramount+', 'HBO Max','Netflix', 'Prime Video', 'Hulu','Disney+'], label ='Only include from: ', info= 'Choose one or more')
    with gr.Row():
        gr.ChatInterface(
            fn = respond,
            additional_inputs = [streaming_platforms],
            type="messages"
    )
 
chatbot.launch()