File size: 12,842 Bytes
b17d9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f17a4c9
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import streamlit as st
from groq import Groq
import io
import base64
import re
import os
from dotenv import load_dotenv
from llama_index.core import VectorStoreIndex, Settings, Document
from llama_index.readers.file import PDFReader
from llama_index.llms.groq import Groq as LlamaGroq
from llama_index.embeddings.langchain import LangchainEmbedding
from langchain_community.embeddings import HuggingFaceEmbeddings
from datetime import datetime
from PIL import Image
import gettext

# Load environment variables and configure
load_dotenv()
groq_api_key = os.getenv("GROQ_API_KEY")
client = Groq(api_key=groq_api_key)

# Configure LlamaIndex
Settings.llm = LlamaGroq(api_key=groq_api_key, model="llama-3.1-70b-versatile")
lc_embed_model = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-mpnet-base-v2"
)
Settings.embed_model = LangchainEmbedding(lc_embed_model)

def initialize_session_state():
    """Initialize all session state variables"""
    if 'chat_engines' not in st.session_state:
        st.session_state.chat_engines = {}
    if 'analyses' not in st.session_state:
        st.session_state.analyses = {}
    if 'documents' not in st.session_state:
        st.session_state.documents = {}
    if 'current_doc' not in st.session_state:
        st.session_state.current_doc = None
    if 'messages' not in st.session_state:
        st.session_state.messages = []
    if 'document_history' not in st.session_state:
        st.session_state.document_history = {}

def encode_image_to_base64(image):
    """Convert PIL Image to base64 string"""
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    return base64.b64encode(buffered.getvalue()).decode()

