Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,18 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Sample dataset of resources
|
| 7 |
resources = pd.DataFrame({
|
|
@@ -10,6 +22,10 @@ resources = pd.DataFrame({
|
|
| 10 |
"Learning Style": ["Visual", "Reading", "Visual", "Visual"]
|
| 11 |
})
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to recommend resources
|
| 14 |
def recommend_resources(learning_style):
|
| 15 |
vectorizer = TfidfVectorizer()
|
|
@@ -19,19 +35,112 @@ def recommend_resources(learning_style):
|
|
| 19 |
recommended_index = similarities.argmax()
|
| 20 |
return resources.iloc[recommended_index]
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Gradio Interface
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Launch the app
|
| 37 |
-
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
import openai
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
import base64
|
| 9 |
+
import os
|
| 10 |
+
from google.auth.transport.requests import Request
|
| 11 |
+
from google.oauth2.credentials import Credentials
|
| 12 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 13 |
+
from googleapiclient.discovery import build
|
| 14 |
+
|
| 15 |
+
# Access the API key from Hugging Face Secrets
|
| 16 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 17 |
|
| 18 |
# Sample dataset of resources
|
| 19 |
resources = pd.DataFrame({
|
|
|
|
| 22 |
"Learning Style": ["Visual", "Reading", "Visual", "Visual"]
|
| 23 |
})
|
| 24 |
|
| 25 |
+
# Global variables
|
| 26 |
+
tasks = []
|
| 27 |
+
points = 0
|
| 28 |
+
|
| 29 |
# Function to recommend resources
|
| 30 |
def recommend_resources(learning_style):
|
| 31 |
vectorizer = TfidfVectorizer()
|
|
|
|
| 35 |
recommended_index = similarities.argmax()
|
| 36 |
return resources.iloc[recommended_index]
|
| 37 |
|
| 38 |
+
# Function to add a task
|
| 39 |
+
def add_task(task, deadline, priority):
|
| 40 |
+
global tasks
|
| 41 |
+
tasks.append({"Task": task, "Deadline": deadline, "Priority": priority, "Completed": False})
|
| 42 |
+
return "Task added successfully!"
|
| 43 |
+
|
| 44 |
+
# Function to mark a task as completed
|
| 45 |
+
def mark_completed(task_index):
|
| 46 |
+
global tasks, points
|
| 47 |
+
if 0 <= task_index < len(tasks):
|
| 48 |
+
tasks[task_index]["Completed"] = True
|
| 49 |
+
points += 10 # Award 10 points for completing a task
|
| 50 |
+
return f"Task marked as completed! You earned 10 points. Total points: {points}"
|
| 51 |
+
return "Invalid task index."
|
| 52 |
+
|
| 53 |
+
# Function to visualize progress
|
| 54 |
+
def show_progress():
|
| 55 |
+
completed = sum(1 for task in tasks if task["Completed"])
|
| 56 |
+
remaining = len(tasks) - completed
|
| 57 |
+
|
| 58 |
+
# Create a progress chart
|
| 59 |
+
plt.bar(["Completed", "Remaining"], [completed, remaining], color=["green", "red"])
|
| 60 |
+
plt.title("Study Progress")
|
| 61 |
+
buffer = BytesIO()
|
| 62 |
+
plt.savefig(buffer, format="png")
|
| 63 |
+
buffer.seek(0)
|
| 64 |
+
image_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 65 |
+
plt.close()
|
| 66 |
+
return f"data:image/png;base64,{image_base64}"
|
| 67 |
+
|
| 68 |
+
# Function to interact with the chatbot
|
| 69 |
+
def chatbot(user_input):
|
| 70 |
+
response = openai.Completion.create(
|
| 71 |
+
engine="text-davinci-003",
|
| 72 |
+
prompt=user_input,
|
| 73 |
+
max_tokens=50
|
| 74 |
+
)
|
| 75 |
+
return response.choices[0].text.strip()
|
| 76 |
+
|
| 77 |
+
# Function to sync tasks with Google Calendar
|
| 78 |
+
def sync_with_calendar():
|
| 79 |
+
creds = None
|
| 80 |
+
if os.path.exists("token.json"):
|
| 81 |
+
creds = Credentials.from_authorized_user_file("token.json", ["https://www.googleapis.com/auth/calendar"])
|
| 82 |
+
if not creds or not creds.valid:
|
| 83 |
+
if creds and creds.expired and creds.refresh_token:
|
| 84 |
+
creds.refresh(Request())
|
| 85 |
+
else:
|
| 86 |
+
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", ["https://www.googleapis.com/auth/calendar"])
|
| 87 |
+
creds = flow.run_local_server(port=0)
|
| 88 |
+
with open("token.json", "w") as token:
|
| 89 |
+
token.write(creds.to_json())
|
| 90 |
+
|
| 91 |
+
service = build("calendar", "v3", credentials=creds)
|
| 92 |
+
for task in tasks:
|
| 93 |
+
event = {
|
| 94 |
+
"summary": task["Task"],
|
| 95 |
+
"start": {"dateTime": task["Deadline"] + "T09:00:00", "timeZone": "UTC"},
|
| 96 |
+
"end": {"dateTime": task["Deadline"] + "T10:00:00", "timeZone": "UTC"},
|
| 97 |
+
}
|
| 98 |
+
event = service.events().insert(calendarId="primary", body=event).execute()
|
| 99 |
+
return "Tasks synced with Google Calendar!"
|
| 100 |
+
|
| 101 |
# Gradio Interface
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("# AI-Powered Study Assistant")
|
| 104 |
+
|
| 105 |
+
with gr.Tab("Task Management"):
|
| 106 |
+
with gr.Row():
|
| 107 |
+
task_input = gr.Textbox(label="Task")
|
| 108 |
+
deadline_input = gr.Textbox(label="Deadline (YYYY-MM-DD)")
|
| 109 |
+
priority_input = gr.Textbox(label="Priority (High/Medium/Low)")
|
| 110 |
+
add_task_button = gr.Button("Add Task")
|
| 111 |
+
task_output = gr.Textbox(label="Output")
|
| 112 |
+
add_task_button.click(add_task, inputs=[task_input, deadline_input, priority_input], outputs=task_output)
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
task_index_input = gr.Number(label="Task Index to Mark as Completed")
|
| 116 |
+
mark_completed_button = gr.Button("Mark Completed")
|
| 117 |
+
mark_completed_output = gr.Textbox(label="Output")
|
| 118 |
+
mark_completed_button.click(mark_completed, inputs=task_index_input, outputs=mark_completed_output)
|
| 119 |
+
|
| 120 |
+
gr.Markdown("### Tasks")
|
| 121 |
+
task_list = gr.Dataframe(headers=["Task", "Deadline", "Priority", "Completed"], value=tasks)
|
| 122 |
+
|
| 123 |
+
with gr.Tab("Progress Tracking"):
|
| 124 |
+
progress_button = gr.Button("Show Progress")
|
| 125 |
+
progress_image = gr.Image(label="Progress Chart")
|
| 126 |
+
progress_button.click(show_progress, outputs=progress_image)
|
| 127 |
+
|
| 128 |
+
with gr.Tab("Chatbot"):
|
| 129 |
+
chatbot_input = gr.Textbox(label="Ask a question:")
|
| 130 |
+
chatbot_output = gr.Textbox(label="Chatbot Response")
|
| 131 |
+
chatbot_button = gr.Button("Ask")
|
| 132 |
+
chatbot_button.click(chatbot, inputs=chatbot_input, outputs=chatbot_output)
|
| 133 |
+
|
| 134 |
+
with gr.Tab("Recommendations"):
|
| 135 |
+
learning_style_input = gr.Textbox(label="Enter your preferred learning style (Visual/Reading):")
|
| 136 |
+
recommendation_output = gr.Textbox(label="Recommended Resource")
|
| 137 |
+
recommend_button = gr.Button("Get Recommendation")
|
| 138 |
+
recommend_button.click(recommend_resources, inputs=learning_style_input, outputs=recommendation_output)
|
| 139 |
+
|
| 140 |
+
with gr.Tab("Google Calendar Sync"):
|
| 141 |
+
sync_button = gr.Button("Sync with Google Calendar")
|
| 142 |
+
sync_output = gr.Textbox(label="Output")
|
| 143 |
+
sync_button.click(sync_with_calendar, outputs=sync_output)
|
| 144 |
|
| 145 |
# Launch the app
|
| 146 |
+
demo.launch()
|