ash23021 commited on
Commit
c90699f
·
verified ·
1 Parent(s): 1167508

Upload 2 files

Browse files
Files changed (2) hide show
  1. agent.py +90 -0
  2. movie_recommendations_with_names.csv +201 -0
agent.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !pip install --upgrade llama-index llama-index-embeddings-huggingface llama-index-llms-huggingface transformers torch accelerate bitsandbytes
2
+
3
+ from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, Document
4
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
+ from llama_index.llms.huggingface import HuggingFaceLLM
6
+ import torch
7
+ import os
8
+ import pandas as pd
9
+ import gradio as gr
10
+
11
+ Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
12
+
13
+
14
+ Settings.embed_model = HuggingFaceEmbedding(
15
+ model_name="sentence-transformers/all-MiniLM-L6-v2")
16
+ Settings.llm = HuggingFaceLLM(
17
+ model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
18
+ tokenizer_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
19
+ context_window=2048,
20
+ max_new_tokens=256,
21
+ device_map="auto",
22
+ model_kwargs={"torch_dtype": torch.float16, "load_in_4bit": True}
23
+ )
24
+
25
+ csv_file_path = "movie_recommendations_with_names.csv"
26
+ df = None
27
+
28
+ try:
29
+ df = pd.read_csv(csv_file_path)
30
+ except FileNotFoundError:
31
+ print(f"Error: CSV file not found at {csv_file_path}")
32
+ print(f"Current working directory: {os.getcwd()}")
33
+
34
+ except Exception as e:
35
+ print(f"An unexpected error occurred while reading the CSV: {e}")
36
+
37
+ if df is not None:
38
+ movies_data = [
39
+ Document(text=f"MovieID: {row['movie_id']}, Title: {row['title']}, Genre: {row['genre']}, Rating: {row['rating']}",
40
+ metadata={"movie_id": row['movie_id'], "title": row['title'], "genre": row['genre'], "rating": row['rating']})
41
+ for index, row in df.iterrows()
42
+ ]
43
+ index = VectorStoreIndex.from_documents(movies_data)
44
+ query_engine = index.as_query_engine()
45
+
46
+ def recommend_movie(genre):
47
+ if not genre.strip():
48
+ return "! Please enter a movie genre."
49
+
50
+ response = query_engine.query(
51
+ f"List titles and genre of movies with genre {genre}."
52
+ f"provide at least 3 recommendations if avalabile."
53
+ )
54
+
55
+ response_lines= str(response).split("\n")
56
+ filtered = [
57
+ line for line in response_lines
58
+ if line.strip()
59
+ and "Note : The query is not specific" not in line
60
+ ]
61
+
62
+ recommendations = []
63
+ for rec in filtered:
64
+ if "Title:" in rec:
65
+ try:
66
+ title = rec.split("Title:")[1].split(",")[0].strip()
67
+ recommendations.append(f"{title}")
68
+ except:
69
+ recommendations.append(f"{rec.strip()}")
70
+
71
+ recommendations = recommendations[:5]
72
+
73
+ if not recommendations:
74
+ return " Sorry , I couldn't find movies for that genre."
75
+ return "\n".join(recommendations)
76
+ #gradio ui
77
+
78
+ interface = gr.Interface(
79
+ fn=recommend_movie,
80
+ inputs=gr.Textbox(
81
+ label="What type of movie are you in the mood for?",
82
+ placeholder="e.g. Action, Comedy, Drama, Sci-Fi"
83
+ ),
84
+ outputs=gr.Textbox(label="🍿 Movie Recommendations"),
85
+ title="🎥 MovieRecBot",
86
+ description="Movie recommendation system powered by LlamaIndex + TinyLlama (Hugging Face)",
87
+ examples=[["Action"], ["Comedy"], ["Romance"], ["Sci-Fi"]],
88
+ )
89
+ interface.launch(share=True)
90
+
movie_recommendations_with_names.csv ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ movie_id,title,genre,rating
2
+ 101,Fight Club,Horror,1.3
3
+ 102,Gladiator,Action,4.8
4
+ 103,Star Wars: A New Hope,Comedy,3.7
5
+ 104,Star Wars: A New Hope,Drama,4.7
6
+ 105,The Dark Knight,Horror,3
7
+ 106,The Silence of the Lambs,Animation,3.1
8
+ 107,Shutter Island,Thriller,4.3
9
+ 108,Pulp Fiction,Comedy,4.9
10
+ 109,The Shawshank Redemption,Fantasy,4.6
11
+ 110,The Departed,Sci-Fi,2.2
12
+ 111,Inception,Fantasy,4.4
13
+ 112,Pulp Fiction,Romance,2.5
14
+ 113,The Prestige,Horror,4.1
15
+ 114,The Departed,Sci-Fi,2.8
16
+ 115,The Silence of the Lambs,Sci-Fi,1.7
17
+ 116,The Godfather,Romance,2
18
+ 117,The Godfather,Thriller,4.1
19
+ 118,Forrest Gump,Thriller,2.5
20
+ 119,The Silence of the Lambs,Fantasy,3.3
21
+ 120,Inception,Comedy,1.8
22
+ 121,The Lion King,Horror,3
23
+ 122,The Shawshank Redemption,Drama,1.7
24
+ 123,Inception,Sci-Fi,3.9
25
+ 124,The Social Network,Drama,1.2
26
+ 125,Star Wars: A New Hope,Action,3.8
27
+ 126,The Matrix Revolutions,Animation,1.3
28
+ 127,Interstellar,Drama,1.7
29
+ 128,The Matrix Revolutions,Animation,5
30
+ 129,Se7en,Romance,2.6
31
+ 130,The Social Network,Animation,1.6
32
+ 131,Inception,Animation,3.9
33
+ 132,The Matrix,Adventure,4.7
34
+ 133,Forrest Gump,Fantasy,3
35
+ 134,Gladiator,Drama,1.1
36
+ 135,Pulp Fiction,Comedy,2.4
37
+ 136,The Social Network,Romance,2.1
38
+ 137,Forrest Gump,Fantasy,1.9
39
+ 138,Star Wars: A New Hope,Action,4.8
40
+ 139,The Silence of the Lambs,Comedy,4.4
41
+ 140,The Godfather,Fantasy,3.3
42
+ 141,Star Wars: A New Hope,Horror,4
43
+ 142,The Dark Knight,Romance,4.1
44
+ 143,The Silence of the Lambs,Adventure,2.7
45
+ 144,The Matrix,Fantasy,4.3
46
+ 145,The Dark Knight,Comedy,3.3
47
+ 146,The Silence of the Lambs,Comedy,4.5
48
+ 147,Avatar,Action,3.6
49
+ 148,Shutter Island,Drama,3.9
50
+ 149,The Prestige,Comedy,1.5
51
+ 150,The Lord of the Rings: The Fellowship of the Ring,Animation,4.9
52
+ 151,The Lord of the Rings: The Fellowship of the Ring,Adventure,4.9
53
+ 152,The Lord of the Rings: The Fellowship of the Ring,Drama,3.7
54
+ 153,The Silence of the Lambs,Comedy,4.9
55
+ 154,Fight Club,Fantasy,1
56
+ 155,The Social Network,Comedy,4.4
57
+ 156,Forrest Gump,Thriller,4.2
58
+ 157,The Godfather,Action,4.6
59
+ 158,The Prestige,Adventure,3
60
+ 159,The Prestige,Adventure,3.1
61
+ 160,Star Wars: A New Hope,Adventure,5
62
+ 161,Titanic,Drama,3.1
63
+ 162,The Departed,Drama,2
64
+ 163,Avatar,Fantasy,3.9
65
+ 164,The Social Network,Thriller,3.6
66
+ 165,The Lord of the Rings: The Fellowship of the Ring,Romance,2.2
67
+ 166,The Godfather: Part II,Action,3.9
68
+ 167,The Social Network,Comedy,1.1
69
+ 168,Se7en,Adventure,2
70
+ 169,The Social Network,Romance,1.7
71
+ 170,The Empire Strikes Back,Adventure,4.6
72
+ 171,Pulp Fiction,Horror,2.2
73
+ 172,Schindler's List,Thriller,2.6
74
+ 173,The Departed,Drama,3.4
75
+ 174,Fight Club,Comedy,3.3
76
+ 175,The Matrix Revolutions,Thriller,4
77
+ 176,The Silence of the Lambs,Comedy,1.8
78
+ 177,Fight Club,Comedy,1.5
79
+ 178,The Usual Suspects,Animation,1.1
80
+ 179,The Lion King,Sci-Fi,1.2
81
+ 180,The Silence of the Lambs,Action,3.8
82
+ 181,Shutter Island,Comedy,3.9
83
+ 182,The Departed,Fantasy,2.8
84
+ 183,Gladiator,Thriller,1.8
85
+ 184,Inception,Sci-Fi,4.3
86
+ 185,Se7en,Horror,3.8
87
+ 186,The Usual Suspects,Animation,2.6
88
+ 187,Titanic,Sci-Fi,4.1
89
+ 188,The Departed,Action,4.9
90
+ 189,The Godfather,Fantasy,4
91
+ 190,Pulp Fiction,Adventure,1.9
92
+ 191,The Prestige,Horror,4.6
93
+ 192,The Empire Strikes Back,Horror,3.9
94
+ 193,The Departed,Adventure,3.3
95
+ 194,The Departed,Drama,4.7
96
+ 195,The Usual Suspects,Drama,1.3
97
+ 196,The Dark Knight,Adventure,4.5
98
+ 197,Schindler's List,Romance,4
99
+ 198,The Matrix,Animation,2.4
100
+ 199,The Social Network,Sci-Fi,3.6
101
+ 200,The Godfather,Action,1.5
102
+ 201,The Godfather,Romance,4.8
103
+ 202,Interstellar,Animation,3.9
104
+ 203,Star Wars: A New Hope,Thriller,3.3
105
+ 204,The Dark Knight,Romance,4.9
106
+ 205,Inception,Comedy,2.7
107
+ 206,Shutter Island,Thriller,2.8
108
+ 207,Avatar,Drama,3.9
109
+ 208,The Lord of the Rings: The Fellowship of the Ring,Adventure,2
110
+ 209,The Matrix Revolutions,Comedy,3.6
111
+ 210,The Lion King,Horror,4.6
112
+ 211,The Silence of the Lambs,Drama,3.9
113
+ 212,The Prestige,Adventure,4.4
114
+ 213,The Matrix,Comedy,4
115
+ 214,The Shawshank Redemption,Comedy,2.2
116
+ 215,The Matrix,Horror,2.6
117
+ 216,The Prestige,Thriller,2.1
118
+ 217,The Empire Strikes Back,Thriller,3
119
+ 218,The Godfather: Part II,Horror,3
120
+ 219,The Departed,Comedy,4.3
121
+ 220,The Social Network,Adventure,2.6
122
+ 221,Se7en,Drama,4
123
+ 222,Pulp Fiction,Drama,4.8
124
+ 223,Star Wars: A New Hope,Action,3.4
125
+ 224,The Usual Suspects,Animation,4.2
126
+ 225,Avatar,Comedy,3.1
127
+ 226,Schindler's List,Action,2.4
128
+ 227,The Departed,Comedy,1.9
129
+ 228,The Dark Knight,Romance,2.8
130
+ 229,The Social Network,Drama,1.5
131
+ 230,The Matrix Revolutions,Fantasy,3.7
132
+ 231,Fight Club,Action,1.2
133
+ 232,Star Wars: A New Hope,Fantasy,1.7
134
+ 233,The Lion King,Adventure,4.1
135
+ 234,Shutter Island,Fantasy,2.4
136
+ 235,Fight Club,Thriller,2.1
137
+ 236,Gladiator,Adventure,1.2
138
+ 237,Inception,Horror,2.6
139
+ 238,The Usual Suspects,Adventure,4.9
140
+ 239,Inception,Thriller,4.3
141
+ 240,The Godfather,Drama,2.2
142
+ 241,The Godfather: Part II,Drama,4.2
143
+ 242,The Godfather,Comedy,4.5
144
+ 243,Star Wars: A New Hope,Animation,4.5
145
+ 244,Interstellar,Horror,5
146
+ 245,The Dark Knight,Thriller,1.2
147
+ 246,Schindler's List,Comedy,3.2
148
+ 247,The Shawshank Redemption,Fantasy,3.3
149
+ 248,The Godfather: Part II,Drama,1.7
150
+ 249,The Lord of the Rings: The Fellowship of the Ring,Drama,3.8
151
+ 250,Se7en,Action,4.5
152
+ 251,The Social Network,Sci-Fi,1.4
153
+ 252,The Empire Strikes Back,Adventure,1.8
154
+ 253,Forrest Gump,Action,3.1
155
+ 254,The Lion King,Thriller,2.4
156
+ 255,The Godfather,Animation,2.7
157
+ 256,Shutter Island,Fantasy,2.6
158
+ 257,The Social Network,Adventure,4.3
159
+ 258,The Lion King,Sci-Fi,2.9
160
+ 259,The Godfather,Thriller,1.1
161
+ 260,Titanic,Sci-Fi,3.3
162
+ 261,The Dark Knight,Animation,3.9
163
+ 262,The Godfather: Part II,Drama,2.2
164
+ 263,The Empire Strikes Back,Drama,2.4
165
+ 264,Fight Club,Horror,2.4
166
+ 265,The Usual Suspects,Sci-Fi,3.2
167
+ 266,Shutter Island,Romance,1
168
+ 267,The Shawshank Redemption,Thriller,1.3
169
+ 268,Interstellar,Romance,2.3
170
+ 269,The Godfather,Horror,3.9
171
+ 270,Gladiator,Romance,2.6
172
+ 271,Fight Club,Horror,3.9
173
+ 272,Star Wars: A New Hope,Thriller,1.1
174
+ 273,The Social Network,Adventure,1.9
175
+ 274,The Lion King,Romance,1.9
176
+ 275,The Empire Strikes Back,Fantasy,3.1
177
+ 276,The Godfather: Part II,Action,4.6
178
+ 277,Star Wars: A New Hope,Thriller,2.1
179
+ 278,Forrest Gump,Drama,3.9
180
+ 279,The Lion King,Drama,3.2
181
+ 280,The Godfather: Part II,Action,2.4
182
+ 281,The Matrix,Horror,4.1
183
+ 282,The Matrix,Romance,1.9
184
+ 283,Pulp Fiction,Animation,3.8
185
+ 284,Shutter Island,Action,5
186
+ 285,The Usual Suspects,Thriller,4.2
187
+ 286,The Lion King,Thriller,2
188
+ 287,The Usual Suspects,Sci-Fi,1.4
189
+ 288,The Lord of the Rings: The Fellowship of the Ring,Drama,2.9
190
+ 289,The Departed,Thriller,4.9
191
+ 290,The Prestige,Romance,1.5
192
+ 291,The Godfather,Action,4.4
193
+ 292,Fight Club,Comedy,2.1
194
+ 293,Pulp Fiction,Horror,3.1
195
+ 294,The Empire Strikes Back,Action,2.3
196
+ 295,The Lion King,Comedy,3.2
197
+ 296,Se7en,Drama,2
198
+ 297,Shutter Island,Adventure,2.3
199
+ 298,The Lion King,Thriller,4.7
200
+ 299,Fight Club,Fantasy,4.2
201
+ 300,The Lord of the Rings: The Fellowship of the Ring,Fantasy,1.8