File size: 13,228 Bytes
3591f24
 
 
 
 
 
 
 
 
2e32647
 
 
 
 
 
 
 
 
 
 
3591f24
 
2e32647
3591f24
2e32647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3591f24
 
 
2e32647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3591f24
 
 
2e32647
 
 
 
 
 
 
 
 
 
 
 
 
3591f24
 
 
2e32647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3591f24
 
 
2e32647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3591f24
2e32647
 
 
 
 
3591f24
2e32647
3591f24
2e32647
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
---
title: AI Writing Studio
emoji: ✍️
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: "4.0.0"
app_file: app.py
pinned: false
license: mit
short_description: AI writing revision with FLAN-T5 and rubric scoring
tags:
  - education
  - writing
  - nlp
  - text2text-generation
  - instruction-following
  - analysis
suggested_hardware: cpu-basic
suggested_storage: small
---

# Writing Studio - HuggingFace Spaces Edition

Production-grade AI Writing Studio powered by **FLAN-T5** for intelligent text revision.

## About

AI Writing Studio is a production-grade educational writing assistant that provides **real AI-powered text revision** using instruction-following models:

- **🤖 AI-Powered Revision** using FLAN-T5 (instruction-tuned for text revision)
- **📊 Real Rubric Scoring** across 5 criteria (Clarity, Conciseness, Organization, Evidence, Grammar)
- **🔍 Visual Diff Highlighting** to see exactly what changed
- **📝 5 Specialized Modes** (General, Literature, Tech Comm, Academic, Creative)

## 🆕 What's New: FLAN-T5 Integration

**Major Update**: Replaced GPT-2 with FLAN-T5 for **real AI-powered text revision**.

