|
import streamlit as st
|
|
import faiss
|
|
import numpy as np
|
|
import pandas as pd
|
|
import cohere
|
|
from datetime import datetime
|
|
import os
|
|
from google_play_scraper import app
|
|
from dotenv import load_dotenv
|
|
import firebase_admin
|
|
from firebase_admin import credentials, db
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
cohere_api_key = os.getenv('CO_API_KEY')
|
|
co = cohere.Client(cohere_api_key)
|
|
|
|
|
|
index = faiss.read_index("faiss_index.bin")
|
|
|
|
|
|
|
|
test3 = pd.read_csv("nowgg_embeddings.csv")
|
|
|
|
|
|
|
|
if not firebase_admin._apps:
|
|
cred = credentials.Certificate("firegame.json")
|
|
firebase_admin.initialize_app(cred, {
|
|
'databaseURL': 'https://game-66133-default-rtdb.firebaseio.com/'
|
|
})
|
|
|
|
|
|
|
|
|
|
def get_query_embedding(query):
|
|
response = co.embed(texts=[query])
|
|
return np.array(response.embeddings[0][:250]).astype('float32')
|
|
|
|
|
|
def search_similar(query, k=5):
|
|
query_embedding = get_query_embedding(query).reshape(1, -1)
|
|
distances, indices = index.search(query_embedding, k)
|
|
|
|
results = []
|
|
for idx in indices[0]:
|
|
product_id = test3.iloc[idx]['product_id']
|
|
|
|
app_details = app(product_id)
|
|
result = {
|
|
'title': test3.iloc[idx]['title'],
|
|
'product_id': test3.iloc[idx]['product_id'],
|
|
'description': test3.iloc[idx]['final_description'],
|
|
'link': test3.iloc[idx]['link'],
|
|
|
|
'video':app_details["video"]
|
|
}
|
|
results.append(result)
|
|
return results
|
|
|
|
|
|
def save_feedback_to_firebase(query, feedback):
|
|
ref = db.reference('feedback')
|
|
feedback_data = {
|
|
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
'query': query,
|
|
'feedback': feedback
|
|
}
|
|
ref.push(feedback_data)
|
|
|
|
|
|
|
|
|
|
st.markdown("""
|
|
<style>
|
|
body {
|
|
font-family: 'Arial', sans-serif;
|
|
|
|
}
|
|
.title {
|
|
font-size: 2.5em;
|
|
color: #4CAF50;
|
|
text-align: center;
|
|
margin-bottom: 20px;
|
|
}
|
|
.query-input {
|
|
text-align: center;
|
|
margin-bottom: 20px;
|
|
}
|
|
.result-card {
|
|
background-color: #f9f9f9;
|
|
border-radius: 10px;
|
|
padding: 20px;
|
|
margin-bottom: 20px;
|
|
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
|
}
|
|
.result-title {
|
|
font-size: 1.5em;
|
|
color: #333;
|
|
margin-bottom: 10px;
|
|
}
|
|
.result-productid {
|
|
font-size: 1.0em;
|
|
color: #333;
|
|
margin-bottom: 5px;
|
|
}
|
|
.result-link {
|
|
color: #0066cc;
|
|
text-decoration: none;
|
|
}
|
|
.result-link:hover {
|
|
text-decoration: underline;
|
|
}
|
|
.feedback-section {
|
|
margin-top: 40px;
|
|
text-align: center;
|
|
}
|
|
.feedback-textarea {
|
|
width: 100%;
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
border: 1px solid #ccc;
|
|
margin-bottom: 20px;
|
|
}
|
|
.submit-btn {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
}
|
|
.submit-btn:hover {
|
|
background-color: #45a049;
|
|
}
|
|
</style>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="title">Game Recommendation System</div>', unsafe_allow_html=True)
|
|
|
|
query = st.text_input("Enter your query:", key="query_input", placeholder="Type something...")
|
|
if query:
|
|
top_k_results = search_similar(query)
|
|
st.write('<div class="query-input">Top recommendations:</div>', unsafe_allow_html=True)
|
|
|
|
for result in top_k_results:
|
|
|
|
st.markdown(f"""
|
|
<div class="result-card">
|
|
<div class="result-title">{result['title']}</div>
|
|
<div><a class="result-link" href="{result['link']}">Link</a></div>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
st.video(result['video'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown('<div class="feedback-section">################ Feedback #####################</div>', unsafe_allow_html=True)
|
|
feedback = st.text_area("Please provide your feedback here:", key="feedback_textarea", height=100)
|
|
if st.button("Submit Feedback", key="submit_feedback"):
|
|
save_feedback_to_firebase(query, feedback)
|
|
st.write("Thank you for your feedback!")
|
|
|
|
|