Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +80 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from langchain_core.documents import Document
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain_chroma import Chroma
|
| 6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 8 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 9 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# 1. Create the dataset file if it doesn't exist
|
| 13 |
+
data = {
|
| 14 |
+
"Title": ["Tomb Raider", "Uncharted 4: A Thief's End", "Horizon Zero Dawn", "The Witcher 3: Wild Hunt", "Elden Ring", "Final Fantasy VII Remake", "Hollow Knight", "Hades"],
|
| 15 |
+
"Genre": ["Action-Adventure, Puzzle", "Action-Adventure, Shooter", "Action-Adventure, RPG, Sci-Fi", "RPG, Open World, Fantasy", "RPG, Dark Fantasy, Souls-like", "RPG, Turn-based, Cyberpunk", "Indie, Metroidvania, Platformer", "Indie, Rogue-like, Action"],
|
| 16 |
+
"Description": [
|
| 17 |
+
"Lara Croft explores ancient tombs, solves challenging puzzles, and fights to survive in a mysterious island full of action and secrets.",
|
| 18 |
+
"Nathan Drake hunts for a lost pirate treasure, featuring cinematic action, intense climbing, third-person shooting, and great storytelling.",
|
| 19 |
+
"Aloy explores a vibrant open world ruled by robotic creatures, uncovering secrets of a lost technological past with tactical bow combat.",
|
| 20 |
+
"Geralt of Rivia, a monster hunter, searches for his adopted daughter in a massive fantasy world filled with deep choices and rich side quests.",
|
| 21 |
+
"A challenging dark fantasy action RPG where players explore a vast shattered world, fight brutal bosses, and create their own playstyle.",
|
| 22 |
+
"Cloud Strife fights an evil corporation in a cyberpunk city, featuring a mix of real-time action and strategic turn-based command combat.",
|
| 23 |
+
"A beautiful, challenging 2D adventure through a ruined kingdom of insects, focusing on exploration, tight combat, and deep lore.",
|
| 24 |
+
"Zagreus, the prince of the Underworld, attempts to escape his father's realm in a fast-paced action game with shifting dungeons and rich story."
|
| 25 |
+
]
|
| 26 |
+
}
|
| 27 |
+
df = pd.DataFrame(data)
|
| 28 |
+
df.to_csv("games_dataset.csv", index=False)
|
| 29 |
+
|
| 30 |
+
# 2. Prepare Documents
|
| 31 |
+
documents = []
|
| 32 |
+
for index, row in df.iterrows():
|
| 33 |
+
content = f"Game Title: {row['Title']}\nGenre: {row['Genre']}\nDescription: {row['Description']}"
|
| 34 |
+
doc = Document(page_content=content, metadata={"title": row['Title']})
|
| 35 |
+
documents.append(doc)
|
| 36 |
+
|
| 37 |
+
# 3. Vector Store
|
| 38 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 39 |
+
vector_store = Chroma.from_documents(documents=documents, embedding=embeddings)
|
| 40 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
| 41 |
+
|
| 42 |
+
# 4. RAG Chain
|
| 43 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.7)
|
| 44 |
+
|
| 45 |
+
system_prompt = (
|
| 46 |
+
"You are an expert Video Game Recommendation Assistant.\n"
|
| 47 |
+
"Use the following pieces of retrieved context to answer the user's question.\n"
|
| 48 |
+
"If you don't know the answer or if it's not in the context, suggest a close alternative from the context.\n"
|
| 49 |
+
"Keep your response enthusiastic, helpful, and focused on video games.\n\n"
|
| 50 |
+
"Context:\n{context}\n\n"
|
| 51 |
+
"Question: {input}\n"
|
| 52 |
+
"Answer:"
|
| 53 |
+
)
|
| 54 |
+
prompt = ChatPromptTemplate.from_template(system_prompt)
|
| 55 |
+
|
| 56 |
+
def format_docs(docs):
|
| 57 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 58 |
+
|
| 59 |
+
rag_chain = (
|
| 60 |
+
{"context": retriever | format_docs, "input": RunnablePassthrough()}
|
| 61 |
+
| prompt
|
| 62 |
+
| llm
|
| 63 |
+
| StrOutputParser()
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# 5. Gradio Interface
|
| 67 |
+
def predict(message, history):
|
| 68 |
+
bot_response = rag_chain.invoke(message)
|
| 69 |
+
return bot_response
|
| 70 |
+
|
| 71 |
+
with gr.Blocks(theme="soft") as demo:
|
| 72 |
+
gr.ChatInterface(
|
| 73 |
+
fn=predict,
|
| 74 |
+
title="🎮 GamerRadar — Custom RAG Assistant",
|
| 75 |
+
description="Ask me for game recommendations or similarities based on our specialized bootcamp dataset!",
|
| 76 |
+
examples=["I really liked Tomb Raider. What do you suggest?", "Tell me about adventure games."]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain-community
|
| 3 |
+
langchain-chroma
|
| 4 |
+
langchain-google-genai
|
| 5 |
+
sentence-transformers
|
| 6 |
+
gradio
|
| 7 |
+
pandas
|
| 8 |
+
pysqlite3-binary
|