Lý thuyết
45 phút
Bài 11/15

Building AI Analysis Tool

Xây dựng công cụ AI phân tích dữ liệu và documents

📊 Building AI Analysis Tool

Xây dựng công cụ phân tích data/documents với AI.

Project Overview

What We'll Build

Text
1AI Analysis Tool Features:
2- Upload documents/data
3- AI-powered analysis
4- Insights & summaries
5- Export reports
6- History tracking

Analysis Types

Text
11. Text Analysis
2 - Sentiment
3 - Key topics
4 - Summary
5
62. Data Analysis
7 - Patterns
8 - Trends
9 - Anomalies
10
113. Document Analysis
12 - Extract info
13 - Q&A on content
14 - Summarize

UI Design

Text
1┌────────────────────────────────────────────────┐
2│ 📊 AI Analyzer [History] 👤│
3├────────────────────────────────────────────────┤
4│ │
5│ ┌─────────────────────────────────────────┐ │
6│ │ 📁 Drop files here or [Browse] │ │
7│ │ PDF, TXT, CSV, DOCX (max 10MB) │ │
8│ └─────────────────────────────────────────┘ │
9│ │
10│ Or paste text directly: │
11│ ┌─────────────────────────────────────────┐ │
12│ │ │ │
13│ │ │ │
14│ └─────────────────────────────────────────┘ │
15│ │
16│ Analysis Type: [Full Analysis ▼] │
17│ │
18│ [🔍 Analyze Now] │
19│ │
20├────────────────────────────────────────────────┤
21│ Results │
22│ ┌─────────────────────────────────────────┐ │
23│ │ Summary │ │
24│ │ ───────── │ │
25│ │ This document discusses... │ │
26│ │ │ │
27│ │ Key Points │ │
28│ │ ───────── │ │
29│ │ • Point 1 │ │
30│ │ • Point 2 │ │
31│ │ │ │
32│ │ Sentiment: Positive (85%) │ │
33│ └─────────────────────────────────────────┘ │
34│ │
35│ [📥 Export PDF] [💾 Save] [📋 Copy] │
36└────────────────────────────────────────────────┘

Database Design

Text
1Analysis:
2- title (text)
3- input_text (text)
4- input_file (file)
5- analysis_type (text)
6- results (text/JSON)
7- summary (text)
8- sentiment (text)
9- sentiment_score (number)
10- key_points (list of texts)
11- created_date (date)
12- user (User)
13- is_favorite (yes/no)

File Upload Setup

Bubble File Uploader

Text
1File Uploader element:
2- Accept: .txt, .pdf, .csv, .docx
3- Max size: 10MB
4- Dynamic: Store as private file

Reading File Content

File Processing
Text
1Bubble limitations:
2- Can't directly read PDF/DOCX content
3- Need external service
4
5Options:
61. Use text files only (simplest)
72. Use API for PDF parsing
83. Third-party integration (DocParser)
94. Backend service

Simple Text Upload

Text
1For MVP:
2- Accept .txt and .csv only
3- Read content directly
4- Or copy-paste text input

Analysis Prompts

Full Analysis Prompt

Text
1System:
2"You are an expert analyst. Analyze content thoroughly
3and provide structured insights."
4
5User:
6"Analyze this content and provide:
71. Brief Summary (2-3 sentences)
82. Key Points (5 bullet points)
93. Sentiment (positive/negative/neutral + confidence %)
104. Main Topics (list)
115. Actionable Insights (if applicable)
12
13Format as JSON:
14{
15 'summary': '...',
16 'key_points': ['...', '...'],
17 'sentiment': {'label': '...', 'score': 0.0},
18 'topics': ['...'],
19 'insights': ['...']
20}
21
22Content:
23{content}"

Sentiment Analysis Prompt

Text
1"Analyze the sentiment of this text.
2Return JSON:
3{
4 'sentiment': 'positive/negative/neutral/mixed',
5 'score': 0.0-1.0,
6 'emotions': ['emotion1', 'emotion2'],
7 'explanation': 'brief reason'
8}
9
10Text: {content}"

Summary Prompt

Text
1"Summarize this content:
21. One-sentence overview
32. Detailed summary (100 words)
43. Key takeaways (3 points)
5
6Content: {content}"

Analysis Workflow

