Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
vocabulary = []
|
| 6 |
matches = {}
|
| 7 |
score = 0
|
|
@@ -47,16 +69,60 @@ def reset_quiz():
|
|
| 47 |
score = 0
|
| 48 |
return "Quiz reset! Ready for a new start."
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
with gr.Blocks() as app:
|
| 51 |
with gr.Row():
|
| 52 |
-
gr.Markdown("# 📚 Personal English Vocabulary App")
|
| 53 |
|
|
|
|
| 54 |
with gr.Tab("Upload Vocabulary"):
|
| 55 |
with gr.Row():
|
| 56 |
file_input = gr.File(label="Upload Vocabulary CSV")
|
| 57 |
file_output = gr.Textbox(label="Upload Status")
|
| 58 |
upload_button = gr.Button("Upload")
|
| 59 |
-
|
| 60 |
upload_button.click(parse_csv, inputs=file_input, outputs=file_output)
|
| 61 |
|
| 62 |
with gr.Tab("Add Vocabulary"):
|
|
@@ -64,13 +130,12 @@ with gr.Blocks() as app:
|
|
| 64 |
definition_input = gr.Textbox(label="Definition")
|
| 65 |
add_button = gr.Button("Add Word")
|
| 66 |
add_status = gr.Textbox(label="Status")
|
| 67 |
-
|
| 68 |
add_button.click(add_word, inputs=[word_input, definition_input], outputs=add_status)
|
| 69 |
|
| 70 |
with gr.Tab("Quiz"):
|
| 71 |
quiz_status = gr.Textbox(label="Status")
|
| 72 |
start_button = gr.Button("Start Quiz")
|
| 73 |
-
words_list = gr.Dropdown(label="Select Word", choices=[])
|
| 74 |
definitions_list = gr.Dropdown(label="Select Definition", choices=[])
|
| 75 |
check_button = gr.Button("Check Match")
|
| 76 |
match_status = gr.Textbox(label="Result")
|
|
@@ -82,10 +147,25 @@ with gr.Blocks() as app:
|
|
| 82 |
start_button.click(refresh_quiz, inputs=None, outputs=[quiz_status, words_list, definitions_list])
|
| 83 |
check_button.click(check_match, inputs=[words_list, definitions_list], outputs=match_status)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
reset_button = gr.Button("Reset Quiz")
|
| 87 |
reset_status = gr.Textbox(label="Reset Status")
|
| 88 |
-
|
| 89 |
reset_button.click(reset_quiz, inputs=None, outputs=reset_status)
|
| 90 |
|
| 91 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
+
# Initialize Groq Chatbot
|
| 9 |
+
try:
|
| 10 |
+
from groq import Groq
|
| 11 |
+
except ImportError:
|
| 12 |
+
os.system('pip install groq')
|
| 13 |
+
from groq import Groq
|
| 14 |
+
|
| 15 |
+
groq_api_key = os.getenv("groq_key")
|
| 16 |
+
notion_api_key = os.getenv("Notion_API_Key")
|
| 17 |
+
notion_db_id = os.getenv("Notion_DB_ID")
|
| 18 |
+
|
| 19 |
+
if not groq_api_key:
|
| 20 |
+
raise ValueError("'groq_key' is missing. Please set the API key.")
|
| 21 |
+
if not notion_api_key or not notion_db_id:
|
| 22 |
+
raise ValueError("Notion API Key or Database ID is missing.")
|
| 23 |
+
|
| 24 |
+
client = Groq(api_key=groq_api_key)
|
| 25 |
+
|
| 26 |
+
# Vocabulary App Functions
|
| 27 |
vocabulary = []
|
| 28 |
matches = {}
|
| 29 |
score = 0
|
|
|
|
| 69 |
score = 0
|
| 70 |
return "Quiz reset! Ready for a new start."
|
| 71 |
|
| 72 |
+
# Groq Chatbot Integration
|
| 73 |
+
def chat_with_groq(input_text, chat_history):
|
| 74 |
+
try:
|
| 75 |
+
messages = [
|
| 76 |
+
{"role": "system", "content": (
|
| 77 |
+
"You are an English dictionary and quiz designer tutor. When given a vocabulary word,
|
| 78 |
+
you should provide the following: \n(1) A concise definition (max 50 words)\n(2) A sample sentence (max 20 words)."
|
| 79 |
+
)}
|
| 80 |
+
]
|
| 81 |
+
messages.extend(
|
| 82 |
+
{"role": "user", "content": user_msg} if idx % 2 == 0 else {"role": "assistant", "content": assistant_msg}
|
| 83 |
+
for idx, (user_msg, assistant_msg) in enumerate(chat_history)
|
| 84 |
+
)
|
| 85 |
+
messages.append({"role": "user", "content": input_text})
|
| 86 |
+
|
| 87 |
+
response = client.chat.completions.create(
|
| 88 |
+
model="llama-3.3-70b-versatile",
|
| 89 |
+
messages=messages,
|
| 90 |
+
temperature=1,
|
| 91 |
+
max_tokens=1024,
|
| 92 |
+
)
|
| 93 |
+
return response.choices[0].message.content
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error: {e}"
|
| 96 |
+
|
| 97 |
+
def log_to_notion(name, user_input, bot_response):
|
| 98 |
+
url = "https://api.notion.com/v1/pages"
|
| 99 |
+
headers = {
|
| 100 |
+
"Authorization": f"Bearer {notion_api_key}",
|
| 101 |
+
"Content-Type": "application/json",
|
| 102 |
+
"Notion-Version": "2022-06-28"
|
| 103 |
+
}
|
| 104 |
+
data = {
|
| 105 |
+
"parent": {"database_id": notion_db_id},
|
| 106 |
+
"properties": {
|
| 107 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
| 108 |
+
"Timestamp": {"date": {"start": datetime.now().isoformat()}},
|
| 109 |
+
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 110 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
requests.post(url, headers=headers, json=data)
|
| 114 |
+
|
| 115 |
+
# Gradio UI
|
| 116 |
with gr.Blocks() as app:
|
| 117 |
with gr.Row():
|
| 118 |
+
gr.Markdown("# 📚 Personal English Vocabulary App & Chatbot")
|
| 119 |
|
| 120 |
+
# Tabs
|
| 121 |
with gr.Tab("Upload Vocabulary"):
|
| 122 |
with gr.Row():
|
| 123 |
file_input = gr.File(label="Upload Vocabulary CSV")
|
| 124 |
file_output = gr.Textbox(label="Upload Status")
|
| 125 |
upload_button = gr.Button("Upload")
|
|
|
|
| 126 |
upload_button.click(parse_csv, inputs=file_input, outputs=file_output)
|
| 127 |
|
| 128 |
with gr.Tab("Add Vocabulary"):
|
|
|
|
| 130 |
definition_input = gr.Textbox(label="Definition")
|
| 131 |
add_button = gr.Button("Add Word")
|
| 132 |
add_status = gr.Textbox(label="Status")
|
|
|
|
| 133 |
add_button.click(add_word, inputs=[word_input, definition_input], outputs=add_status)
|
| 134 |
|
| 135 |
with gr.Tab("Quiz"):
|
| 136 |
quiz_status = gr.Textbox(label="Status")
|
| 137 |
start_button = gr.Button("Start Quiz")
|
| 138 |
+
words_list = gr.Dropdown(label="Select Word", choices=[])
|
| 139 |
definitions_list = gr.Dropdown(label="Select Definition", choices=[])
|
| 140 |
check_button = gr.Button("Check Match")
|
| 141 |
match_status = gr.Textbox(label="Result")
|
|
|
|
| 147 |
start_button.click(refresh_quiz, inputs=None, outputs=[quiz_status, words_list, definitions_list])
|
| 148 |
check_button.click(check_match, inputs=[words_list, definitions_list], outputs=match_status)
|
| 149 |
|
| 150 |
+
with gr.Tab("Vocabulary Chatbot"):
|
| 151 |
+
gr.Markdown("## AI-Powered Vocabulary Chatbot")
|
| 152 |
+
chatbot = gr.Chatbot()
|
| 153 |
+
user_input = gr.Textbox(label="Ask about a word or usage:")
|
| 154 |
+
name_input = gr.Textbox(label="Name", placeholder="Enter your name")
|
| 155 |
+
send_button = gr.Button("Submit")
|
| 156 |
+
chat_history = gr.State([])
|
| 157 |
+
|
| 158 |
+
def respond(name, message, chat_history):
|
| 159 |
+
bot_response = chat_with_groq(message, chat_history)
|
| 160 |
+
chat_history.append((message, bot_response))
|
| 161 |
+
log_to_notion(name, message, bot_response)
|
| 162 |
+
return chat_history, ""
|
| 163 |
+
|
| 164 |
+
send_button.click(respond, [name_input, user_input, chat_history], [chatbot, user_input])
|
| 165 |
+
|
| 166 |
with gr.Row():
|
| 167 |
reset_button = gr.Button("Reset Quiz")
|
| 168 |
reset_status = gr.Textbox(label="Reset Status")
|
|
|
|
| 169 |
reset_button.click(reset_quiz, inputs=None, outputs=reset_status)
|
| 170 |
|
| 171 |
app.launch()
|