File size: 3,230 Bytes
c90699f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# !pip install --upgrade llama-index llama-index-embeddings-huggingface llama-index-llms-huggingface transformers torch accelerate bitsandbytes

from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, Document
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLM
import torch
import os
import pandas as pd
import gradio as gr

Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")


Settings.embed_model = HuggingFaceEmbedding(
    model_name="sentence-transformers/all-MiniLM-L6-v2")
Settings.llm = HuggingFaceLLM(
    model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    tokenizer_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    context_window=2048,
    max_new_tokens=256,
    device_map="auto",
    model_kwargs={"torch_dtype": torch.float16, "load_in_4bit": True}
)

csv_file_path = "movie_recommendations_with_names.csv"
df = None

try:
    df = pd.read_csv(csv_file_path)
except FileNotFoundError:
    print(f"Error: CSV file not found at {csv_file_path}")
    print(f"Current working directory: {os.getcwd()}")

except Exception as e:
    print(f"An unexpected error occurred while reading the CSV: {e}")
    
if df is not None:
    movies_data = [
        Document(text=f"MovieID: {row['movie_id']}, Title: {row['title']}, Genre: {row['genre']}, Rating: {row['rating']}",
                 metadata={"movie_id": row['movie_id'], "title": row['title'], "genre": row['genre'], "rating": row['rating']})
        for index, row in df.iterrows()
    ]
index = VectorStoreIndex.from_documents(movies_data)
query_engine = index.as_query_engine()

def recommend_movie(genre):
    if not genre.strip():
        return "! Please enter a movie genre."
    
    response = query_engine.query(
        f"List titles and genre of movies with genre {genre}."
        f"provide at least 3 recommendations if avalabile."
    )
    
    response_lines= str(response).split("\n")
    filtered = [
        line for line in response_lines
        if line.strip()
        and "Note : The query is not specific" not in line
        ]
    
    recommendations = []
    for rec in filtered:
        if "Title:" in rec:
            try:
                title = rec.split("Title:")[1].split(",")[0].strip()
                recommendations.append(f"{title}")
            except:
                recommendations.append(f"{rec.strip()}")
                
    recommendations = recommendations[:5]
    
    if not recommendations:
        return " Sorry , I couldn't find movies for that genre."
    return "\n".join(recommendations)
#gradio ui

interface = gr.Interface(
    fn=recommend_movie,
    inputs=gr.Textbox(
        label="What type of movie are you in the mood for?",
        placeholder="e.g. Action, Comedy, Drama, Sci-Fi"
    ),
    outputs=gr.Textbox(label="🍿 Movie Recommendations"),
    title="🎥 MovieRecBot",
    description="Movie recommendation system powered by LlamaIndex + TinyLlama (Hugging Face)",
    examples=[["Action"], ["Comedy"], ["Romance"], ["Sci-Fi"]],
)
interface.launch(share=True)