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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: MiniCPM Financial RAG
|
| 3 |
+
sdk: gradio
|
| 4 |
+
sdk_version: 5.34.0
|
| 5 |
+
python_version: "3.11"
|
| 6 |
+
app_file: app.py
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# π° MiniCPM Financial RAG
|
| 13 |
+
|
| 14 |
+
### π Financial Document Intelligence Powered by Retrieval-Augmented Generation
|
| 15 |
+
|
| 16 |
+
**Real-world Problem**
|
| 17 |
+
|
| 18 |
+
Financial reports, insurance documents, annual reports, SEC filings, balance sheets, and investment documents often contain hundreds of pages of complex information.
|
| 19 |
+
|
| 20 |
+
Finding specific financial insights manually is time-consuming and error-prone.
|
| 21 |
+
|
| 22 |
+
**MiniCPM Financial RAG** enables users to upload financial PDF documents and ask questions in natural language. The system retrieves the most relevant information from the document and generates accurate, context-aware answers using MiniCPM models.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## π― Example Questions
|
| 27 |
+
|
| 28 |
+
- What is the company's total revenue?
|
| 29 |
+
- What is the net income for this period?
|
| 30 |
+
- What are the major risk factors?
|
| 31 |
+
- Summarize the financial outlook.
|
| 32 |
+
- What liabilities are reported?
|
| 33 |
+
- What is the operating cash flow?
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# π€ Models Used
|
| 39 |
+
|
| 40 |
+
| Task | Model | Parameters | Purpose |
|
| 41 |
+
|------|--------|------------|---------|
|
| 42 |
+
| Financial Question Answering | openbmb/MiniCPM-2B-128K | 2B | Financial reasoning and answer generation |
|
| 43 |
+
| Embedding Generation | openbmb/MiniCPM-Embedding-Light | Lightweight | Semantic retrieval and vector search |
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
# π§ Why MiniCPM?
|
| 48 |
+
|
| 49 |
+
| Model | Benefits |
|
| 50 |
+
|---------|----------|
|
| 51 |
+
| MiniCPM-2B-128K | Lightweight, fast inference, long-context understanding |
|
| 52 |
+
| MiniCPM-Embedding-Light | Efficient embeddings with strong retrieval performance |
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
# π Features
|
| 57 |
+
|
| 58 |
+
| Feature | Description |
|
| 59 |
+
|----------|-------------|
|
| 60 |
+
| π PDF Upload | Upload financial reports and PDF documents |
|
| 61 |
+
| βοΈ Smart Chunking | Automatically split documents into meaningful chunks |
|
| 62 |
+
| π Semantic Search | Retrieve the most relevant financial information |
|
| 63 |
+
| π§ Financial Question Answering | Ask questions in natural language |
|
| 64 |
+
| π Retrieval-Augmented Generation | Generate context-grounded answers |
|
| 65 |
+
| β‘ GPU Acceleration | Fast inference using Modal GPU infrastructure |
|
| 66 |
+
| π Financial Analysis | Analyze revenue, expenses, assets, and liabilities |
|
| 67 |
+
| π― High Accuracy Retrieval | FAISS-based vector similarity search |
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
# π Knowledge Pipeline
|
| 76 |
+
|
| 77 |
+
| Stage | Purpose |
|
| 78 |
+
|--------|----------|
|
| 79 |
+
| PDF Parsing | Extract text from PDF documents |
|
| 80 |
+
| Text Chunking | Break large documents into manageable sections |
|
| 81 |
+
| Embedding Generation | Convert text chunks into vector representations |
|
| 82 |
+
| FAISS Storage | Store vectors efficiently for retrieval |
|
| 83 |
+
| Similarity Search | Retrieve the most relevant document chunks |
|
| 84 |
+
| LLM Generation | Generate grounded answers from retrieved context |
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
# βοΈ Tech Stack
|
| 89 |
+
|
| 90 |
+
| Layer | Technology |
|
| 91 |
+
|--------|------------|
|
| 92 |
+
| Frontend | Gradio |
|
| 93 |
+
| Backend | Modal |
|
| 94 |
+
| LLM | MiniCPM-2B-128K |
|
| 95 |
+
| Embeddings | MiniCPM-Embedding-Light |
|
| 96 |
+
| Vector Database | FAISS |
|
| 97 |
+
| Framework | LangChain |
|
| 98 |
+
| PDF Processing | PyPDFLoader |
|
| 99 |
+
| Deep Learning | PyTorch |
|
| 100 |
+
| Deployment | Hugging Face Spaces |
|
| 101 |
+
|
| 102 |
+
---
|
| 103 |
+
|
| 104 |
+
# π Monitoring
|
| 105 |
+
|
| 106 |
+
The application is continuously monitored to ensure reliability and performance.
|
| 107 |
+
|
| 108 |
+
| Component | Monitoring Method |
|
| 109 |
+
|------------|------------------|
|
| 110 |
+
| Hugging Face Space | Build Logs & Runtime Logs |
|
| 111 |
+
| Modal Backend | Endpoint Monitoring |
|
| 112 |
+
| Retrieval Pipeline | Context Validation |
|
| 113 |
+
| Vector Search | Similarity Search Accuracy |
|
| 114 |
+
| Question Answering | Response Validation |
|
| 115 |
+
| System Health | Runtime Monitoring |
|
| 116 |
+
|
| 117 |
+
### Monitoring Checklist
|
| 118 |
+
|
| 119 |
+
- Monitor application uptime
|
| 120 |
+
- Validate retrieval quality
|
| 121 |
+
- Verify answer accuracy
|
| 122 |
+
- Check Modal endpoint status
|
| 123 |
+
- Review Hugging Face logs
|
| 124 |
+
- Monitor memory and GPU utilization
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
# π Deployment Architecture
|
| 129 |
+
|
| 130 |
+
```text
|
| 131 |
+
Hugging Face Spaces
|
| 132 |
+
β
|
| 133 |
+
βΌ
|
| 134 |
+
Gradio Frontend
|
| 135 |
+
β
|
| 136 |
+
βΌ
|
| 137 |
+
Modal Backend
|
| 138 |
+
β
|
| 139 |
+
ββββββββ΄βββββββ
|
| 140 |
+
βΌ βΌ
|
| 141 |
+
|
| 142 |
+
MiniCPM QA FAISS Retrieval
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
# π Project Structure
|
| 148 |
+
|
| 149 |
+
```text
|
| 150 |
+
MiniCPM_Financial_RAG/
|
| 151 |
+
|
| 152 |
+
βββ backend/
|
| 153 |
+
β βββ app.py
|
| 154 |
+
β
|
| 155 |
+
βββ frontend/
|
| 156 |
+
β βββ gradio_app.py
|
| 157 |
+
β
|
| 158 |
+
βββ requirements.txt
|
| 159 |
+
β
|
| 160 |
+
βββ README.md
|
| 161 |
+
β
|
| 162 |
+
βββ assets/
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
# π» Installation
|
| 168 |
+
|
| 169 |
+
## Create Virtual Environment
|
| 170 |
+
|
| 171 |
+
```bash
|
| 172 |
+
python -m venv venv
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
## Activate Environment
|
| 176 |
+
|
| 177 |
+
### Windows
|
| 178 |
+
|
| 179 |
+
```bash
|
| 180 |
+
venv\Scripts\activate
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
### Linux / Mac
|
| 184 |
+
|
| 185 |
+
```bash
|
| 186 |
+
source venv/bin/activate
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
## Install Dependencies
|
| 190 |
+
|
| 191 |
+
```bash
|
| 192 |
+
pip install -r requirements.txt
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
---
|
| 196 |
+
|
| 197 |
+
# π Run Application
|
| 198 |
+
|
| 199 |
+
```bash
|
| 200 |
+
python app.py
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
Application URL:
|
| 204 |
+
|
| 205 |
+
```text
|
| 206 |
+
http://localhost:8000
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
---
|
| 210 |
+
|
| 211 |
+
# π― Target Users
|
| 212 |
+
|
| 213 |
+
| User Type | Use Case |
|
| 214 |
+
|------------|----------|
|
| 215 |
+
| Financial Analysts | Analyze reports and statements |
|
| 216 |
+
| Investors | Extract investment insights |
|
| 217 |
+
| Accountants | Review financial data quickly |
|
| 218 |
+
| Auditors | Validate financial information |
|
| 219 |
+
| Researchers | Analyze large financial documents |
|
| 220 |
+
| Students | Learn financial concepts interactively |
|
| 221 |
+
|
| 222 |
+
---
|
| 223 |
+
|
| 224 |
+
# π Benefits
|
| 225 |
+
|
| 226 |
+
| Benefit | Description |
|
| 227 |
+
|----------|-------------|
|
| 228 |
+
| Faster Analysis | Reduce manual document review time |
|
| 229 |
+
| Accurate Retrieval | Retrieve the most relevant financial information |
|
| 230 |
+
| Context-Aware Answers | Grounded responses from document content |
|
| 231 |
+
| Scalable Architecture | Handles large financial reports efficiently |
|
| 232 |
+
| Cost Effective | Uses lightweight MiniCPM models |
|
| 233 |
+
|
| 234 |
+
---
|
| 235 |
+
|
app.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_pdf(pdf):
|
| 16 |
+
|
| 17 |
+
if pdf is None:
|
| 18 |
+
return "β Please upload a PDF"
|
| 19 |
+
|
| 20 |
+
with open(pdf.name, "rb") as f:
|
| 21 |
+
pdf_bytes = f.read()
|
| 22 |
+
|
| 23 |
+
r = requests.post(
|
| 24 |
+
"https://gajanand1902--minicpm-rag-upload-pdf.modal.run",
|
| 25 |
+
json={"pdf_bytes": list(pdf_bytes)}
|
| 26 |
+
)
|
| 27 |
+
data = r.json()
|
| 28 |
+
|
| 29 |
+
if isinstance(data, dict):
|
| 30 |
+
return str(data)
|
| 31 |
+
|
| 32 |
+
return data
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def chat(message, history):
|
| 36 |
+
r = requests.post(
|
| 37 |
+
"https://gajanand1902--minicpm-rag-chat-api.modal.run",
|
| 38 |
+
json={"question": message}
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
data = r.json()
|
| 42 |
+
|
| 43 |
+
if isinstance(data, dict):
|
| 44 |
+
return data.get("answer", str(data))
|
| 45 |
+
|
| 46 |
+
return str(data)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Custom Theme
|
| 50 |
+
theme = gr.themes.Soft(
|
| 51 |
+
primary_hue="blue",
|
| 52 |
+
secondary_hue="cyan",
|
| 53 |
+
neutral_hue="slate"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
css = """
|
| 57 |
+
.gradio-container {
|
| 58 |
+
max-width: none !important;
|
| 59 |
+
width: 100% !important;
|
| 60 |
+
margin: 0 !important;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.upload-card {
|
| 64 |
+
width: 100% !important;
|
| 65 |
+
max-width: none !important;
|
| 66 |
+
padding: 20px;
|
| 67 |
+
border-radius: 15px;
|
| 68 |
+
background: #f8fafc;
|
| 69 |
+
border: 1px solid #e5e7eb;
|
| 70 |
+
}
|
| 71 |
+
padding: 20px;
|
| 72 |
+
border-radius: 15px;
|
| 73 |
+
background: #f8fafc;
|
| 74 |
+
border: 1px solid #e5e7eb;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.footer {
|
| 78 |
+
text-align:center;
|
| 79 |
+
color:gray;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
button {
|
| 83 |
+
border-radius: 10px !important;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.chatbot {
|
| 87 |
+
border-radius: 15px !important;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.chat-section textarea {
|
| 91 |
+
background: #ffffff !important;
|
| 92 |
+
border: 2px solid #2563eb !important;
|
| 93 |
+
border-radius: 16px !important;
|
| 94 |
+
padding: 14px !important;
|
| 95 |
+
font-size: 15px !important;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
.chat-section textarea:focus {
|
| 99 |
+
border-color: #06b6d4 !important;
|
| 100 |
+
box-shadow: 0 0 10px rgba(6,182,212,0.4) !important;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
.chat-section textarea::placeholder {
|
| 104 |
+
color: #64748b !important;
|
| 105 |
+
opacity: 1 !important;
|
| 106 |
+
font-weight: 500;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
with gr.Blocks(
|
| 112 |
+
title="π MiniCPM Financial RAG",
|
| 113 |
+
theme=theme,
|
| 114 |
+
css=css
|
| 115 |
+
) as demo:
|
| 116 |
+
# Header
|
| 117 |
+
gr.HTML("""
|
| 118 |
+
<div style="
|
| 119 |
+
text-align:center;
|
| 120 |
+
padding:20px;
|
| 121 |
+
background:linear-gradient(90deg,#2563eb,#06b6d4);
|
| 122 |
+
color:white;
|
| 123 |
+
border-radius:15px;
|
| 124 |
+
margin-bottom:20px;"
|
| 125 |
+
>
|
| 126 |
+
</div>
|
| 127 |
+
""")
|
| 128 |
+
# Layout: left upload, right chat
|
| 129 |
+
with gr.Row():
|
| 130 |
+
# Left side (smaller header + upload)
|
| 131 |
+
with gr.Column(scale=1):
|
| 132 |
+
# Small header
|
| 133 |
+
gr.HTML("""
|
| 134 |
+
<div class="header" style="text-align:center;padding:8px;background:linear-gradient(90deg,#2563eb,#06b6d4);color:white;border-radius:12px;margin-bottom:8px;">
|
| 135 |
+
<h1 style="font-size:1.2rem;margin:0;">π MiniCPM Financial QA RAG</h1>
|
| 136 |
+
<p style="font-size:0.9rem;margin:0;">Upload a Financial PDF and Chat with it using AI</p>
|
| 137 |
+
</div>
|
| 138 |
+
""")
|
| 139 |
+
with gr.Group(elem_classes="upload-card"):
|
| 140 |
+
gr.Markdown("### π Upload Document")
|
| 141 |
+
pdf = gr.File(label="Choose PDF", file_types=[".pdf"])
|
| 142 |
+
upload_btn = gr.Button("π Process PDF", variant="primary")
|
| 143 |
+
status = gr.Textbox(label="π Status", interactive=False)
|
| 144 |
+
upload_btn.click(fn=upload_pdf, inputs=pdf, outputs=status)
|
| 145 |
+
# Right side (chat with scrollbar)
|
| 146 |
+
with gr.Column(scale=3):
|
| 147 |
+
gr.Markdown("### π¬ Ask Questions")
|
| 148 |
+
|
| 149 |
+
with gr.Group(elem_classes="chat-section"):
|
| 150 |
+
|
| 151 |
+
gr.ChatInterface(
|
| 152 |
+
fn=chat,
|
| 153 |
+
chatbot=gr.Chatbot(
|
| 154 |
+
height=600,
|
| 155 |
+
show_label=False
|
| 156 |
+
),
|
| 157 |
+
textbox=gr.Textbox(
|
| 158 |
+
placeholder="π€ Ask a question about your financial report...",
|
| 159 |
+
container=False,
|
| 160 |
+
scale=7
|
| 161 |
+
)
|
| 162 |
+
)
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.34.0
|
| 2 |
+
modal
|
| 3 |
+
requests
|
| 4 |
+
|