File size: 14,110 Bytes
dca1254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from streamlit_chat import message
from langchain.chains import ConversationalRetrievalChain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import PromptTemplate
from langchain_community.vectorstores import FAISS
import pdfplumber
import docx2txt
from langchain_community.embeddings import OllamaEmbeddings
from langchain_groq import ChatGroq
from dotenv import load_dotenv
from easygoogletranslate import EasyGoogleTranslate
import os
import csv
import re
from io import StringIO
import speech_recognition as sr
import pygame
from threading import Thread
from gtts import gTTS
import gc
import torch

os.environ['CUDA_VISIBLE_DEVICES'] = ''
torch.set_num_threads(1)

load_dotenv()
groq_api_key = os.getenv('GROQ_API_KEY')

MAX_DOCUMENTS = 5

def initialize_session_state():
    if 'history' not in st.session_state:
        st.session_state['history'] = []
    if 'generated' not in st.session_state:
        st.session_state['generated'] = ["Hello! Ask me anything about πŸ€—"]
    if 'past' not in st.session_state:
        st.session_state['past'] = ["Hey! πŸ‘‹"]
    if 'translated' not in st.session_state:
        st.session_state['translated'] = ["Hello! Ask me anything about πŸ€—"]
    if 'translation_requested' not in st.session_state:
        st.session_state['translation_requested'] = [False] * len(st.session_state['generated'])
    if 'chain' not in st.session_state:
        st.session_state['chain'] = None
    if 'vector_store' not in st.session_state:
        st.session_state['vector_store'] = None

def translate_text(text, target_language='en'):
    translator = EasyGoogleTranslate(target_language=target_language)
    try:
        return translator.translate(text)
    except Exception as e:
        st.error(f"Translation error: {e}")
        return text

def clean_text_for_speech(text):
    # Replacing symbols and formatting text
    text = re.sub(r'[*_~#|‒●■◆β–ͺ]', '', text)
    text = re.sub(r'\n', ' ', text)
    text = re.sub(r'\s+', ' ', text)
    text = re.sub(r'([.!?])\s*', r'\1 ', text)
    text = re.sub(r'[:;]', ' ', text)
    text = re.sub(r'[-]', ' ', text)
    text = re.sub(r'[(){}\[\]]', '', text)

    # Handle numbers and decimals
    text = re.sub(r'(\d+)\.(\d+)', r'\1 point \2', text)
    
    # Make sure to handle numbers correctly
    replacements = {
        '&': 'and', '%': 'percent', '$': 'dollars', '€': 'euros', 'Β£': 'pounds',
        '@': 'at', '#': 'hashtag', 'e.g.': 'for example', 'i.e.': 'that is',
        'etc.': 'et cetera', 'vs.': 'versus', 'fig.': 'figure', 'approx.': 'approximately',
    }
    for key, value in replacements.items():
        text = text.replace(key, value)
    
    return text.strip()


def text_to_speech(text, language='en', speed=1.0):
    cleaned_text = clean_text_for_speech(text)
    tts = gTTS(text=cleaned_text, lang=language, slow=(speed < 1.0))
    tts.save("output.mp3")
    with open("output.mp3", "rb") as audio_file:
        audio_bytes = audio_file.read()
    return audio_bytes



