AugustinHuang commited on
Commit
74b9bb1
·
verified ·
1 Parent(s): 99e1cbc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +210 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py - Gradio App cho Hugging Face Spaces
2
+ import gradio as gr
3
+ import plotly.express as px
4
+ import plotly.graph_objects as go
5
+ from plotly.subplots import make_subplots
6
+ import pandas as pd
7
+ import re
8
+ from transformers import pipeline
9
+ import io
10
+ import base64
11
+
12
+ class TaskDashboardAI:
13
+ def __init__(self):
14
+ # Load AI models
15
+ self.classifier = pipeline(
16
+ "zero-shot-classification",
17
+ model="facebook/bart-large-mnli"
18
+ )
19
+
20
+ # Categories mặc định
21
+ self.default_categories = [
22
+ "Learning", "Practice", "Planning/Ideas",
23
+ "Research", "Organization"
24
+ ]
25
+
26
+ # Keywords mapping
27
+ self.category_keywords = {
28
+ "Learning": ["học", "video", "xem", "tìm hiểu", "khám phá", "study", "learn"],
29
+ "Practice": ["thực hành", "practice", "code", "nghịch", "thử", "làm"],
30
+ "Planning/Ideas": ["dự định", "muốn", "sẽ", "plan", "chiến lược", "mvp", "tò mò"],
31
+ "Organization": ["folder", "lưu trữ", "tạo", "setup", "organize", "quản lý"],
32
+ "Research": ["nghiên cứu", "tìm hiểu", "research", "đọc", "phân tích"],
33
+ }
34
+
35
+ def extract_tasks_from_text(self, text):
36
+ """Tách text thành từng task"""
37
+ # Pattern: tìm các dòng bắt đầu bằng "•" hoặc số
38
+ tasks = re.split(r'[•\-\*]|\n\d+\.', text)
39
+ tasks = [t.strip() for t in tasks if t.strip() and len(t.strip()) > 10]
40
+ return tasks
41
+
42
+ def classify_task(self, task):
43
+ """Dùng AI để phân loại task"""
44
+ # Cách 1: Zero-shot classification
45
+ result = self.classifier(
46
+ task,
47
+ self.default_categories,
48
+ multi_label=False
49
+ )
50
+ return result['labels'][0]
51
+
52
+ def classify_task_keywords(self, task):
53
+ """Phân loại bằng keywords (backup)"""
54
+ task_lower = task.lower()
55
+ scores = {}
56
+
57
+ for category, keywords in self.category_keywords.items():
58
+ score = sum(1 for kw in keywords if kw in task_lower)
59
+ scores[category] = score
60
+
61
+ return max(scores, key=scores.get) if max(scores.values()) > 0 else "Other"
62
+
63
+ def determine_chart_type(self, data_dict):
64
+ """AI quyết định loại biểu đồ phù hợp"""
65
+ n_categories = len(data_dict)
66
+ total_items = sum(data_dict.values())
67
+
68
+ if n_categories <= 3:
69
+ return "pie"
70
+ elif n_categories <= 6:
71
+ return "donut"
72
+ elif n_categories <= 10:
73
+ return "bar"
74
+ else:
75
+ return "horizontal_bar"
76
+
77
+ def create_dashboard(self, tasks_with_categories):
78
+ """Tạo dashboard thông minh"""
79
+ # Đếm tasks theo category
80
+ category_counts = {}
81
+ for task, category in tasks_with_categories:
82
+ category_counts[category] = category_counts.get(category, 0) + 1
83
+
84
+ # Chọn chart type
85
+ chart_type = self.determine_chart_type(category_counts)
86
+
87
+ # Tạo figure
88
+ categories = list(category_counts.keys())
89
+ counts = list(category_counts.values())
90
+ colors = px.colors.qualitative.Pastel
91
+
92
+ if chart_type in ["pie", "donut"]:
93
+ fig = go.Figure(data=[go.Pie(
94
+ labels=categories,
95
+ values=counts,
96
+ hole=0.4 if chart_type == "donut" else 0,
97
+ marker_colors=colors,
98
+ textinfo='percent+label',
99
+ insidetextorientation='radial'
100
+ )])
101
+ fig.update_layout(
102
+ title_text="📊 Task Distribution by Category",
103
+ height=600,
104
+ showlegend=True
105
+ )
106
+
107
+ elif chart_type == "bar":
108
+ fig = px.bar(
109
+ x=categories,
110
+ y=counts,
111
+ color=categories,
112
+ color_discrete_sequence=colors,
113
+ title="📊 Task Distribution by Category"
114
+ )
115
+ fig.update_layout(
116
+ height=600,
117
+ showlegend=False,
118
+ xaxis_title="Category",
119
+ yaxis_title="Number of Tasks"
120
+ )
121
+
122
+ else: # horizontal_bar
123
+ fig = px.bar(
124
+ y=categories,
125
+ x=counts,
126
+ orientation='h',
127
+ color=categories,
128
+ color_discrete_sequence=colors,
129
+ title="📊 Task Distribution by Category"
130
+ )
131
+ fig.update_layout(
132
+ height=600,
133
+ showlegend=False
134
+ )
135
+
136
+ return fig, category_counts
137
+
138
+ def process_text(self, text):
139
+ """Xử lý text input và tạo dashboard"""
140
+ # Extract tasks
141
+ tasks = self.extract_tasks_from_text(text)
142
+
143
+ # Classify từng task
144
+ tasks_with_categories = []
145
+ for task in tasks:
146
+ # Dùng AI classification
147
+ category = self.classify_task(task)
148
+ tasks_with_categories.append((task, category))
149
+
150
+ # Tạo dashboard
151
+ fig, category_counts = self.create_dashboard(tasks_with_categories)
152
+
153
+ # Tạo summary text
154
+ summary = "📋 **Classification Results:**\n\n"
155
+ for task, category in tasks_with_categories:
156
+ summary += f"✓ **{category}**: {task[:50]}...\n"
157
+
158
+ summary += f"\n📊 **Summary:**\n"
159
+ for cat, count in category_counts.items():
160
+ summary += f"• {cat}: {count} tasks\n"
161
+
162
+ return fig, summary, tasks_with_categories
163
+
164
+ # Gradio Interface
165
+ dashboard_ai = TaskDashboardAI()
166
+
167
+ def create_dashboard_from_text(text):
168
+ fig, summary, tasks = dashboard_ai.process_text(text)
169
+ return fig, summary
170
+
171
+ # Tạo Gradio UI
172
+ with gr.Blocks(title="AI Task Dashboard Generator", theme=gr.themes.Soft()) as demo:
173
+ gr.Markdown("""
174
+ # 🤖 AI Task Dashboard Generator
175
+ **Paste your daily tasks → AI classifies → Auto creates beautiful charts**
176
+ """)
177
+
178
+ with gr.Row():
179
+ with gr.Column(scale=1):
180
+ input_text = gr.Textbox(
181
+ label="📝 Paste Your Tasks Here",
182
+ placeholder="• tôi đã học Python\n• tôi đã thực hành web scraping\n...",
183
+ lines=15
184
+ )
185
+ btn = gr.Button("🚀 Generate Dashboard", variant="primary")
186
+
187
+ with gr.Column(scale=2):
188
+ chart_output = gr.Plot(label="📊 Dashboard")
189
+ summary_output = gr.Textbox(label="📋 Classification Summary", lines=10)
190
+
191
+ btn.click(
192
+ fn=create_dashboard_from_text,
193
+ inputs=input_text,
194
+ outputs=[chart_output, summary_output]
195
+ )
196
+
197
+ # Examples
198
+ gr.Examples(
199
+ examples=[
200
+ """• tôi đã học Python cơ bản
201
+ • tôi đã xem video về machine learning
202
+ • tôi muốn tạo dashboard tự động
203
+ • tôi đã thực hành web scraping
204
+ • tôi cần organize folders
205
+ • tôi sẽ nghiên cứu về AI agents""",
206
+ ],
207
+ inputs=input_text
208
+ )
209
+
210
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ plotly>=5.15.0
3
+ pandas>=2.0.0
4
+ transformers>=4.30.0
5
+ torch
6
+ sentence-transformers