Spaces:
Configuration error
Configuration error
Upload 4 files
Browse files- README.md +17 -13
- app.py +62 -64
- config.py +13 -0
- requirements.txt +7 -1
README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RAG Chatbot với Gemini
|
| 2 |
+
|
| 3 |
+
Chatbot sử dụng Retrieval-Augmented Generation (RAG) với Gemini API.
|
| 4 |
+
|
| 5 |
+
## Cài đặt
|
| 6 |
+
|
| 7 |
+
1. Clone repo
|
| 8 |
+
2. Cài đặt dependencies: `pip install -r requirements.txt`
|
| 9 |
+
3. Thêm Gemini API key vào file `.env`
|
| 10 |
+
4. Đặt dataset vào `data/your_dataset.txt`
|
| 11 |
+
5. Chạy: `python app.py`
|
| 12 |
+
|
| 13 |
+
## Deploy lên Hugging Face Spaces
|
| 14 |
+
|
| 15 |
+
1. Tạo Space mới trên Hugging Face
|
| 16 |
+
2. Upload tất cả files
|
| 17 |
+
3. Thêm secret GEMINI_API_KEY trong Settings
|
app.py
CHANGED
|
@@ -1,64 +1,62 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for
|
| 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 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from utils.embeddings import EmbeddingModel
|
| 4 |
+
from utils.vector_store import VectorStore
|
| 5 |
+
from utils.rag_chain import RAGChain
|
| 6 |
+
from config import CHUNK_SIZE, CHUNK_OVERLAP
|
| 7 |
+
|
| 8 |
+
# Initialize components
|
| 9 |
+
embedding_model = EmbeddingModel()
|
| 10 |
+
vector_store = VectorStore()
|
| 11 |
+
vector_store.create_collection()
|
| 12 |
+
|
| 13 |
+
def load_and_process_data(file_path):
|
| 14 |
+
"""Load và xử lý dataset"""
|
| 15 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 16 |
+
text = f.read()
|
| 17 |
+
|
| 18 |
+
# Chia thành chunks
|
| 19 |
+
chunks = []
|
| 20 |
+
for i in range(0, len(text), CHUNK_SIZE - CHUNK_OVERLAP):
|
| 21 |
+
chunk = text[i:i + CHUNK_SIZE]
|
| 22 |
+
chunks.append(chunk)
|
| 23 |
+
|
| 24 |
+
# Tạo embeddings
|
| 25 |
+
embeddings = embedding_model.embed_documents(chunks)
|
| 26 |
+
|
| 27 |
+
# Lưu vào vector store
|
| 28 |
+
vector_store.add_documents(chunks, embeddings)
|
| 29 |
+
|
| 30 |
+
return len(chunks)
|
| 31 |
+
|
| 32 |
+
# Load data khi khởi động
|
| 33 |
+
if os.path.exists("data/your_dataset.txt"):
|
| 34 |
+
num_chunks = load_and_process_data("data/your_dataset.txt")
|
| 35 |
+
print(f"Đã load {num_chunks} chunks")
|
| 36 |
+
|
| 37 |
+
# Initialize RAG chain
|
| 38 |
+
rag_chain = RAGChain(vector_store, embedding_model)
|
| 39 |
+
|
| 40 |
+
def chatbot_response(message, history):
|
| 41 |
+
"""Xử lý tin nhắn và trả về response"""
|
| 42 |
+
try:
|
| 43 |
+
response = rag_chain.get_answer(message)
|
| 44 |
+
return response
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Lỗi: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# Tạo Gradio interface
|
| 49 |
+
demo = gr.ChatInterface(
|
| 50 |
+
fn=chatbot_response,
|
| 51 |
+
title="RAG Chatbot với Gemini",
|
| 52 |
+
description="Chatbot sử dụng RAG (Retrieval-Augmented Generation) với Gemini API",
|
| 53 |
+
examples=[
|
| 54 |
+
"Xin chào!",
|
| 55 |
+
"Hãy giải thích về RAG",
|
| 56 |
+
"Thông tin trong dataset là gì?"
|
| 57 |
+
],
|
| 58 |
+
theme="soft"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|
|
|
|
|
|
config.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
load_dotenv()
|
| 5 |
+
|
| 6 |
+
# Gemini API Key (lấy free tại https://makersuite.google.com/app/apikey)
|
| 7 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "your-api-key-here")
|
| 8 |
+
|
| 9 |
+
# Model settings
|
| 10 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 11 |
+
GEMINI_MODEL = "gemini-pro"
|
| 12 |
+
CHUNK_SIZE = 500
|
| 13 |
+
CHUNK_OVERLAP = 50
|
requirements.txt
CHANGED
|
@@ -1 +1,7 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.16.0
|
| 2 |
+
google-generativeai==0.3.2
|
| 3 |
+
langchain==0.1.0
|
| 4 |
+
langchain-google-genai==0.0.5
|
| 5 |
+
chromadb==0.4.22
|
| 6 |
+
sentence-transformers==2.2.2
|
| 7 |
+
python-dotenv==1.0.0
|