File size: 6,177 Bytes
cc3810c
3db7fb5
9ab3d34
 
cc3810c
 
 
 
3db7fb5
cc3810c
9ab3d34
 
cc3810c
3db7fb5
9ab3d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc3810c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab3d34
cc3810c
 
 
 
 
499fe45
 
d2b99c8
cc3810c
 
 
 
 
 
 
 
 
9ab3d34
cc3810c
 
 
 
 
 
 
 
 
 
 
 
 
9ab3d34
cc3810c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119ac1f
cc3810c
 
 
 
 
 
 
 
 
 
9ab3d34
 
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
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModel
import torch
from transformers import pipeline
import wikipedia
import numpy as np
import faiss

# Initialize components
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
vector_store = None

# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0]
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def encode_text(texts):
    # Tokenize sentences
    encoded_input = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors='pt')
    
    # Compute token embeddings
    with torch.no_grad():
        model_output = model(**encoded_input)

    # Perform pooling
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
    
    return sentence_embeddings.numpy()

class VectorStore:
    def __init__(self, dimension: int):
        self.dimension = dimension
        self.index = faiss.IndexFlatL2(dimension)
        self.texts = []

    def add_texts(self, texts, embeddings):
        self.texts.extend(texts)
        self.index.add(embeddings)

    def search(self, query_embedding, k):
        query_embedding = query_embedding.reshape(1, -1)
        distances, indices = self.index.search(query_embedding, k)
        results = []
        for idx, distance in zip(indices[0], distances[0]):
            if idx < len(self.texts):
                results.append((self.texts[idx], float(distance)))
        return results

def get_wikipedia_content():
    try:
        topics = [
            'Artificial intelligence',
            'Machine learning',
            'Deep learning',
            'Natural language processing',
            'Computer vision'
        ]
        
        all_content = []
        for topic in topics:
            try:
                page = wikipedia.page(topic)
                all_content.append(page.content[:2000])
            except wikipedia.exceptions.DisambiguationError as e:
                try:
                    page = wikipedia.page(e.options[0])
                    all_content.append(page.content[:2000])
                except:
                    continue
            except:
                continue
                
        return '\n\n'.join(all_content)
    except Exception as e:
        return """
        Artificial Intelligence (AI) is the simulation of human intelligence processes by machines.
        Machine Learning is a subset of artificial intelligence that provides systems the ability to learn.
        Deep Learning is part of machine learning based on artificial neural networks.
        Natural Language Processing (NLP) helps computers understand human language.
        """

def chunk_text(text, chunk_size=300):
    sentences = [s.strip() for s in text.split('.') if s.strip()]
    chunks = []
    current_chunk = []
    current_length = 0
    
    for sentence in sentences:
        sentence_length = len(sentence)
        if current_length + sentence_length > chunk_size and current_chunk:
            chunks.append('. '.join(current_chunk) + '.')
            current_chunk = [sentence]
            current_length = sentence_length
        else:
            current_chunk.append(sentence)
            current_length += sentence_length
    
    if current_chunk:
        chunks.append('. '.join(current_chunk) + '.')
    
    return chunks

def init_vector_store():
    global vector_store
    text = get_wikipedia_content()
    chunks = chunk_text(text)
    embeddings = encode_text(chunks)
    vector_store = VectorStore(dimension=embeddings.shape[1])
    vector_store.add_texts(chunks, embeddings)
    return vector_store

def generate_answer(query: str, context: str) -> str:
    try:
        generator = pipeline('text-generation', model='gpt2')
        prompt = f"Context: {prompt}"
        response = generator(prompt, max_length=150, num_return_sequences=1)
        return response[0]['generated_text']
    except Exception as e:
        relevant_sentences = [s for s in context.split('.') if query.lower() in s.lower()]
        if relevant_sentences:
            return relevant_sentences[0] + '.'
        return "I apologize, but I couldn't generate a specific answer based on the available information."

def chat_function(message, history):
    query_embedding = encode_text([message])
    results = vector_store.search(query_embedding, k=3)
    context = ' '.join([text for text, _ in results])
    answer = generate_answer(message, context)
    return answer

def handle_file_upload(file):
    if file is None:
        return "No file uploaded"
    
    try:
        with open(file.name, 'r', encoding='utf-8') as f:
            content = f.read()
        chunks = chunk_text(content)
        embeddings = encode_text(chunks)
        vector_store.add_texts(chunks, embeddings)
        return f"File processed successfully. Added {len(chunks)} chunks to knowledge base."
    except Exception as e:
        return f"Error processing file: {str(e)}"

# Initialize vector store
print("Initializing vector store with Wikipedia content...")
init_vector_store()

# Create Gradio interface
demo = gr.Blocks()

with demo:
    gr.Markdown("# RAG Chatbot")
    
    with gr.Row():
        file_input = gr.File(label="Upload Document")
        upload_button = gr.Button("Process File")
    
    upload_output = gr.Textbox(label="Upload Status")
    
    chatbot = gr.ChatInterface(
        chat_function,
        examples=["What is artificial intelligence?", "Explain machine learning", "Deep learning","Natural language processing","Computer vision"],
        title="Chat with your documents"
    )
    
    upload_button.click(
        handle_file_upload,
        inputs=[file_input],
        outputs=[upload_output]
    )

# Launch for Hugging Face Spaces
if __name__ == "__main__":
    demo.launch()