def conversation_chat(query, chain, history):
    template = """

    You are an expert analyst with deep knowledge across various fields. Your task is to provide an in-depth, comprehensive analysis of the uploaded documents. Approach each question with critical thinking and attention to detail.

    You are only allowed to answer questions directly related to the content of the uploaded documents. 

    If a question is outside the scope of the documents, respond with: 'I'm sorry, I can only answer questions about the uploaded documents.'



    Guidelines for Analysis:



    1. Document Overview:

       - Identify the type of document(s) (research paper, report, data set, etc.)

       - Summarize the main topic and purpose of each document



    2. Content Analysis:

       - For research papers: Analyze the abstract, introduction, methodology, results, discussion, and conclusion

       - For reports: Examine executive summary, key findings, and recommendations

       - For data sets: Describe the structure, variables, and any apparent trends



    3. Key Points and Findings:

       - Highlight the most significant information and insights from each document

       - Identify any unique or surprising elements in the content



    4. Contextual Analysis:

       - Place the information in a broader context within its field

       - Discuss how this information relates to current trends or issues



    5. Critical Evaluation:

       - Assess the strengths and limitations of the presented information

       - Identify any potential biases or gaps in the data or arguments



    6. Implications and Applications:

       - Discuss the potential impact of the findings or information

       - Suggest possible applications or areas for further research



    7. Comparative Analysis (if multiple documents):

       - Compare and contrast information across different documents

       - Identify any conflicting data or viewpoints



    8. Data Interpretation:

       - For numerical data: Provide clear explanations of statistics or trends

       - For qualitative information: Offer interpretations of key quotes or concepts



    9. Sourcing and Credibility:

       - Comment on the credibility of the sources (if apparent)

       - Note any references to other important works in the field



    10. Comprehensive Response:

        - Ensure all aspects of the question are addressed

        - Provide a balanced view, considering multiple perspectives if applicable



    Remember to maintain an objective, analytical tone. Your goal is to provide the most thorough and insightful analysis possible based on the available documents.



    Previous Context: {previous_context}



    Question: {query}

    """
    prompt = PromptTemplate.from_template(template)
    
    result = chain.invoke({"question": query, "chat_history": history}, prompt=prompt)
    
    answer = result.get("answer", "I'm sorry, I couldn't generate an answer.")
    history.append((query, answer))
    return answer

def display_chat_history(chain):
    st.write("Chat History:")

    for i in range(len(st.session_state['past'])):
        message(st.session_state['past'][i], is_user=True, key=f'{i}_user', avatar_style="avataaars", seed="Aneka")
        message(st.session_state['generated'][i], key=f'{i}_bot', avatar_style="bottts", seed="Aneka")
        
        col1, col2, col3 = st.columns([2, 1, 1])
        with col1:
            dest_language = st.selectbox('Select language for translation:', 
                                         options=['hi', 'kn'], 
                                         index=0, 
                                         key=f'{i}_lang_select')
        with col2:
            if st.button(f'Translate Message {i}', key=f'{i}_translate'):
                translated_text = translate_text(st.session_state['generated'][i], target_language=dest_language)
                st.session_state['translated'][i] = translated_text
                st.session_state['translation_requested'][i] = True
                st.experimental_rerun()
        with col3:
            if st.button(f'Play Message {i}', key=f'{i}_play'):
                audio_bytes = text_to_speech(st.session_state['generated'][i])
                st.audio(audio_bytes, format="audio/mp3")

        if st.session_state['translation_requested'][i]:
            message(st.session_state['translated'][i], key=f'{i}_bot_translated', avatar_style="bottts", seed="Aneka")
            if st.button(f'Play Translated Message {i}', key=f'{i}_play_translated'):
                audio_bytes = text_to_speech(st.session_state['translated'][i], dest_language)
                st.audio(audio_bytes, format="audio/mp3")

    with st.form(key='user_input_form'):
        user_input = st.text_input("Ask questions about your uploaded documents:", key="user_input")
        submit_button = st.form_submit_button(label='Send')

    if submit_button and user_input:
        output = conversation_chat(user_input, chain, st.session_state['history'])
        st.session_state['past'].append(user_input)
        st.session_state['generated'].append(output)
        st.session_state['translated'].append(output)
        st.session_state['translation_requested'].append(False)
        st.rerun()


def process_file(file):
    if file.type == "application/pdf":
        return process_pdf(file)
    elif file.type == "text/plain":
        return file.getvalue().decode("utf-8")
    elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        return docx2txt.process(file)
    elif file.type == "text/csv":
        return process_csv(file)
    else:
        st.error(f"Unsupported file type: {file.type}")
        return ""

def process_csv(file):
    text = ""
    try:
        file_content = file.getvalue().decode('utf-8')
        csvfile = StringIO(file_content)
        reader = csv.reader(csvfile)
        headers = next(reader, None)
        if headers:
            text += f"CSV Headers: {', '.join(headers)}\n\n"
        for i, row in enumerate(reader, 1):
            text += f"Row {i}: {' | '.join(row)}\n"
        text += f"\nTotal rows: {i}\n"
    except Exception as e:
        st.error(f"Error reading CSV file: {e}")
    return text

