Spaces:
Sleeping
Sleeping
File size: 4,111 Bytes
e44a599 e999e65 e44a599 | 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 | import os
import pandas as pd
from langchain_core.documents import Document
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
import gradio as gr
# 1. Create the dataset file if it doesn't exist
data = {
"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"],
"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"],
"Description": [
"Lara Croft explores ancient tombs, solves challenging puzzles, and fights to survive in a mysterious island full of action and secrets.",
"Nathan Drake hunts for a lost pirate treasure, featuring cinematic action, intense climbing, third-person shooting, and great storytelling.",
"Aloy explores a vibrant open world ruled by robotic creatures, uncovering secrets of a lost technological past with tactical bow combat.",
"Geralt of Rivia, a monster hunter, searches for his adopted daughter in a massive fantasy world filled with deep choices and rich side quests.",
"A challenging dark fantasy action RPG where players explore a vast shattered world, fight brutal bosses, and create their own playstyle.",
"Cloud Strife fights an evil corporation in a cyberpunk city, featuring a mix of real-time action and strategic turn-based command combat.",
"A beautiful, challenging 2D adventure through a ruined kingdom of insects, focusing on exploration, tight combat, and deep lore.",
"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."
]
}
df = pd.DataFrame(data)
df.to_csv("games_dataset.csv", index=False)
# 2. Prepare Documents
documents = []
for index, row in df.iterrows():
content = f"Game Title: {row['Title']}\nGenre: {row['Genre']}\nDescription: {row['Description']}"
doc = Document(page_content=content, metadata={"title": row['Title']})
documents.append(doc)
# 3. Vector Store
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vector_store = Chroma.from_documents(documents=documents, embedding=embeddings)
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
# 4. RAG Chain
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.7)
system_prompt = (
"You are an expert Video Game Recommendation Assistant.\n"
"Use the following pieces of retrieved context to answer the user's question.\n"
"If you don't know the answer or if it's not in the context, suggest a close alternative from the context.\n"
"Keep your response enthusiastic, helpful, and focused on video games.\n\n"
"Context:\n{context}\n\n"
"Question: {input}\n"
"Answer:"
)
prompt = ChatPromptTemplate.from_template(system_prompt)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "input": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# 5. Gradio Interface
def predict(message, history):
bot_response = rag_chain.invoke(message)
return bot_response
with gr.Blocks(theme="soft") as demo:
gr.ChatInterface(
fn=predict,
title="GamerRadar — Custom RAG Assistant",
description="Ask me for game recommendations or similarities based on our specialized bootcamp dataset!",
examples=["I really liked Tomb Raider. What do you suggest?", "Tell me about adventure games."]
)
if __name__ == "__main__":
demo.launch() |