File size: 3,496 Bytes
464b72a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Sinhala Chatbot
emoji: "🎙️"
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 7860
pinned: false
---

# Sinhala Chatbot

A voice-enabled Sinhala/English chatbot that uses speech recognition, translation, RAG, and text-to-speech.

## Features

- Voice recording in Sinhala or English
- Speech-to-text using Whisper ASR (`seniruk/whisper-small-si`)
- Translation to English before querying RAG
- RAG from uploaded PDFs (FAISS + embeddings)
- AI fallback (Gemini or Hugging Face)
- Text-to-speech with Google TTS

## Project Structure

```
chatbot-project-python/
  app/
    __init__.py
    main.py
    admin.py
    rag.py
    static/
      css/style.css
      js/script.js
    templates/
      index.html
      admin.html
  rag_data/
  .env
  .env.example
  requirements.txt
  README.md
```

## Prerequisites

- Python 3.9+
- A modern browser (Chrome, Edge, Firefox)
- Microphone access
- Gemini API key (optional but recommended)
- Hugging Face API token (optional fallback)

## Installation

### 1. Create a virtual environment

```bash
# Windows
python -m venv venv
venv\Scripts\activate

# macOS/Linux
python3 -m venv venv
source venv/bin/activate
```

### 2. Install dependencies

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

### 3. Configure environment variables

Copy the example environment file and add your API keys:

```bash
# Windows
copy .env.example .env

# macOS/Linux
cp .env.example .env
```

Edit `.env` and add:

```
GEMINI_API_KEY=your_gemini_key_here
HF_API_TOKEN=your_huggingface_token_here
```

If you do not provide a Gemini key, the app will fall back to the free Hugging Face API.

## Running the Application

### Start the main chatbot (port 8000)

```bash
# From the project root
python -m app.main
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```

Or using uvicorn directly:

```bash
swas
```

### Start the admin panel (port 9000)

In a separate terminal:

```bash
python -m app.admin
```

Or using uvicorn directly:

```bash
uvicorn app.admin:admin_app --reload --host 0.0.0.0 --port 9000
```

### Access the applications

- Chatbot UI: http://localhost:8000
- Admin panel (PDF upload): http://localhost:9000

## Usage

1. Upload PDFs using the admin panel (port 9000)
2. Open the chatbot UI (port 8000)
3. Click the microphone and speak in Sinhala or English
4. The app transcribes, translates to English, then queries RAG
5. The response is shown in text and can be played via TTS

## Troubleshooting

- If RAG answers are always from AI, upload at least one PDF and verify RAG status.
- If you see a missing API key error, check `.env` and restart the server.
- If model loading is slow, the first run downloads Whisper and embeddings.
- If PDFs already exist under `rag_data/`, the app now rebuilds/loads RAG at startup automatically.
- You can manually rebuild from all PDFs with `POST /api/rag/rebuild`.

## Deploy on Hugging Face Spaces (Docker)

This project can be deployed with UI on Hugging Face Spaces using Docker.

### 1. Create a new Space

- Go to Hugging Face Spaces and create a new Space.
- Set `SDK` to `Docker`.
- Upload/push this project files to that Space repository.

### 2. Add Space secrets

In Space settings, add these secrets:

- `GEMINI_API_KEY` (optional)
- `HF_API_TOKEN` (optional fallback)

### 3. Build and run

The provided `Dockerfile` starts:

- Main UI at `/`
- Admin UI at `/admin`

When Space build completes, open:

- `https://<your-space>.hf.space/`
- `https://<your-space>.hf.space/admin`