Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
import pandas as pd
|
| 4 |
import ssl
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Workaround for SSL issues in some environments
|
| 7 |
ssl._create_default_https_context = ssl._create_unverified_context
|
|
@@ -12,7 +15,24 @@ matches = {}
|
|
| 12 |
score = 0
|
| 13 |
remaining_pairs = 0
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def parse_csv(file):
|
| 17 |
global remaining_pairs
|
| 18 |
try:
|
|
@@ -59,6 +79,57 @@ def reset_game():
|
|
| 59 |
remaining_pairs = 0
|
| 60 |
return "Game reset. Upload a new file or add words to start again.", vocabulary
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Gradio Interface
|
| 63 |
def main():
|
| 64 |
with gr.Blocks() as app:
|
|
@@ -79,11 +150,18 @@ def main():
|
|
| 79 |
score_output = gr.Textbox(label="Score")
|
| 80 |
remaining_output = gr.Textbox(label="Remaining Pairs")
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
with gr.Tab("Reset Game"):
|
| 83 |
reset_button = gr.Button("Reset")
|
| 84 |
reset_output = gr.Textbox(label="Reset Status")
|
| 85 |
|
| 86 |
-
# Actions
|
| 87 |
upload_button.click(parse_csv, inputs=[csv_upload], outputs=[upload_output])
|
| 88 |
start_button.click(start_game, outputs=[game_status, word_dropdown, definition_dropdown])
|
| 89 |
match_button.click(check_match, inputs=[word_dropdown, definition_dropdown], outputs=[match_output, score_output, remaining_output])
|
|
@@ -91,7 +169,6 @@ def main():
|
|
| 91 |
|
| 92 |
return app
|
| 93 |
|
| 94 |
-
# Run Application
|
| 95 |
if __name__ == "__main__":
|
| 96 |
app = main()
|
| 97 |
app.launch()
|
|
|
|
| 2 |
import random
|
| 3 |
import pandas as pd
|
| 4 |
import ssl
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import requests
|
| 8 |
|
| 9 |
# Workaround for SSL issues in some environments
|
| 10 |
ssl._create_default_https_context = ssl._create_unverified_context
|
|
|
|
| 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):
|
| 37 |
global remaining_pairs
|
| 38 |
try:
|
|
|
|
| 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:
|
|
|
|
| 150 |
score_output = gr.Textbox(label="Score")
|
| 151 |
remaining_output = gr.Textbox(label="Remaining Pairs")
|
| 152 |
|
| 153 |
+
with gr.Tab("English Vocab Bot"):
|
| 154 |
+
chatbot = gr.Chatbot()
|
| 155 |
+
name_input = gr.Textbox(label="Name", placeholder="Enter your name")
|
| 156 |
+
message_input = gr.Textbox(placeholder="Input your vocab...")
|
| 157 |
+
send_button = gr.Button("Submit")
|
| 158 |
+
chat_history = gr.State([])
|
| 159 |
+
send_button.click(respond, [name_input, message_input, chat_history], [chatbot, message_input])
|
| 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, word_dropdown, definition_dropdown])
|
| 167 |
match_button.click(check_match, inputs=[word_dropdown, definition_dropdown], outputs=[match_output, score_output, remaining_output])
|
|
|
|
| 169 |
|
| 170 |
return app
|
| 171 |
|
|
|
|
| 172 |
if __name__ == "__main__":
|
| 173 |
app = main()
|
| 174 |
app.launch()
|