shilpabanerjee commited on
Commit
f81fe54
·
1 Parent(s): 907303a

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +149 -0
  3. games.csv +3 -0
  4. requirements.txt +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ games.csv filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import torch
4
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
5
+ from spellchecker import SpellChecker
6
+ from sklearn.feature_extraction.text import TfidfVectorizer
7
+ from sklearn.metrics.pairwise import cosine_similarity
8
+
9
+ # Load the games.csv file into a pandas DataFrame
10
+ @st.cache_resource # Caches the loaded data to improve performance
11
+ def load_data():
12
+ data = pd.read_csv('games.csv')
13
+ return data.copy()
14
+
15
+ games_data = load_data()
16
+
17
+ # Load the pre-trained GPT-2 model and tokenizer
18
+ tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
19
+ model = GPT2LMHeadModel.from_pretrained('gpt2')
20
+
21
+ # Load the spell checker
22
+ spell = SpellChecker()
23
+
24
+ # Function to handle user questions and provide answers based on the loaded data
25
+ def answer_question(question):
26
+ question = question.lower()
27
+ answer = ""
28
+
29
+ if "release date" in question:
30
+ # Find the release date of a game
31
+ game_title = question.split("release date of ")[1].strip()
32
+ matching_games = games_data[games_data['Title'].str.lower() == game_title]
33
+ if not matching_games.empty:
34
+ release_date = matching_games.iloc[0]['Release_Date']
35
+ answer = f"The release date of '{game_title}' is {release_date}"
36
+ else:
37
+ answer = f"Sorry, I couldn't find any information about '{game_title}'"
38
+
39
+ elif "developer" in question:
40
+ # Find the developers of a game
41
+ game_title = question.split("developers of ")[1].strip()
42
+ matching_games = games_data[games_data['Title'].str.lower() == game_title]
43
+ if not matching_games.empty:
44
+ developers = matching_games.iloc[0]['Developers']
45
+ answer = f"The developers of '{game_title}' are {developers}"
46
+ else:
47
+ answer = f"Sorry, I couldn't find any information about '{game_title}'"
48
+
49
+ elif "similar games to" in question:
50
+ # Find similar games based on user question
51
+ game_title = question.split("similar games to ")[1].strip()
52
+ matching_games = games_data[games_data['Title'].str.lower() == game_title]
53
+ if not matching_games.empty:
54
+ genre = matching_games.iloc[0]['Genres']
55
+ similar_games = find_similar_games(game_title, genre)
56
+ if similar_games:
57
+ answer = f"Here are some similar games to '{game_title}': {', '.join(similar_games)}"
58
+ else:
59
+ answer = f"Sorry, I couldn't find any similar games to '{game_title}'"
60
+ else:
61
+ answer = f"Sorry, I couldn't find any information about '{game_title}'"
62
+
63
+ # Add more question-answer logic here based on the columns in your games.csv file
64
+
65
+ return answer
66
+
67
+ # Perform prompt tuning to improve model responses
68
+ def perform_prompt_tuning(input_text):
69
+ responses = []
70
+ for _ in range(3):
71
+ inputs = tokenizer.encode(input_text, return_tensors='pt')
72
+ prompt_len = inputs.shape[1]
73
+ 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)
74
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
75
+ responses.append(response)
76
+ return responses
77
+
78
+
79
+ # Spell check the user's question
80
+ def spell_check(question):
81
+ question_tokens = question.split()
82
+ corrected_tokens = [spell.correction(token) for token in question_tokens if spell.correction(token) is not None]
83
+ corrected_question = " ".join(corrected_tokens) if corrected_tokens else ""
84
+ return corrected_question
85
+
86
+
87
+
88
+ # Find similar games based on genre
89
+ def find_similar_games(game_title, genre):
90
+ vectorizer = TfidfVectorizer()
91
+ game_titles = games_data['Title'].values.tolist()
92
+ game_titles.remove(game_title)
93
+ game_titles.append(game_title) # Append the game title to the end of the list
94
+ genre_matrix = vectorizer.fit_transform(games_data['Genres'].values.tolist())
95
+ genre_similarities = cosine_similarity(genre_matrix)
96
+ game_index = game_titles.index(game_title)
97
+ similar_game_indices = genre_similarities[game_index].argsort()[:-4:-1] # Get top 3 similar games
98
+ similar_games = [game_titles[i] for i in similar_game_indices]
99
+ return similar_games
100
+
101
+ # Define the main function that will run the Streamlit app
102
+ def main():
103
+ st.title("Game Wikipedia")
104
+
105
+ # Create a text input field for user queries
106
+ user_question = st.text_input("Ask a question")
107
+
108
+ # Spell check the user's question
109
+ corrected_question = spell_check(user_question)
110
+
111
+ # Display example questions for the user to copy-paste
112
+ st.write("Example Questions:")
113
+ st.write("1. release date of Hades")
114
+ st.write("2. developers of God of War")
115
+ st.write("3. Summary for Hollow Knight")
116
+
117
+ # When the user submits a question, get the answer and display it
118
+ if st.button("Submit"):
119
+ # Perform prompt tuning to get model responses
120
+ responses = perform_prompt_tuning(corrected_question)
121
+
122
+ # Provide correct answer if available
123
+ correct_answer = answer_question(corrected_question)
124
+ if correct_answer:
125
+ st.write("Correct Answer:")
126
+ st.write(correct_answer)
127
+
128
+ # Display the responses
129
+ st.write("Model Responses:")
130
+ for i, response in enumerate(responses):
131
+ st.write(f"{i+1}. {response}")
132
+
133
+ # Provide additional information if available
134
+ if "Sorry, I couldn't find any information" not in responses[0]:
135
+ st.write("Additional Information:")
136
+ additional_info = answer_question(corrected_question)
137
+ st.write(additional_info)
138
+
139
+ # Display the answer based on user selection
140
+ selected_index = st.number_input("Select the best response (1, 2, 3)", value=1, min_value=1, max_value=3, step=1)
141
+ answer = responses[selected_index - 1]
142
+
143
+ # Display the answer
144
+ st.write("Model's Answer:")
145
+ st.write(answer)
146
+
147
+
148
+ if __name__ == "__main__":
149
+ main()
games.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:91b18bf8932bc4cc84a39ea273ad7177dbb13a06a29cc9e1c4428ea7418a7823
3
+ size 27455318
requirements.txt ADDED
Binary file (5.38 kB). View file