Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,91 @@
|
|
| 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
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print(f"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
f"
|
| 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 |
-
|
|
|
|
|
|
| 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}
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
csv_file_path = "movie_recommendations_with_names.csv"
|
| 27 |
+
df = None
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
df = pd.read_csv(csv_file_path)
|
| 31 |
+
except FileNotFoundError:
|
| 32 |
+
print(f"Error: CSV file not found at {csv_file_path}")
|
| 33 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"An unexpected error occurred while reading the CSV: {e}")
|
| 37 |
+
|
| 38 |
+
if df is not None:
|
| 39 |
+
movies_data = [
|
| 40 |
+
Document(text=f"MovieID: {row['movie_id']}, Title: {row['title']}, Genre: {row['genre']}, Rating: {row['rating']}",
|
| 41 |
+
metadata={"movie_id": row['movie_id'], "title": row['title'], "genre": row['genre'], "rating": row['rating']})
|
| 42 |
+
for index, row in df.iterrows()
|
| 43 |
+
]
|
| 44 |
+
index = VectorStoreIndex.from_documents(movies_data)
|
| 45 |
+
query_engine = index.as_query_engine()
|
| 46 |
+
|
| 47 |
+
def recommend_movie(genre):
|
| 48 |
+
if not genre.strip():
|
| 49 |
+
return "! Please enter a movie genre."
|
| 50 |
+
|
| 51 |
+
response = query_engine.query(
|
| 52 |
+
f"List titles and genre of movies with genre {genre}."
|
| 53 |
+
f"provide at least 3 recommendations if avalabile."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
response_lines= str(response).split("\n")
|
| 57 |
+
filtered = [
|
| 58 |
+
line for line in response_lines
|
| 59 |
+
if line.strip()
|
| 60 |
+
and "Note : The query is not specific" not in line
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
recommendations = []
|
| 64 |
+
for rec in filtered:
|
| 65 |
+
if "Title:" in rec:
|
| 66 |
+
try:
|
| 67 |
+
title = rec.split("Title:")[1].split(",")[0].strip()
|
| 68 |
+
recommendations.append(f"{title}")
|
| 69 |
+
except:
|
| 70 |
+
recommendations.append(f"{rec.strip()}")
|
| 71 |
+
|
| 72 |
+
recommendations = recommendations[:5]
|
| 73 |
+
|
| 74 |
+
if not recommendations:
|
| 75 |
+
return " Sorry , I couldn't find movies for that genre."
|
| 76 |
+
return "\n".join(recommendations)
|
| 77 |
+
#gradio ui
|
| 78 |
+
|
| 79 |
+
interface = gr.Interface(
|
| 80 |
+
fn=recommend_movie,
|
| 81 |
+
inputs=gr.Textbox(
|
| 82 |
+
label="What type of movie are you in the mood for?",
|
| 83 |
+
placeholder="e.g. Action, Comedy, Drama, Sci-Fi"
|
| 84 |
+
),
|
| 85 |
+
outputs=gr.Textbox(label="🍿 Movie Recommendations"),
|
| 86 |
+
title="🎥 MovieRecBot",
|
| 87 |
+
description="Movie recommendation system powered by LlamaIndex + TinyLlama (Hugging Face)",
|
| 88 |
+
examples=[["Action"], ["Comedy"], ["Romance"], ["Sci-Fi"]],
|
| 89 |
+
)
|
| 90 |
+
interface.launch(share=True)
|
| 91 |
+
|