File size: 11,733 Bytes
67f25fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# 🌐 Free Cloud Deployment Guide

## 🎯 Best Free Options for Your Project

### ✅ **Recommended: Streamlit Community Cloud**
- **Perfect for your project** (Streamlit frontend)
- **Completely free**
- **Easy GitHub integration**
- **Custom domain support**

### ✅ **Alternative: Hugging Face Spaces**
- **Free GPU/CPU hosting**
- **Perfect for AI/ML projects**
- **Great for showcasing AI models**

### ✅ **Backup: Railway/Render**
- **Full-stack deployment**
- **Free tiers available**
- **Good for production demos**

---

## 🚀 **Option 1: Streamlit Community Cloud (RECOMMENDED)**

### Prerequisites:
1. **GitHub account** (free)
2. **Streamlit account** (free - sign up with GitHub)

### Step 1: Prepare Your Repository

Create these files for Streamlit Cloud deployment:

#### **requirements.txt** (for Streamlit Cloud)
```txt
# Core dependencies
streamlit==1.28.2
requests==2.31.0
pandas==2.1.3
numpy==1.24.3
python-dateutil==2.8.2

# Visualization
plotly==5.17.0
altair==5.1.2

# UI components
streamlit-option-menu==0.3.6
streamlit-aggrid==0.3.4.post3

# For language detection (lightweight)
langdetect==1.0.9
```

#### **streamlit_app.py** (Entry point)
```python
# Streamlit Cloud entry point
import streamlit as st
import sys
import os

# Add frontend directory to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'frontend'))

# Import the main app
from app import main

if __name__ == "__main__":
    main()
```

#### **.streamlit/config.toml** (Streamlit configuration)
```toml
[server]
headless = true
port = 8501

[browser]
gatherUsageStats = false

[theme]
primaryColor = "#FF6B6B"
backgroundColor = "#FFFFFF"
secondaryBackgroundColor = "#F0F2F6"
textColor = "#262730"
```

### Step 2: Create Cloud-Compatible Backend

Since Streamlit Cloud can't run your FastAPI backend, we'll create a lightweight version:

#### **cloud_backend.py** (Mock backend for demo)
```python
"""
Lightweight backend simulation for Streamlit Cloud deployment
This provides mock responses that look realistic for demos
"""

import random
import time
from typing import Dict, List
import pandas as pd
from datetime import datetime

class CloudTranslationService:
    """Mock translation service for cloud deployment"""
    
    def __init__(self):
        self.languages = {
            "en": "English", "hi": "Hindi", "bn": "Bengali", 
            "gu": "Gujarati", "kn": "Kannada", "ml": "Malayalam",
            "mr": "Marathi", "or": "Odia", "pa": "Punjabi",
            "ta": "Tamil", "te": "Telugu", "ur": "Urdu",
            "as": "Assamese", "ne": "Nepali", "sa": "Sanskrit"
        }
        
        # Sample translations for realistic demo
        self.sample_translations = {
            ("hello", "en", "hi"): "नमस्ते",
            ("smartphone", "en", "hi"): "स्मार्टफोन",
            ("book", "en", "hi"): "किताब",
            ("computer", "en", "hi"): "कंप्यूटर",
            ("beautiful", "en", "hi"): "सुंदर",
            ("hello", "en", "ta"): "வணக்கம்",
            ("smartphone", "en", "ta"): "ஸ்மார்ட்ஃபோன்",
            ("book", "en", "ta"): "புத்தகம்",
            ("hello", "en", "te"): "నమస్కారం",
            ("smartphone", "en", "te"): "స్మార్ట్‌ఫోన్",
        }
        
        # Mock translation history
        self.history = []
        self._generate_sample_history()
    
    def _generate_sample_history(self):
        """Generate realistic sample history"""
        sample_data = [
            ("Premium Smartphone with 128GB storage", "प्रीमियम स्मार्टफोन 128GB स्टोरेज के साथ", "en", "hi", 0.94),
            ("Wireless Bluetooth Headphones", "वायरलेस ब्लूटूथ हेडफोन्स", "en", "hi", 0.91),
            ("Cotton T-Shirt for Men", "पुरुषों के लिए कॉटन टी-शर्ट", "en", "hi", 0.89),
            ("Premium Smartphone with 128GB storage", "128GB சேமிப்பகத்துடன் பிரீமியம் ஸ்மார்ட்ஃபோன்", "en", "ta", 0.92),
            ("Wireless Bluetooth Headphones", "వైర్‌లెస్ బ్లూటూత్ హెడ్‌ఫోన్‌లు", "en", "te", 0.90),
        ]
        
        for i, (orig, trans, src, tgt, conf) in enumerate(sample_data):
            self.history.append({
                "id": i + 1,
                "original_text": orig,
                "translated_text": trans,
                "source_language": src,
                "target_language": tgt,
                "model_confidence": conf,
                "created_at": "2025-01-25T10:30:00",
                "corrected_text": None
            })
    
    def detect_language(self, text: str) -> Dict:
        """Mock language detection"""
        # Simple heuristic detection
        if any(char in text for char in "अआइईउऊएऐओऔकखगघचछजझटठडढणतथदधनपफबभमयरलवशषसह"):
            return {"language": "hi", "confidence": 0.95, "language_name": "Hindi"}
        elif any(char in text for char in "அஆஇஈஉஊஎஏஐஒஓஔகஙசஞடணதநபமயரலவழளறன"):
            return {"language": "ta", "confidence": 0.94, "language_name": "Tamil"}
        else:
            return {"language": "en", "confidence": 0.98, "language_name": "English"}
    
    def translate(self, text: str, source_lang: str, target_lang: str) -> Dict:
        """Mock translation with realistic responses"""
        time.sleep(1)  # Simulate processing time
        
        # Check for exact matches first
        key = (text.lower(), source_lang, target_lang)
        if key in self.sample_translations:
            translated = self.sample_translations[key]
            confidence = round(random.uniform(0.88, 0.96), 2)
        else:
            # Generate realistic-looking translations
            if target_lang == "hi":
                translated = f"[Hindi] {text}"
            elif target_lang == "ta":
                translated = f"[Tamil] {text}"
            elif target_lang == "te":
                translated = f"[Telugu] {text}"
            else:
                translated = f"[{self.languages.get(target_lang, target_lang)}] {text}"
            
            confidence = round(random.uniform(0.82, 0.94), 2)
        
        # Add to history
        translation_id = len(self.history) + 1
        self.history.append({
            "id": translation_id,
            "original_text": text,
            "translated_text": translated,
            "source_language": source_lang,
            "target_language": target_lang,
            "model_confidence": confidence,
            "created_at": datetime.now().isoformat(),
            "corrected_text": None
        })
        
        return {
            "translated_text": translated,
            "source_language": source_lang,
            "target_language": target_lang,
            "confidence": confidence,
            "translation_id": translation_id
        }
    
    def get_history(self, limit: int = 50) -> List[Dict]:
        """Get translation history"""
        return self.history[-limit:]
    
    def submit_correction(self, translation_id: int, corrected_text: str, feedback: str = "") -> Dict:
        """Submit correction"""
        for item in self.history:
            if item["id"] == translation_id:
                item["corrected_text"] = corrected_text
                break
        
        return {
            "correction_id": random.randint(1000, 9999),
            "message": "Correction submitted successfully",
            "status": "success"
        }
    
    def get_supported_languages(self) -> Dict:
        """Get supported languages"""
        return {
            "languages": self.languages,
            "total_count": len(self.languages)
        }

# Global instance
cloud_service = CloudTranslationService()
```

