File size: 5,230 Bytes
e4eb82b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Quick Start Guide

Get up and running with the NLP Analysis API in minutes!

## Prerequisites

- Python 3.8 or higher
- pip package manager

## Installation Steps

### 1. Clone or Navigate to Project

```bash
cd sentimant
```

### 2. Create Virtual Environment (Recommended)

**Windows:**
```bash
python -m venv venv
venv\Scripts\activate
```

**Linux/Mac:**
```bash
python -m venv venv
source venv/bin/activate
```

### 3. Install Dependencies

```bash
pip install -r requirements.txt
```

This will install:
- FastAPI (web framework)
- Uvicorn (ASGI server)
- Transformers (Hugging Face models)
- PyTorch (ML backend)
- Pydantic (data validation)

### 4. Start the Server

```bash
python run_server.py
```

Or:

```bash
python main.py
```

### 5. Verify Installation

Open your browser and visit:

- **API Status**: http://localhost:8000
- **Interactive Docs**: http://localhost:8000/docs
- **Alternative Docs**: http://localhost:8000/redoc
- **Health Check**: http://localhost:8000/health

## First API Call

### Using cURL

**Sentiment Analysis:**
```bash
curl -X POST "http://localhost:8000/analyze" \
     -H "Content-Type: application/json" \
     -d "{\"text\": \"I love this API!\"}"
```

**Named Entity Recognition:**
```bash
curl -X POST "http://localhost:8000/ner" \
     -H "Content-Type: application/json" \
     -d "{\"text\": \"Apple Inc. is located in Cupertino, California.\"}"
```

**Translation:**
```bash
curl -X POST "http://localhost:8000/translate" \
     -H "Content-Type: application/json" \
     -d "{\"text\": \"Hello world\", \"source_lang\": \"en\", \"target_lang\": \"ar\"}"
```

### Using Python

```python
import requests

# Sentiment Analysis
response = requests.post(
    "http://localhost:8000/analyze",
    json={"text": "I love this API!"}
)
print(response.json())

# NER
response = requests.post(
    "http://localhost:8000/ner",
    json={"text": "Apple Inc. is in Cupertino, California."}
)
print(response.json())

# Translation
response = requests.post(
    "http://localhost:8000/translate",
    json={
        "text": "Hello world",
        "source_lang": "en",
        "target_lang": "ar"
    }
)
print(response.json())
```

### Using Interactive Docs

1. Open http://localhost:8000/docs in your browser
2. Click on any endpoint (e.g., "/analyze")
3. Click "Try it out"
4. Enter your text in the JSON body
5. Click "Execute"
6. See the response below

## What's Next?

- Read the [README.md](README.md) for detailed API documentation
- Check [ARCHITECTURE.md](ARCHITECTURE.md) to understand the codebase
- Explore the `lib/` directory structure
- Try different text samples
- Test batch processing

## Troubleshooting

### Models Not Loading

**Problem**: Long startup time or model loading errors

**Solutions**:
- Ensure stable internet connection (models download on first use)
- Free up disk space (models are ~500MB each)
- Check system RAM (models require ~2-3GB)

### Port Already in Use

**Problem**: `Address already in use` error

**Solutions**:
```bash
# Change port in main.py or run_server.py
uvicorn main:app --port 8001
```

### Import Errors

**Problem**: Module not found errors

**Solutions**:
- Ensure you're in the correct directory
- Activate virtual environment
- Reinstall requirements: `pip install -r requirements.txt`

### Slow Response Times

**Problem**: API responses are slow

**Solutions**:
- First request is always slower (cold start)
- Consider using GPU if available
- Check system resources
- Optimize batch size for large datasets

## Common Use Cases

### Analyze Product Reviews

```python
reviews = [
    "This product is amazing!",
    "Terrible quality, disappointed.",
    "It's okay, nothing special."
]

for review in reviews:
    response = requests.post(
        "http://localhost:8000/analyze",
        json={"text": review}
    )
    sentiment = response.json()
    print(f"Review: {review}")
    print(f"Sentiment: {sentiment['sentiment']} ({sentiment['confidence']})")
```

### Extract Business Information

```python
text = "Apple Inc. CEO Tim Cook announced new products at WWDC in Cupertino, California."

response = requests.post(
    "http://localhost:8000/ner",
    json={"text": text}
)

entities = response.json()
for entity in entities['entities']:
    print(f"{entity['label']}: {entity['text']} ({entity['score']})")
```

### Batch Processing

```python
texts = [
    "I love Python!",
    "FastAPI is great!",
    "Python is the best!"
]

response = requests.post(
    "http://localhost:8000/analyze-batch",
    json={"texts": texts}
)

results = response.json()
for result in results['results']:
    print(f"{result['text']}: {result['sentiment']}")
```

## Tips for Best Performance

1. **Use Batch Endpoints**: For multiple texts, use `/analyze-batch`
2. **Cache Results**: Don't re-analyze the same text
3. **Keep Server Running**: Model loading is expensive
4. **Monitor Memory**: Close unused connections
5. **Use Async**: For concurrent requests

## Need Help?

- Check the [README.md](README.md) for detailed documentation
- Review [ARCHITECTURE.md](ARCHITECTURE.md) for code structure
- Examine error messages in the server logs
- Use the interactive docs at `/docs` for API exploration

Happy analyzing! 🚀