def process_pdf(file):
    text = ""
    with pdfplumber.open(file) as pdf:
        for page_num, page in enumerate(pdf.pages, 1):
            page_text = page.extract_text()
            if page_text:
                text += f"[Page {page_num}]\n{page_text}\n\n"
    
    sections = re.findall(r'(?i)(abstract|introduction|methodology|results|discussion|conclusion).*?\n(.*?)(?=\n(?i)(abstract|introduction|methodology|results|discussion|conclusion)|$)', text, re.DOTALL)
    structured_text = "\n\n".join([f"{section[0].capitalize()}:\n{section[1]}" for section in sections])
    
    return structured_text if structured_text else text

def recognize_speech():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        st.write("Listening... Please speak now.")
        try:
            st.info("Listening for up to 10 seconds...")
            recognizer.adjust_for_ambient_noise(source, duration=1)
            audio = recognizer.listen(source, timeout=10, phrase_time_limit=5)
            st.success("Audio captured. Processing...")
        except sr.WaitTimeoutError:
            st.warning("No speech detected. Please try again.")
            return ""
    
    try:
        text = recognizer.recognize_google(audio)
        st.success(f"You said: {text}")
        return text
    except sr.UnknownValueError:
        st.error("Sorry, I couldn't understand that.")
        return ""
    except sr.RequestError as e:
        st.error(f"Could not request results; {e}")
        return ""

def create_conversational_chain(vector_store):
    llm = ChatGroq(groq_api_key=groq_api_key, model_name='llama3-70b-8192')
    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    chain = ConversationalRetrievalChain.from_llm(
        llm=llm, 
        chain_type='stuff',
        retriever=vector_store.as_retriever(search_kwargs={"k": 5}),
        memory=memory
    )
    return chain

def main():
    initialize_session_state()
    st.set_page_config(page_title="DOCS Chatbot & Translator", layout="wide")
    st.title("Smart Document Tool πŸ€“")
    
    st.sidebar.header("About App:")
    st.sidebar.write("This app utilizes Streamlit")
    
    uploaded_files = st.file_uploader("Upload your Docs", type=["pdf", "txt", "docx", "csv"], accept_multiple_files=True)

    if uploaded_files:
        all_text = ""
        for uploaded_file in uploaded_files[:MAX_DOCUMENTS]:
            try:
                all_text += f"File: {uploaded_file.name}\n\n{process_file(uploaded_file)}\n\n"
            except Exception as e:
                st.error(f"Error processing file {uploaded_file.name}: {e}")
            finally:
                gc.collect()
        
        if len(uploaded_files) > MAX_DOCUMENTS:
            st.warning(f"Only the first {MAX_DOCUMENTS} documents were processed due to memory constraints.")

        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=4000,
            chunk_overlap=300,
            length_function=len,
            separators=["\n\n", "\n", " ", ""]
        )
        text_chunks = text_splitter.split_text(all_text)

        embeddings = OllamaEmbeddings(model="nomic-embed-text")

        with st.spinner('Analyzing Document...'):
            vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
            st.session_state['vector_store'] = vector_store
            st.session_state['chain'] = create_conversational_chain(vector_store)
            display_chat_history(st.session_state['chain'])
        
        if st.button('Speak Now'):
            recognized_text = recognize_speech()
            if recognized_text:
                st.session_state['past'].append(recognized_text)
                output = conversation_chat(recognized_text, st.session_state['chain'], st.session_state['history'])
                st.session_state['generated'].append(output)
                st.session_state['translated'].append(output)
                st.session_state['translation_requested'].append(False)
                
                audio_bytes = text_to_speech(output)
                st.audio(audio_bytes, format="audio/mp3")
                
                st.rerun()
            else:
                st.warning("No speech input was processed. Please try speaking again.")

        gc.collect()

    st.sidebar. caption="Your AI Assistant"
if __name__ == "__main__":
    main()