Text
1Button "Analyze" clicked:
2
3Step 1: Get content
4- If file uploaded: read file content
5- Else: use text input
6- Validate not empty
7
8Step 2: Set loading state
9- Show "Analyzing..."
10- Disable button
11
12Step 3: Select prompt
13- Based on analysis_type dropdown
14
15Step 4: Call OpenAI API
16- With appropriate prompt
17- Send content
18
19Step 5: Parse response
20- Extract JSON
21- Handle errors
22
23Step 6: Display results
24- Populate result sections
25- Show visualizations
26
27Step 7: Save to database
28- Create Analysis record
29- Store all results
30
31Step 8: Clear loading

Displaying Results

Summary Section

Text
1Group "Summary":
2- Text: results:summary
3- Visible when results exist

Key Points

Text
1Repeating Group:
2- Type: text
3- Source: results:key_points
4- Cell: bullet + text

Sentiment Visualization

Text
1Visual components:
21. Label badge (color-coded)
3 - Positive: green
4 - Negative: red
5 - Neutral: gray
6
72. Progress bar
8 - Width: sentiment_score * 100%
9
103. Emoji indicator
11 - 😊 / 😐 / 😢

Topics Tags

Text
1Repeating Group (horizontal):
2- Type: text
3- Source: results:topics
4- Cell: chip/tag style

CSV Data Analysis

Parse CSV

Text
1For CSV files:
21. Read file content
32. Parse rows and columns
43. Send structure info to AI

Analysis Prompt for Data

Text
1"Analyze this CSV data:
2Columns: {column_names}
3Sample rows: {first_10_rows}
4Total rows: {row_count}
5
6Provide:
71. Data overview
82. Column descriptions
93. Key patterns or trends
104. Data quality issues
115. Recommendations"

Document Q&A Feature

Ask Questions

Text
1After analysis:
2- Show question input
3- "Ask a question about this document"
4- AI answers based on content

Implementation

Text
1Q&A Prompt:
2"Based on this document:
3{document_content}
4
5Answer this question:
6{user_question}
7
8Be specific and cite relevant parts."

Export Features

Copy Results

Text
1Format for clipboard:
2"
3ANALYSIS RESULTS
4================
5Summary: {summary}
6
7Key Points:
8- {point1}
9- {point2}
10...
11
12Sentiment: {sentiment} ({score}%)
13"

Export as PDF

Text
1Options:
21. Use PDF generation plugin
32. Create HTML, convert to PDF
43. Third-party service (PDFMonkey)

History Page

Layout

Text
1┌────────────────────────────────────────┐
2│ Analysis History │
3├────────────────────────────────────────┤
4│ ┌────────────────────────────────────┐ │
5│ │ 📄 Q3 Report Analysis │ │
6│ │ Dec 15, 2024 | Full Analysis │ │
7│ │ Sentiment: Positive │ │
8│ │ [View] [Export] [Delete] │ │
9│ └────────────────────────────────────┘ │
10└────────────────────────────────────────┘

View Previous Analysis

Text
1Click on history item:
21. Navigate to analysis page
32. Load saved results
43. Display without re-analyzing

Error Handling

Handle Errors
Text
1Common issues:
2
31. File too large
4 "File exceeds 10MB limit. Please use a smaller file."
5
62. Unsupported format
7 "This file type is not supported. Please use TXT or CSV."
8
93. Content too long
10 "Content is too long. Please use text under 50,000 characters."
11
124. API error
13 "Analysis failed. Please try again."

Performance Tips

Optimize Performance
Text
11. Truncate very long content
2 - Send first 50,000 characters
3 - Or summarize in chunks
4
52. Show partial results
6 - Display as available
7 - Don't wait for everything
8
93. Cache similar analyses
10 - Hash input content
11 - Return cached if exists
12
134. Background processing
14 - For large files
15 - Email results when done

Testing Checklist

Test Your Tool
Text
1☐ Text input analysis works
2☐ File upload works (txt, csv)
3☐ All analysis types work
4☐ Results display correctly
5☐ Sentiment visualization works
6☐ Copy results works
7☐ Save to history works
8☐ View history works
9☐ Error handling works
10☐ Loading states show

Bài Tập

Complete Project

Build AI Analysis Tool:

  1. Create database structure
  2. Build upload/input UI
  3. Implement analysis prompts
  4. Create analysis workflow
  5. Display results with visualizations
  6. Add sentiment indicator
  7. Implement Q&A feature
  8. Build history page
  9. Add export options
  10. Test with various content

Tiếp theo: Bài 12 - User Authentication & Payments