### Step 3: Modify Frontend for Cloud

#### **frontend/cloud_app.py** (Cloud-optimized version)
```python
"""
Cloud-optimized version of the Multi-Lingual Catalog Translator
Works without FastAPI backend by using mock services
"""

import streamlit as st
import sys
import os

# Add parent directory to path to import cloud_backend
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from cloud_backend import cloud_service

# Copy your existing app.py code here but replace API calls with cloud_service calls
# For example:

st.set_page_config(
    page_title="Multi-Lingual Catalog Translator",
    page_icon="🌐",
    layout="wide"
)

def main():
    st.title("🌐 Multi-Lingual Product Catalog Translator")
    st.markdown("### Powered by IndicTrans2 by AI4Bharat")
    st.markdown("**🚀 Cloud Demo Version**")
    
    # Add a banner explaining this is a demo
    st.info("🌟 **This is a cloud demo version with simulated AI responses**. The full version with real IndicTrans2 models runs locally and can be deployed on cloud infrastructure with GPU support.")
    
    # Your existing UI code here...
    # Replace API calls with cloud_service calls

if __name__ == "__main__":
    main()
```

### Step 4: Deploy to Streamlit Cloud

1. **Push to GitHub:**
   ```bash
   git add .
   git commit -m "Add Streamlit Cloud deployment"
   git push origin main
   ```

2. **Deploy on Streamlit Cloud:**
   - Go to [share.streamlit.io](https://share.streamlit.io)
   - Sign in with GitHub
   - Click "New app"
   - Select your repository
   - Set main file path: `streamlit_app.py`
   - Click "Deploy"

3. **Your app will be live at:**
   `https://[your-username]-[repo-name]-streamlit-app-[hash].streamlit.app`

---

## 🤗 **Option 2: Hugging Face Spaces**

Perfect for AI/ML projects with free GPU access!

### Step 1: Create Space Files

#### **app.py** (Hugging Face entry point)
```python
import gradio as gr
import requests
import json

def translate_text(text, source_lang, target_lang):
    # Your translation logic here
    # Can use the cloud_backend for demo
    return f"Translated: {text} ({source_lang} → {target_lang})"

# Create Gradio interface
demo = gr.Interface(
    fn=translate_text,
    inputs=[
        gr.Textbox(label="Text to translate"),
        gr.Dropdown(["en", "hi", "ta", "te", "bn"], label="Source Language"),
        gr.Dropdown(["en", "hi", "ta", "te", "bn"], label="Target Language")
    ],
    outputs=gr.Textbox(label="Translation"),
    title="Multi-Lingual Catalog Translator",
    description="AI-powered translation for e-commerce using IndicTrans2"
)

if __name__ == "__main__":
    demo.launch()
```

#### **requirements.txt** (for Hugging Face)
```txt
gradio==3.50.0
transformers==4.35.0
torch==2.1.0
fasttext==0.9.2
```

### Step 2: Deploy to Hugging Face
1. Create account at [huggingface.co](https://huggingface.co)
2. Create new Space
3. Upload your files
4. Your app will be live at `https://huggingface.co/spaces/[username]/[space-name]`

---

## 🚂 **Option 3: Railway (Full-Stack)**

For deploying both frontend and backend:

### Step 1: Create Railway Configuration

#### **railway.json**
```json
{
  "build": {
    "builder": "NIXPACKS"
  },
  "deploy": {
    "startCommand": "streamlit run streamlit_app.py --server.port $PORT --server.address 0.0.0.0",
    "healthcheckPath": "/",
    "healthcheckTimeout": 100
  }
}
```

### Step 2: Deploy
1. Go to [railway.app](https://railway.app)
2. Connect GitHub repository
3. Deploy automatically

---

## 📋 **Quick Setup for Streamlit Cloud**

Let me create the necessary files for you: