Update app.py
Browse files
app.py
CHANGED
|
@@ -14,23 +14,7 @@ vocabulary = []
|
|
| 14 |
matches = {}
|
| 15 |
score = 0
|
| 16 |
remaining_pairs = 0
|
| 17 |
-
|
| 18 |
-
# Initialize Groq and Notion APIs
|
| 19 |
-
try:
|
| 20 |
-
from groq import Groq
|
| 21 |
-
except ImportError:
|
| 22 |
-
os.system('pip install groq')
|
| 23 |
-
from groq import Groq
|
| 24 |
-
|
| 25 |
-
api_key = os.getenv("groq_key")
|
| 26 |
-
notion_api_key = os.getenv("Notion_API_Key")
|
| 27 |
-
notion_db_id = os.getenv("Notion_DB_ID")
|
| 28 |
-
if not api_key:
|
| 29 |
-
raise ValueError("Environment variable 'groq_key' not set.")
|
| 30 |
-
if not notion_api_key or not notion_db_id:
|
| 31 |
-
raise ValueError("Notion API key or database ID is not set.")
|
| 32 |
-
|
| 33 |
-
client = Groq(api_key=api_key)
|
| 34 |
|
| 35 |
# Helper Functions for Matching Game
|
| 36 |
def parse_csv(file):
|
|
@@ -46,7 +30,7 @@ def parse_csv(file):
|
|
| 46 |
return "Error reading the file. Ensure it's a valid CSV.", vocabulary
|
| 47 |
|
| 48 |
def start_game():
|
| 49 |
-
global matches, score, remaining_pairs
|
| 50 |
if len(vocabulary) < 1:
|
| 51 |
return "Please upload a vocabulary file or add words first.", [], []
|
| 52 |
|
|
@@ -58,78 +42,43 @@ def start_game():
|
|
| 58 |
random.shuffle(definitions)
|
| 59 |
|
| 60 |
matches = {item['word']: item['definition'] for item in vocabulary}
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def check_match(word, definition):
|
| 64 |
-
global matches, score, remaining_pairs
|
| 65 |
if matches.get(word) == definition:
|
| 66 |
score += 1
|
| 67 |
remaining_pairs -= 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if remaining_pairs == 0:
|
| 69 |
-
return "Correct! You've matched all pairs! 🎉 Game Over.", score,
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
-
return f"Incorrect! {word} does not match {definition}. Try again.", score,
|
| 73 |
|
| 74 |
def reset_game():
|
| 75 |
-
global vocabulary, matches, score, remaining_pairs
|
| 76 |
vocabulary.clear()
|
| 77 |
matches.clear()
|
| 78 |
score = 0
|
| 79 |
remaining_pairs = 0
|
|
|
|
| 80 |
return "Game reset. Upload a new file or add words to start again.", vocabulary
|
| 81 |
|
| 82 |
-
# New Helper Functions for Groq Chat Integration
|
| 83 |
-
def log_to_notion(name, user_input, bot_response):
|
| 84 |
-
url = "https://api.notion.com/v1/pages"
|
| 85 |
-
headers = {
|
| 86 |
-
"Authorization": f"Bearer {notion_api_key}",
|
| 87 |
-
"Content-Type": "application/json",
|
| 88 |
-
"Notion-Version": "2022-06-28"
|
| 89 |
-
}
|
| 90 |
-
data = {
|
| 91 |
-
"parent": {"database_id": notion_db_id},
|
| 92 |
-
"properties": {
|
| 93 |
-
"Name": {"title": [{"text": {"content": name}}]},
|
| 94 |
-
"Timestamp": {"date": {"start": datetime.now().isoformat() }},
|
| 95 |
-
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 96 |
-
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
|
| 97 |
-
}
|
| 98 |
-
}
|
| 99 |
-
response = requests.post(url, headers=headers, json=data)
|
| 100 |
-
if response.status_code != 200:
|
| 101 |
-
print(f"Failed to log to Notion: {response.text}")
|
| 102 |
-
|
| 103 |
-
def chat_with_groq(input_text, chat_history):
|
| 104 |
-
try:
|
| 105 |
-
messages = [
|
| 106 |
-
{"role": "system", "content": (
|
| 107 |
-
"You are an English dictionary and quiz designer tutor. When you are given a vocabulary, do two things:\n"
|
| 108 |
-
"(1) Provide a definition within 50 words\n(2) Provide a sample sentence under 20 words."
|
| 109 |
-
)}
|
| 110 |
-
]
|
| 111 |
-
messages.extend(
|
| 112 |
-
{"role": "user", "content": user_msg} if idx % 2 == 0 else {"role": "assistant", "content": assistant_msg}
|
| 113 |
-
for idx, (user_msg, assistant_msg) in enumerate(chat_history)
|
| 114 |
-
)
|
| 115 |
-
messages.append({"role": "user", "content": input_text})
|
| 116 |
-
completion = client.chat.completions.create(
|
| 117 |
-
model="llama-3.3-70b-versatile",
|
| 118 |
-
messages=messages,
|
| 119 |
-
temperature=1,
|
| 120 |
-
max_tokens=1024
|
| 121 |
-
)
|
| 122 |
-
response = completion.choices[0].message.content
|
| 123 |
-
return response
|
| 124 |
-
except Exception as e:
|
| 125 |
-
return f"Error occurred: {e}"
|
| 126 |
-
|
| 127 |
-
def respond(name, message, chat_history):
|
| 128 |
-
response = chat_with_groq(message, chat_history)
|
| 129 |
-
chat_history.append((message, response))
|
| 130 |
-
log_to_notion(name, message, response)
|
| 131 |
-
return chat_history, ""
|
| 132 |
-
|
| 133 |
# Gradio Interface
|
| 134 |
def main():
|
| 135 |
with gr.Blocks() as app:
|
|
@@ -143,28 +92,38 @@ def main():
|
|
| 143 |
with gr.Tab("Play Matching Game"):
|
| 144 |
game_status = gr.Textbox(label="Game Status")
|
| 145 |
start_button = gr.Button("Start Game")
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
match_button = gr.Button("Check Match")
|
| 149 |
match_output = gr.Textbox(label="Match Result")
|
| 150 |
score_output = gr.Textbox(label="Score")
|
| 151 |
remaining_output = gr.Textbox(label="Remaining Pairs")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
with gr.Tab("Reset Game"):
|
| 162 |
reset_button = gr.Button("Reset")
|
| 163 |
reset_output = gr.Textbox(label="Reset Status")
|
| 164 |
|
| 165 |
upload_button.click(parse_csv, inputs=[csv_upload], outputs=[upload_output])
|
| 166 |
-
start_button.click(start_game, outputs=[game_status,
|
| 167 |
-
match_button.click(check_match, inputs=[word_dropdown, definition_dropdown], outputs=[match_output, score_output, remaining_output])
|
| 168 |
reset_button.click(reset_game, outputs=[reset_output])
|
| 169 |
|
| 170 |
return app
|
|
|
|
| 14 |
matches = {}
|
| 15 |
score = 0
|
| 16 |
remaining_pairs = 0
|
| 17 |
+
selected_pairs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Helper Functions for Matching Game
|
| 20 |
def parse_csv(file):
|
|
|
|
| 30 |
return "Error reading the file. Ensure it's a valid CSV.", vocabulary
|
| 31 |
|
| 32 |
def start_game():
|
| 33 |
+
global matches, score, remaining_pairs, selected_pairs
|
| 34 |
if len(vocabulary) < 1:
|
| 35 |
return "Please upload a vocabulary file or add words first.", [], []
|
| 36 |
|
|
|
|
| 42 |
random.shuffle(definitions)
|
| 43 |
|
| 44 |
matches = {item['word']: item['definition'] for item in vocabulary}
|
| 45 |
+
selected_pairs = [] # Reset selected pairs
|
| 46 |
+
|
| 47 |
+
word_cards = [{'label': w, 'value': w} for w in words]
|
| 48 |
+
definition_cards = [{'label': d, 'value': d} for d in definitions]
|
| 49 |
+
|
| 50 |
+
return "Match words with their definitions to eliminate pairs.", word_cards, definition_cards
|
| 51 |
|
| 52 |
def check_match(word, definition):
|
| 53 |
+
global matches, score, remaining_pairs, vocabulary, selected_pairs
|
| 54 |
if matches.get(word) == definition:
|
| 55 |
score += 1
|
| 56 |
remaining_pairs -= 1
|
| 57 |
+
# Mark the matched pair as selected
|
| 58 |
+
selected_pairs.append((word, definition))
|
| 59 |
+
|
| 60 |
+
# Remove the matched pair from the game
|
| 61 |
+
vocabulary = [item for item in vocabulary if item['word'] != word]
|
| 62 |
if remaining_pairs == 0:
|
| 63 |
+
return "Correct! You've matched all pairs! 🎉 Game Over.", score, [], []
|
| 64 |
+
|
| 65 |
+
# Update words and definitions for remaining pairs
|
| 66 |
+
words = [{'label': item['word'], 'value': item['word']} for item in vocabulary]
|
| 67 |
+
definitions = [{'label': item['definition'], 'value': item['definition']} for item in vocabulary]
|
| 68 |
+
|
| 69 |
+
return f"Correct! {word} matches {definition}. Remaining pairs: {remaining_pairs}", score, words, definitions
|
| 70 |
else:
|
| 71 |
+
return f"Incorrect! {word} does not match {definition}. Try again.", score, [], []
|
| 72 |
|
| 73 |
def reset_game():
|
| 74 |
+
global vocabulary, matches, score, remaining_pairs, selected_pairs
|
| 75 |
vocabulary.clear()
|
| 76 |
matches.clear()
|
| 77 |
score = 0
|
| 78 |
remaining_pairs = 0
|
| 79 |
+
selected_pairs.clear()
|
| 80 |
return "Game reset. Upload a new file or add words to start again.", vocabulary
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# Gradio Interface
|
| 83 |
def main():
|
| 84 |
with gr.Blocks() as app:
|
|
|
|
| 92 |
with gr.Tab("Play Matching Game"):
|
| 93 |
game_status = gr.Textbox(label="Game Status")
|
| 94 |
start_button = gr.Button("Start Game")
|
| 95 |
+
word_cards = gr.Gallery(label="Word Cards", height=300)
|
| 96 |
+
definition_cards = gr.Gallery(label="Definition Cards", height=300)
|
| 97 |
+
|
| 98 |
+
selected_word = gr.Textbox(label="Selected Word", interactive=False)
|
| 99 |
+
selected_definition = gr.Textbox(label="Selected Definition", interactive=False)
|
| 100 |
+
|
| 101 |
match_button = gr.Button("Check Match")
|
| 102 |
match_output = gr.Textbox(label="Match Result")
|
| 103 |
score_output = gr.Textbox(label="Score")
|
| 104 |
remaining_output = gr.Textbox(label="Remaining Pairs")
|
| 105 |
+
|
| 106 |
+
def select_word(word):
|
| 107 |
+
return word
|
| 108 |
+
|
| 109 |
+
def select_definition(definition):
|
| 110 |
+
return definition
|
| 111 |
+
|
| 112 |
+
word_cards.select(select_word, inputs=[], outputs=[selected_word])
|
| 113 |
+
definition_cards.select(select_definition, inputs=[], outputs=[selected_definition])
|
| 114 |
+
|
| 115 |
+
match_button.click(
|
| 116 |
+
check_match,
|
| 117 |
+
inputs=[selected_word, selected_definition],
|
| 118 |
+
outputs=[match_output, score_output, word_cards, definition_cards]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
with gr.Tab("Reset Game"):
|
| 122 |
reset_button = gr.Button("Reset")
|
| 123 |
reset_output = gr.Textbox(label="Reset Status")
|
| 124 |
|
| 125 |
upload_button.click(parse_csv, inputs=[csv_upload], outputs=[upload_output])
|
| 126 |
+
start_button.click(start_game, outputs=[game_status, word_cards, definition_cards])
|
|
|
|
| 127 |
reset_button.click(reset_game, outputs=[reset_output])
|
| 128 |
|
| 129 |
return app
|