Game-Wikipedia / app.py
shilpabanerjee's picture
Upload app.py
f7fed67
import streamlit as st
import pandas as pd
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load the games.csv file into a pandas DataFrame
@st.cache_resource # Caches the loaded data to improve performance
def load_data():
data = pd.read_csv('games.csv')
return data.copy()
games_data = load_data()
# Load the pre-trained GPT-2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')
# Function to handle user questions and provide answers based on the loaded data
def answer_question(question):
question = question.lower()
answer = ""
if "release date" in question:
# Find the release date of a game
game_title = question.split("release date of ")[1].strip()
matching_games = games_data[games_data['Title'].str.lower() == game_title]
if not matching_games.empty:
release_date = matching_games.iloc[0]['Release_Date']
answer = f"The release date of '{game_title}' is {release_date}"
else:
answer = f"Sorry, I couldn't find any information about '{game_title}'"
elif "developer" in question:
# Find the developers of a game
game_title = question.split("developers of ")[1].strip()
matching_games = games_data[games_data['Title'].str.lower() == game_title]
if not matching_games.empty:
developers = matching_games.iloc[0]['Developers']
answer = f"The developers of '{game_title}' are {developers}"
else:
answer = f"Sorry, I couldn't find any information about '{game_title}'"
elif "similar games to" in question:
# Find similar games based on user question
game_title = question.split("similar games to ")[1].strip()
matching_games = games_data[games_data['Title'].str.lower() == game_title]
if not matching_games.empty:
genre = matching_games.iloc[0]['Genres']
similar_games = find_similar_games(game_title, genre)
if similar_games:
answer = f"Here are some similar games to '{game_title}': {', '.join(similar_games)}"
else:
answer = f"Sorry, I couldn't find any similar games to '{game_title}'"
else:
answer = f"Sorry, I couldn't find any information about '{game_title}'"
# Add more question-answer logic here based on the columns in your games.csv file
return answer
# Perform prompt tuning to improve model responses
def perform_prompt_tuning(input_text):
responses = []
for _ in range(3):
inputs = tokenizer.encode(input_text, return_tensors='pt')
prompt_len = inputs.shape[1]
outputs = model.generate(inputs, max_length=200, num_return_sequences=1, no_repeat_ngram_size=2, do_sample=True, top_k=50, top_p=0.95, temperature=0.7)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
responses.append(response)
return responses
# Find similar games based on genre
def find_similar_games(game_title, genre):
# Implement your logic for finding similar games based on genre
# Return a list of similar game titles
pass
# Define the main function that will run the Streamlit app
def main():
st.title("Game Wikipedia")
# Create a text input field for user queries
user_question = st.text_input("Ask a question")
# Display example questions for the user to copy-paste
st.write("Example Questions:")
st.write("1. release date of Hades")
st.write("2. developers of God of War")
st.write("3. Summary for Hollow Knight")
# When the user submits a question, get the answer and display it
if st.button("Submit"):
# Perform prompt tuning to get model responses
responses = perform_prompt_tuning(user_question)
# Provide correct answer if available
correct_answer = answer_question(user_question)
if correct_answer:
st.write("Correct Answer:")
st.write(correct_answer)
# Display the responses
st.write("Model Responses:")
for i, response in enumerate(responses):
st.write(f"{i+1}. {response}")
# Provide additional information if available
if "Sorry, I couldn't find any information" not in responses[0]:
st.write("Additional Information:")
additional_info = answer_question(user_question)
st.write(additional_info)
# Display the answer based on user selection
selected_index = st.number_input("Select the best response (1, 2, 3)", value=1, min_value=1, max_value=3, step=1)
answer = responses[selected_index - 1]
# Display the answer
st.write("Model's Answer:")
st.write(answer)
if __name__ == "__main__":
main()