def process_image(image):
    """Process image using Llama vision model"""
    img_base64 = encode_image_to_base64(image)
    img_url = f"data:image/jpeg;base64,{img_base64}"
    
    completion = client.chat.completions.create(
        model="llama-3.2-11b-vision-preview",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": """Please analyze this government document and provide:
                        1. Document type and purpose
                        2. Key requirements and deadlines
                        3. Complex terms explained simply
                        4. Required actions or next steps
                        5. Important contact information or submission details"""
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": img_url
                        }
                    }
                ]
            }
        ],
        temperature=0.1,
        max_tokens=1024,
        top_p=1,
        stream=False
    )
    
    return completion.choices[0].message.content

def generate_pdf_analysis(documents):
    """Generate analysis from PDF documents using Groq"""
    try:
        # Combine all document content
        full_text = "\n".join([doc.text for doc in documents])
        
        # Generate analysis using Groq
        completion = client.chat.completions.create(
            model="llama-3.1-70b-versatile",
            messages=[
                {
                    "role": "user",
                    "content": (
                        "Please analyze this government document and provide:\n"
                        "1. Document Type and Purpose:\n"
                        "   - What kind of document is this?\n"
                        "   - What is its main purpose?\n\n"
                        "2. Key Requirements:\n"
                        "   - What are the main requirements or conditions?\n"
                        "   - What documents or information are needed?\n\n"
                        "3. Important Deadlines:\n"
                        "   - What are the key dates and deadlines?\n"
                        "   - Are there any time-sensitive requirements?\n\n"
                        "4. Complex Terms Explained:\n"
                        "   - Explain any technical or legal terms in simple language\n"
                        "   - Clarify any complex procedures\n\n"
                        "5. Required Actions:\n"
                        "   - What steps need to be taken?\n"
                        "   - What is the process to follow?\n\n"
                        "6. Contact Information:\n"
                        "   - Who to contact for queries?\n"
                        "   - Where to submit the documents?\n\n"
                        "Document content:\n" + full_text
                    )
                }
            ],
            temperature=0.1,
            max_tokens=2048,
            top_p=1
        )
        
        # Format the analysis with proper styling
        analysis = completion.choices[0].message.content

        completionsum = client.chat.completions.create(
            model="llama-3.1-8b-instant",
            messages=[
                {
                    "role": "user",
                    "content": (
                        "Summarize the following content: " + analysis
                    )
                }
            ],
            temperature=0.1,
            max_tokens=2048,
            top_p=1
        )

        analysissum = completionsum.choices[0].message.content
        
        return analysissum
    except Exception as e:
        error_msg = "Error generating PDF analysis: " + str(e)
        raise Exception(error_msg)

def clean_llm_output(output):
    """Clean LLM output by removing HTML tags and formatting symbols"""
    # Remove HTML tags
    cleaned_text = re.sub(r'<[^>]+>', '', output)
    # Remove double asterisks
    cleaned_text = cleaned_text.replace('**', '')
    cleaned_text = cleaned_text.replace('*', '')
    # Remove extra whitespace
    cleaned_text = re.sub(r'\s+', ' ', cleaned_text)
    return cleaned_text.strip()

def format_analysis_results(text):
    """Format analysis results into structured HTML"""
    # First clean the text
    cleaned_text = clean_llm_output(text)
    
    # Split into sections
    sections = []
    current_section = ""
    current_title = ""
    
    for line in cleaned_text.split('\n'):
        line = line.strip()
        if ':' in line and not line.startswith('*'):
            # If we have a previous section, save it
            if current_title:
                sections.append((current_title, current_section.strip()))
            # Start new section
            parts = line.split(':', 1)
            current_title = parts[0].strip()
            current_section = parts[1].strip() if len(parts) > 1 else ""
        else:
            current_section += " " + line
    
    # Add the last section
    if current_title:
        sections.append((current_title, current_section.strip()))
    
    # Generate HTML
    html = "<div class='analysis-results'>"
    for title, content in sections:
        html += f"""
            <div class='analysis-section card' style='margin-bottom: 1rem;'>
                <h4 style='color: #60A5FA; margin-bottom: 0.5rem;'>{title}</h4>
                <p style='margin: 0;'>{content}</p>
            </div>
        """
    html += "</div>"
    
    return html

def process_captured_image(picture):
    """Process image captured from camera with mobile-friendly UI"""
    try:
        # Show processing status
        status_placeholder = st.empty()
        status_placeholder.markdown(
            "<div class='status-badge status-warning'>"
            "๐Ÿ“ธ Processing captured image..."
            "</div>",
            unsafe_allow_html=True
        )
        
        # Process the image
        image = Image.open(picture)
        
        # Display the captured image with proper mobile sizing
        st.image(
            image,
            caption="Captured Document",
            use_column_width=True  # Makes image responsive
        )
        
        # Process image with AI
        with st.spinner("Analyzing document..."):
            analysis = process_image(image)
        
        # Generate filename with timestamp
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"captured_image_{timestamp}"
        
        # Save results
        st.session_state.analyses[filename] = {
            'type': 'image/jpeg',
            'analysis': analysis,
            'timestamp': datetime.datetime.now()
        }
        
        # Create chat engine
        st.session_state.chat_engines[filename] = create_chat_engine(analysis)
        
        # Save to history
        save_to_history(
            filename,
            'Captured Image',
            analysis,
            datetime.datetime.now()
        )
        
        # Update status to success
        status_placeholder.markdown(
            "<div class='status-badge status-success'>"
            "โœ… Image analyzed successfully!"
            "</div>",
            unsafe_allow_html=True
        )
        
        # Display analysis results
        st.markdown(
            "<div class='card'>"
            "<h4>Analysis Results</h4>"
            f"<div style='margin: 1rem 0;'>{analysis}</div>"
            "</div>",
            unsafe_allow_html=True
        )
        
        # Mobile-friendly action buttons
        st.markdown("<div class='touch-spacing'>", unsafe_allow_html=True)
        
        col1, col2 = st.columns(2)
        with col1:
            if st.button("๐Ÿ’ฌ Start Chat", use_container_width=True):
                st.session_state.current_doc = filename
                st.switch_page("pages/2_๐Ÿ’ฌ_Document_Chat.py")
        with col2:
            if st.button("๐Ÿ“ธ New Capture", use_container_width=True):
                st.rerun()
                
        st.markdown("</div>", unsafe_allow_html=True)
        
    except Exception as e:
        st.error(
            "โŒ Error processing image\n"
            f"Details: {str(e)}"
        )

def process_pdf(pdf_file):
    """Process PDF document using LlamaIndex"""
    temp_dir = "temp_docs"
    os.makedirs(temp_dir, exist_ok=True)
    temp_path = os.path.join(temp_dir, "temp.pdf")
    
    with open(temp_path, "wb") as f:
        f.write(pdf_file.getvalue())
    
    try:
        reader = PDFReader()
        documents = reader.load_data(temp_path)
        return documents
    finally:
        if os.path.exists(temp_path):
            os.remove(temp_path)
        if os.path.exists(temp_dir) and not os.listdir(temp_dir):
            os.rmdir(temp_dir)

def create_chat_engine(content):
    """Create chat engine from document content"""
    if isinstance(content, str):
        documents = [Document(text=content)]
    else:
        documents = content
    
    index = VectorStoreIndex.from_documents(documents)
    return index.as_chat_engine(chat_mode="condense_question", verbose=True)

def generate_document(doc_type, fields):
    """Generate government documents based on type and fields"""
    prompt = f"""Generate a formal {doc_type} with the following details:
    
    {fields}
    
    Please format this as a proper official document following standard government formatting."""
    
    completion = client.chat.completions.create(
        model="llama-3.1-70b-versatile",
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ],
        temperature=0.7,
        max_tokens=2048,
        top_p=1
    )
    
    return completion.choices[0].message.content

def save_to_history(doc_name, doc_type, content, timestamp=None):
    """Save document to history with metadata"""
    if timestamp is None:
        timestamp = datetime.now()
    
    st.session_state.document_history[doc_name] = {
        'type': doc_type,
        'content': content,
        'timestamp': timestamp,
        'status': 'Processed'
    }

def get_document_history():
    """Retrieve document history sorted by timestamp"""
    history = st.session_state.document_history
    return dict(sorted(
        history.items(),
        key=lambda x: x[1]['timestamp'],
        reverse=True
    ))

def delete_from_history(doc_name):
    """Delete document from history"""
    if doc_name in st.session_state.document_history:
        del st.session_state.document_history[doc_name]
        if doc_name in st.session_state.chat_engines:
            del st.session_state.chat_engines[doc_name]
        if doc_name in st.session_state.analyses:
            del st.session_state.analyses[doc_name]
        if st.session_state.current_doc == doc_name:
            st.session_state.current_doc = None

def format_timestamp(timestamp):
    """Format timestamp for display"""
    return timestamp.strftime("%Y-%m-%d %H:%M:%S")