**What Changed**:
- ✅ **FLAN-T5** now default model (instruction-following, actually revises text)
- ❌ **GPT-2 removed** (only continues text, doesn't revise)
- 🎯 **Instruction-optimized prompts** for better revision quality
- 🚀 **Automatic model detection** (supports both T5 and GPT-2 pipelines)

**Why This Matters**:
GPT-2 couldn't revise text—it only continued it with unrelated content. FLAN-T5 understands revision instructions and produces genuine improvements to your writing.

**Trade-off**: First load is ~60s instead of ~30s, but you get actual AI revision instead of gibberish!

## Quick Start

1. Open the app on HuggingFace Spaces
2. Paste text (200-500 words recommended for first try)
3. Choose revision mode (try "General" first)
4. Click "✨ Revise & Analyze"
5. Wait ~60s for first analysis (model loading)
6. Compare original vs AI-revised text
7. Review rubric scores and highlighted changes

## Features

### ✨ AI-Powered Revision with FLAN-T5

**Why FLAN-T5?**
FLAN-T5 is an **instruction-tuned model** specifically trained to follow revision instructions. Unlike GPT-2 (which only continues text), FLAN-T5 actually understands and executes revision tasks like:
- Improving clarity and readability
- Enhancing academic tone
- Strengthening evidence and support
- Refining technical precision
- Enriching creative imagery

**Real Text Revision**: The AI doesn't just continue your text—it genuinely revises it based on the selected mode.

### 📊 Real Rubric Analysis
Unlike simple prototypes, this version includes actual analysis algorithms:
- **Clarity**: Analyzes sentence length, complexity, and structure
- **Conciseness**: Detects wordy phrases and redundancy
- **Organization**: Checks paragraph structure and transitions
- **Evidence**: Looks for supporting examples and data
- **Grammar**: Basic error detection

### 📝 5 Specialized Revision Modes
Choose from instruction-tuned templates optimized for FLAN-T5:
- **General**: Improve clarity and readability for everyday writing
- **Literature**: Strengthen literary analysis with better evidence and terminology
- **Tech Comm**: Enhance technical precision and professional tone
- **Academic**: Improve formal tone, organization, and scholarly voice
- **Creative**: Enhance imagery, voice, and reader engagement

### 🔍 Visual Diff Highlighting
See exactly what the AI changed with side-by-side comparison and highlighted differences.

### 🏭 Production Quality
- Comprehensive error handling
- Input validation and sanitization
- Structured logging
- Intelligent caching for faster responses
- Type-safe configuration with Pydantic
- Automatic model type detection

## Usage

1. **Paste your text** in the input box (up to 10,000 characters)
2. **Choose a revision mode** matching your writing context (General, Literature, Tech Comm, Academic, Creative)
3. **Click "✨ Revise & Analyze"** to get AI revision + rubric feedback
4. **Review results**: Compare original vs revised text, check rubric scores, view highlighted changes

### Tips

- **First analysis takes ~60 seconds** (FLAN-T5 model loading) - this is normal!
- **Subsequent analyses are much faster** (~5-10s) thanks to caching
- Start with shorter texts (200-500 words) for quicker results
- Try different revision modes to see how the AI adapts its approach
- Use the rubric feedback to understand what improved
- The diff view shows exactly what changed and why

## Models

### Default: google/flan-t5-base

**Why FLAN-T5?**
FLAN-T5 (Fine-tuned Language Net) is an **instruction-following model** from Google Research, specifically designed to understand and execute text revision tasks. This is fundamentally different from GPT-2 style models:

| Feature | FLAN-T5 (Current) | GPT-2 (Previous) |
|---------|------------------|------------------|
| **Task Type** | Instruction following | Text continuation |
| **Can Revise Text?** | ✅ Yes | ❌ No (only continues) |
| **Understands Instructions?** | ✅ Yes | ❌ No |
| **Works with Revision Modes?** | ✅ Yes | ❌ No |
| **Model Size** | ~250M parameters | ~124M parameters |
| **First Load Time** | ~60s | ~30s |
| **Quality** | High (task-specific) | Low (off-task) |

**FLAN-T5 Advantages:**
- ✅ Actually revises text (not just continuation)
- ✅ Follows mode-specific instructions (General, Academic, etc.)
- ✅ Produces contextually appropriate output
- ✅ Understands the task at hand

**Why Not GPT-2?**
GPT-2 and distilgpt2 are **autoregressive text generators** trained only to continue text. When given revision instructions, they ignore them and generate unrelated continuations. FLAN-T5 was explicitly trained on instruction-following tasks, making it ideal for text revision.

### Alternative Models (Advanced)

You can change the model in the UI, but these require more resources:

**google/flan-t5-large** (780M params)
- Better revision quality
- Requires CPU upgrade or GPU
- ~2-3 minutes first load

**google/flan-t5-xl** (3B params)
- Best quality revisions
- Requires T4 GPU on HF Spaces
- ~5 minutes first load

## Performance

### Hardware Recommendations

**Free Tier (CPU Basic)** ⭐ Recommended
- Works well with **google/flan-t5-base**
- First load: ~60 seconds (model download + initialization)
- Subsequent analyses: ~5-10 seconds
- Perfect for educational use and demos

**CPU Upgrade**
- Handles **google/flan-t5-large** comfortably
- First load: ~2-3 minutes
- Subsequent: ~10-15 seconds
- Better revision quality

**T4 GPU** ⚡ Best Performance
- Runs **google/flan-t5-xl** smoothly
- First load: ~5 minutes
- Subsequent: ~3-5 seconds
- Highest quality revisions

### FLAN-T5 vs GPT-2 Performance

FLAN-T5 is slightly larger than distilgpt2, but the quality difference is dramatic:
- FLAN-T5: Slower but **actually revises text correctly**
- GPT-2: Faster but **produces unusable output** (wrong task)

**The extra 30 seconds of load time is worth it for functional AI revision!**

### Optimization

The app includes production-grade optimizations:
- **Model caching**: Loaded once, reused for all requests
- **Result caching**: Same input = instant cached response
- **Intelligent pipeline selection**: Automatically uses correct pipeline for model type
- **Lazy loading**: Services initialized only when needed
- **Efficient text processing**: Minimizes unnecessary operations

## Configuration

The app works out-of-the-box with sensible defaults optimized for FLAN-T5. To customize, you can set environment variables in your HuggingFace Space settings.

### Available Environment Variables

```bash
# Model Configuration
DEFAULT_MODEL=google/flan-t5-base  # HuggingFace model ID (use FLAN-T5 variants)
MAX_MODEL_LENGTH=512               # Maximum model input/output length
DEFAULT_MAX_LENGTH=512             # Default generation length

# Application Settings
ENVIRONMENT=production             # Runtime environment (development/staging/production)
LOG_LEVEL=INFO                     # Logging level (DEBUG/INFO/WARNING/ERROR)
LOG_FORMAT=text                    # Log format (json/text) - text is easier on HF Spaces
MAX_TEXT_LENGTH=10000              # Maximum input text length

# Performance
ENABLE_CACHE=true                  # Enable result caching
CACHE_MAX_SIZE=100                 # Maximum cache entries
ENABLE_METRICS=false               # Disable metrics server on HF Spaces

# Features
ENABLE_DIFF_HIGHLIGHTING=true      # Enable visual diff view
ENABLE_RUBRIC_SCORING=true         # Enable rubric analysis
ENABLE_PROMPT_PACKS=true           # Enable revision mode selection
```

## Troubleshooting

### "Out of Memory" Error
**Problem**: Space crashes or shows OOM error
**Solutions**:
- ✅ Stick with `google/flan-t5-base` on free tier (works well)
- ✅ Reduce input text length (try 200-500 words)
- ✅ Upgrade to CPU upgrade tier for larger models
- ❌ Don't try flan-t5-large or flan-t5-xl without GPU

### Slow First Load (~60 seconds)
**This is normal!** FLAN-T5-base is ~250M parameters.
- First analysis: ~60s (model download + initialization)
- Subsequent: ~5-10s (model cached in memory)
- If it times out: Refresh and try again (HF Spaces issue)

### "Model Loading Failed"
**Problem**: Error during model initialization
**Solutions**:
- Check model name spelling (must be exact HuggingFace ID)
- Ensure internet connectivity for model download
- Try default: `google/flan-t5-base`
- Check HF Spaces logs for specific error

### AI Revision Doesn't Make Sense
**Problem**: Revision output is garbled or off-topic
**Solutions**:
- ✅ Make sure you're using FLAN-T5 (not GPT-2!)
- ✅ Try a different revision mode (General, Academic, etc.)
- ✅ Check input text is clear and well-formed
- ✅ Try shorter input text (model has 512 token limit)
- Remember: FLAN-T5 base is small; larger models (flan-t5-large) give better results

### "Text Generation Failed"
**Problem**: Error during AI revision generation
**Solutions**:
- Input too long (try shorter text)
- Model timeout (refresh and retry)
- Check HF Spaces status (temporary service issue)

## Privacy

- Text processed in-memory only
- Results cached temporarily for speed
- No long-term storage on HF Spaces
- No user tracking

## Technical Details

### How FLAN-T5 Integration Works

The app automatically detects model type and uses the appropriate pipeline:

**For FLAN-T5 models** (text2text-generation):
```python
# Detects 't5' or 'flan' in model name
pipeline("text2text-generation", model="google/flan-t5-base")
```

**For GPT-2 models** (text-generation):
```python
# Fallback for text continuation models
pipeline("text-generation", model="gpt2")
```

**Instruction-Following Prompts**:
FLAN-T5 requires structured instruction format:
```
Revise the following text to improve clarity, conciseness, and readability.
Make it clear and easy to understand while maintaining the original meaning.

Text: [user input]

Revised text:
```

This format tells FLAN-T5 exactly what to do, resulting in actual revisions instead of text continuation.

### Architecture

**Production-Grade Layered Design**:
```
src/writing_studio/
├── core/
│   ├── analyzer.py      # Main orchestrator
│   ├── config.py        # Pydantic settings (FLAN-T5 defaults)
│   └── exceptions.py    # Custom error types
├── services/
│   ├── model_service.py    # FLAN-T5 pipeline management
│   ├── prompt_service.py   # Instruction-following prompts
│   ├── rubric_service.py   # Rule-based scoring algorithms
│   └── diff_service.py     # Visual diff generation
├── utils/
│   ├── logging.py       # Structured logging
│   ├── validation.py    # Input sanitization
│   └── metrics.py       # Prometheus metrics
└── app.py               # HuggingFace Spaces entry point
```

## Source Code

Full source code available at: [GitHub Repository](https://github.com/yourusername/writing-studio)

### Local Development

```bash
git clone https://github.com/yourusername/writing-studio
cd writing-studio
pip install -r requirements.txt
python app.py
```

## Contributing

Contributions welcome! See [GitHub](https://github.com/yourusername/writing-studio) for:
- Full documentation
- Development setup
- Testing guidelines
- Code quality standards

## License

MIT License - See LICENSE file

## Acknowledgments

- **FLAN-T5**: [google/flan-t5-base](https://huggingface.co/google/flan-t5-base) by Google Research
- Built with [Gradio](https://gradio.app/) - Python web UI for ML
- Powered by [HuggingFace Transformers](https://huggingface.co/transformers/) - State-of-the-art NLP
- Hosted on [HuggingFace Spaces](https://huggingface.co/spaces) - Free ML app hosting
- Instruction-tuning research: [FLAN paper](https://arxiv.org/abs/2210.11416)

## Support

Need help?
- Issues: [GitHub Issues](https://github.com/yourusername/writing-studio/issues)
- Documentation: [GitHub Docs](https://github.com/yourusername/writing-studio/tree/main/docs)
- Questions: [GitHub Discussions](https://github.com/yourusername/writing-studio/discussions)