File size: 37,916 Bytes
c99406b
59177aa
c99406b
 
1df6810
e41b021
9a48a94
1df6810
59177aa
 
79f2daf
59177aa
9a48a94
59177aa
fe49aa3
 
9a48a94
79f2daf
eab896a
 
 
 
 
 
 
5eb15fa
 
eab896a
59177aa
 
eab896a
 
a589da1
 
eab896a
 
 
 
 
5eb15fa
 
59177aa
c99406b
 
 
9a48a94
71c328b
 
59177aa
9a48a94
71c328b
9a48a94
 
71c328b
 
 
 
 
 
eab896a
 
59177aa
eab896a
 
59177aa
 
 
eab896a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb15fa
3f26784
eab896a
 
 
 
 
 
 
 
 
5eb15fa
eab896a
 
 
 
 
 
 
5eb15fa
eab896a
 
 
 
 
 
 
5eb15fa
eab896a
 
 
 
 
 
 
79f2daf
5eb15fa
59177aa
eab896a
 
 
 
 
 
5eb15fa
59177aa
 
4f228ef
59177aa
eab896a
 
 
 
 
 
 
4f228ef
59177aa
 
 
eab896a
 
 
 
 
 
 
 
59177aa
 
eab896a
 
 
59177aa
eab896a
59177aa
eab896a
 
 
59177aa
eab896a
4f228ef
 
59177aa
eab896a
59177aa
 
 
eab896a
 
 
 
 
 
 
 
 
59177aa
5eb15fa
79f2daf
59177aa
 
 
fe49aa3
59177aa
 
fe49aa3
 
59177aa
fe49aa3
 
 
 
 
 
59177aa
 
 
5eb15fa
 
 
 
59177aa
 
 
5eb15fa
 
59177aa
 
 
5eb15fa
59177aa
 
 
5eb15fa
 
59177aa
5eb15fa
59177aa
5eb15fa
 
59177aa
 
5eb15fa
59177aa
5eb15fa
59177aa
 
5eb15fa
59177aa
 
fe49aa3
c99406b
59177aa
fe49aa3
59177aa
eab896a
59177aa
 
 
 
 
 
fe49aa3
 
59177aa
fe49aa3
4f228ef
5eb15fa
59177aa
5eb15fa
59177aa
eab896a
5eb15fa
 
59177aa
5eb15fa
 
 
fe49aa3
59177aa
c99406b
fe49aa3
59177aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eab896a
59177aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eab896a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c99406b
59177aa
 
 
79f2daf
c99406b
 
 
 
fe49aa3
59177aa
 
fe49aa3
79f2daf
59177aa
 
79f2daf
eab896a
59177aa
eab896a
59177aa
9a48a94
e41b021
79f2daf
e41b021
eab896a
e41b021
 
eab896a
9a48a94
 
e41b021
9a48a94
79f2daf
e41b021
 
9a48a94
 
c99406b
79f2daf
eab896a
59177aa
c99406b
3f26784
c99406b
59177aa
 
 
1df6810
c99406b
eab896a
 
1df6810
59177aa
6db8a66
24cd13f
6db8a66
 
 
 
 
 
59177aa
 
eab896a
 
 
 
fe49aa3
59177aa
fe49aa3
eab896a
5eb15fa
eab896a
59177aa
eab896a
 
 
 
59177aa
5eb15fa
59177aa
eab896a
 
 
 
59177aa
5eb15fa
eab896a
 
59177aa
eab896a
 
 
 
 
 
a589da1
59177aa
 
 
 
 
 
5eb15fa
59177aa
eab896a
c99406b
eab896a
 
59177aa
 
 
 
eab896a
 
 
 
 
 
 
 
 
 
59177aa
eab896a
c99406b
eab896a
c99406b
 
59177aa
 
c99406b
 
 
59177aa
c99406b
59177aa
 
 
 
 
 
 
eab896a
59177aa
 
fe49aa3
59177aa
eab896a
59177aa
eab896a
59177aa
 
 
 
 
 
 
eab896a
59177aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe49aa3
 
eab896a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1df6810
59177aa
eab896a
 
 
8761807
 
 
 
 
62d2b15
eab896a
59177aa
62d2b15
eab896a
9a48a94
eab896a
 
75e05d9
59177aa
eab896a
59177aa
62d2b15
59177aa
24cd13f
 
59177aa
 
 
4f228ef
 
e41b021
eab896a
24cd13f
e41b021
9a48a94
59177aa
 
e41b021
59177aa
 
e41b021
59177aa
e41b021
 
eab896a
59177aa
 
 
 
e41b021
 
59177aa
 
eab896a
 
 
 
 
59177aa
eab896a
4f228ef
eab896a
 
59177aa
 
4f228ef
 
e41b021
eab896a
e41b021
 
 
 
 
 
eab896a
e41b021
 
 
 
 
 
59177aa
e41b021
4f228ef
eab896a
 
 
 
 
 
 
 
 
 
 
e41b021
 
59177aa
24cd13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eab896a
24cd13f
 
eab896a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24cd13f
 
 
 
 
eab896a
59177aa
eab896a
24cd13f
eab896a
71c328b
eab896a
71c328b
 
eab896a
71c328b
 
 
eab896a
59177aa
 
24cd13f
eab896a
71c328b
 
 
24cd13f
eab896a
71c328b
 
 
 
eab896a
 
71c328b
 
59177aa
 
4f228ef
 
eab896a
4f228ef
59177aa
4f228ef
59177aa
 
4f228ef
 
59177aa
e41b021
59177aa
 
1df6810
4f52bbc
59177aa
 
eab896a
1df6810
 
e41b021
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# ──────────────────────────────── Imports ────────────────────────────────
import os, json, re, logging, requests, markdown, time, io
from datetime import datetime

import streamlit as st
from openai import OpenAI  # OpenAI 라이브러리

from gradio_client import Client
import pandas as pd
import PyPDF2  # For handling PDF files

# ──────────────────────────────── Environment Variables / Constants ─────────────────────────
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")  
BRAVE_KEY      = os.getenv("SERPHOUSE_API_KEY", "")  # Keep this name
BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"
IMAGE_API_URL  = "http://211.233.58.201:7896"
MAX_TOKENS     = 7999

# Search modes and style definitions (in English)
SEARCH_MODES = {
    "comprehensive": "Comprehensive answer with multiple sources",
    "academic": "Academic and research-focused results",
    "news": "Latest news and current events",
    "technical": "Technical and specialized information",
    "educational": "Educational and learning resources"
}

RESPONSE_STYLES = {
    "professional": "Professional and formal tone",
    "casual": "Friendly and conversational tone",
    "simple": "Simple and easy to understand",
    "detailed": "Detailed and thorough explanations"
}

# Example search queries
EXAMPLE_QUERIES = {
    "example1": "What are the latest developments in quantum computing?",
    "example2": "How does climate change affect biodiversity in tropical rainforests?",
    "example3": "What are the economic implications of artificial intelligence in the job market?"
}

# ──────────────────────────────── Logging ────────────────────────────────
logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s - %(levelname)s - %(message)s")

# ──────────────────────────────── OpenAI Client ──────────────────────────

# OpenAI ν΄λΌμ΄μ–ΈνŠΈμ— νƒ€μž„μ•„μ›ƒκ³Ό μž¬μ‹œλ„ 둜직 μΆ”κ°€
@st.cache_resource
def get_openai_client():
    """Create an OpenAI client with timeout and retry settings."""
    if not OPENAI_API_KEY:
        raise RuntimeError("⚠️ OPENAI_API_KEY ν™˜κ²½ λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
    return OpenAI(
        api_key=OPENAI_API_KEY,
        timeout=60.0,  # νƒ€μž„μ•„μ›ƒ 60초둜 μ„€μ •
        max_retries=3  # μž¬μ‹œλ„ 횟수 3회둜 μ„€μ •
    )
    
# ──────────────────────────────── System Prompt ─────────────────────────
def get_system_prompt(mode="comprehensive", style="professional", include_search_results=True, include_uploaded_files=False) -> str:
    """
    Generate a system prompt for the perplexity-like interface based on:
    - The selected search mode and style
    - Guidelines for using web search results and uploaded files
    """

    # Base prompt for comprehensive mode
    comprehensive_prompt = """
You are an advanced AI assistant that provides comprehensive answers with multiple sources, similar to Perplexity.

Your task is to:
1. Thoroughly analyze the user's query
2. Provide a clear, well-structured answer integrating information from multiple sources
3. Include relevant images, videos, and links in your response
4. Format your answer with proper headings, bullet points, and sections
5. Cite sources inline and provide a references section at the end

Important guidelines:
- Organize information logically with clear section headings
- Use bullet points and numbered lists for clarity
- Include specific, factual information whenever possible
- Provide balanced perspectives on controversial topics
- Display relevant statistics, data, or quotes when appropriate
- Format your response using markdown for readability
"""

    # Alternative modes
    mode_prompts = {
        "academic": """
Your focus is on providing academic and research-focused responses:
- Prioritize peer-reviewed research and academic sources
- Include citations in a formal academic format
- Discuss methodologies and research limitations where relevant
- Present different scholarly perspectives on the topic
- Use precise, technical language appropriate for an academic audience
""",
        "news": """
Your focus is on providing the latest news and current events:
- Prioritize recent news articles and current information
- Include publication dates for all news sources
- Present multiple perspectives from different news outlets
- Distinguish between facts and opinions/editorial content
- Update information with the most recent developments
""",
        "technical": """
Your focus is on providing technical and specialized information:
- Use precise technical terminology appropriate to the field
- Include code snippets, formulas, or technical diagrams where relevant
- Break down complex concepts into step-by-step explanations
- Reference technical documentation, standards, and best practices
- Consider different technical approaches or methodologies
""",
        "educational": """
Your focus is on providing educational and learning resources:
- Structure information in a learning-friendly progression
- Include examples, analogies, and visual explanations
- Highlight key concepts and definitions
- Suggest further learning resources at different difficulty levels
- Present information that's accessible to learners at various levels
"""
    }

    # Response styles
    style_guides = {
        "professional": "Use a professional, authoritative voice. Clearly explain technical terms and present data systematically.",
        "casual": "Use a relaxed, conversational style with a friendly tone. Include relatable examples and occasionally use informal expressions.",
        "simple": "Use straightforward language and avoid jargon. Keep sentences and paragraphs short. Explain concepts as if to someone with no background in the subject.",
        "detailed": "Provide thorough explanations with comprehensive background information. Explore nuances and edge cases. Present multiple perspectives and detailed analysis."
    }

    # Guidelines for using search results
    search_guide = """
Guidelines for Using Search Results:
- Include source links directly in your response using markdown: [Source Name](URL)
- For each major claim or piece of information, indicate its source
- If sources conflict, explain the different perspectives and their reliability
- Include 3-5 relevant images by writing: ![Image description](image_url)
- Include 1-2 relevant video links when appropriate by writing: [Video: Title](video_url)
- Format search information into a cohesive, well-structured response
- Include a "References" section at the end listing all major sources with links
"""

    # Guidelines for using uploaded files
    upload_guide = """
Guidelines for Using Uploaded Files:
- Treat the uploaded files as primary sources for your response
- Extract and highlight key information from files that directly addresses the query
- Quote relevant passages and cite the specific file
- For numerical data in CSV files, consider creating summary statements
- For PDF content, reference specific sections or pages
- Integrate file information seamlessly with web search results
- When information conflicts, prioritize file content over general web results
"""

    # Choose base prompt based on mode
    if mode == "comprehensive":
        final_prompt = comprehensive_prompt
    else:
        final_prompt = comprehensive_prompt + "\n" + mode_prompts.get(mode, "")

    # Add style guide
    if style in style_guides:
        final_prompt += f"\n\nTone and Style: {style_guides[style]}"

    # Add search results guidance
    if include_search_results:
        final_prompt += f"\n\n{search_guide}"

    # Add uploaded files guidance
    if include_uploaded_files:
        final_prompt += f"\n\n{upload_guide}"

    # Additional formatting instructions
    final_prompt += """
\n\nAdditional Formatting Requirements:
- Use markdown headings (## and ###) to organize your response 
- Use bold text (**text**) for emphasis on important points
- Include a "Related Questions" section at the end with 3-5 follow-up questions
- Format your response with proper spacing and paragraph breaks
- Make all links clickable by using proper markdown format: [text](url)
"""

    return final_prompt

# ──────────────────────────────── Brave Search API ────────────────────────
@st.cache_data(ttl=3600)
def brave_search(query: str, count: int = 20):
    """
    Call the Brave Web Search API β†’ list[dict]
    Returns fields: index, title, link, snippet, displayed_link
    """
    if not BRAVE_KEY:
        raise RuntimeError("⚠️ SERPHOUSE_API_KEY (Brave API Key) environment variable is empty.")

    headers = {
        "Accept": "application/json",
        "Accept-Encoding": "gzip",
        "X-Subscription-Token": BRAVE_KEY
    }
    params = {"q": query, "count": str(count)}

    for attempt in range(3):
        try:
            r = requests.get(BRAVE_ENDPOINT, headers=headers, params=params, timeout=15)
            r.raise_for_status()
            data = r.json()

            logging.info(f"Brave search result data structure: {list(data.keys())}")

            raw = data.get("web", {}).get("results") or data.get("results", [])
            if not raw:
                logging.warning(f"No Brave search results found. Response: {data}")
                raise ValueError("No search results found.")
            
            arts = []
            for i, res in enumerate(raw[:count], 1):
                url = res.get("url", res.get("link", ""))
                host = re.sub(r"https?://(www\.)?", "", url).split("/")[0]
                arts.append({
                    "index": i,
                    "title": res.get("title", "No title"),
                    "link": url,
                    "snippet": res.get("description", res.get("text", "No snippet")),
                    "displayed_link": host
                })

            logging.info(f"Brave search success: {len(arts)} results")
            return arts

        except Exception as e:
            logging.error(f"Brave search failure (attempt {attempt+1}/3): {e}")
            if attempt < 2:
                time.sleep(2)

    return []

def mock_results(query: str) -> str:
    """Fallback search results if API fails"""
    ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return (f"# Fallback Search Content (Generated: {ts})\n\n"
            f"The search API request failed. Please generate a response based on any pre-existing knowledge about '{query}'.\n\n"
            f"You may consider the following points:\n\n"
            f"- Basic concepts and importance of {query}\n"
            f"- Commonly known related statistics or trends\n"
            f"- Typical expert opinions on this subject\n"
            f"- Questions that readers might have\n\n"
            f"Note: This is fallback guidance, not real-time data.\n\n")

def do_web_search(query: str) -> str:
    """Perform web search and format the results."""
    try:
        arts = brave_search(query, 20)
        if not arts:
            logging.warning("No search results, using fallback content")
            return mock_results(query)

        hdr = "# Web Search Results\nUse these results to provide a comprehensive answer with multiple sources. Include relevant images, videos, and links.\n\n"
        body = "\n".join(
            f"### Result {a['index']}: {a['title']}\n\n{a['snippet']}\n\n"
            f"**Source**: [{a['displayed_link']}]({a['link']})\n\n---\n"
            for a in arts
        )
        return hdr + body
    except Exception as e:
        logging.error(f"Web search process failed: {str(e)}")
        return mock_results(query)

# ──────────────────────────────── File Upload Handling ─────────────────────
def process_text_file(file):
    """Handle text file"""
    try:
        content = file.read()
        file.seek(0)

        text = content.decode('utf-8', errors='ignore')
        if len(text) > 10000:
            text = text[:9700] + "...(truncated)..."

        result = f"## Text File: {file.name}\n\n"
        result += text
        return result
    except Exception as e:
        logging.error(f"Error processing text file: {str(e)}")
        return f"Error processing text file: {str(e)}"

def process_csv_file(file):
    """Handle CSV file"""
    try:
        content = file.read()
        file.seek(0)

        df = pd.read_csv(io.BytesIO(content))
        result = f"## CSV File: {file.name}\n\n"
        result += f"- Rows: {len(df)}\n"
        result += f"- Columns: {len(df.columns)}\n"
        result += f"- Column Names: {', '.join(df.columns.tolist())}\n\n"

        result += "### Data Preview\n\n"
        preview_df = df.head(10)
        try:
            markdown_table = preview_df.to_markdown(index=False)
            if markdown_table:
                result += markdown_table + "\n\n"
            else:
                result += "Unable to display CSV data.\n\n"
        except Exception as e:
            logging.error(f"Markdown table conversion error: {e}")
            result += "Displaying data as text:\n\n"
            result += str(preview_df) + "\n\n"

        num_cols = df.select_dtypes(include=['number']).columns
        if len(num_cols) > 0:
            result += "### Basic Statistical Information\n\n"
            try:
                stats_df = df[num_cols].describe().round(2)
                stats_markdown = stats_df.to_markdown()
                if stats_markdown:
                    result += stats_markdown + "\n\n"
                else:
                    result += "Unable to display statistical information.\n\n"
            except Exception as e:
                logging.error(f"Statistical info conversion error: {e}")
                result += "Unable to generate statistical information.\n\n"

        return result
    except Exception as e:
        logging.error(f"CSV file processing error: {str(e)}")
        return f"Error processing CSV file: {str(e)}"

def process_pdf_file(file):
    """Handle PDF file"""
    try:
        # Read file in bytes
        file_bytes = file.read()
        file.seek(0)

        # Use PyPDF2
        pdf_file = io.BytesIO(file_bytes)
        reader = PyPDF2.PdfReader(pdf_file, strict=False)

        # Basic info
        result = f"## PDF File: {file.name}\n\n"
        result += f"- Total pages: {len(reader.pages)}\n\n"

        # Extract text by page (limit to first 5 pages)
        max_pages = min(5, len(reader.pages))
        all_text = ""

        for i in range(max_pages):
            try:
                page = reader.pages[i]
                page_text = page.extract_text()

                current_page_text = f"### Page {i+1}\n\n"
                if page_text and len(page_text.strip()) > 0:
                    # Limit to 1500 characters per page
                    if len(page_text) > 1500:
                        current_page_text += page_text[:1500] + "...(truncated)...\n\n"
                    else:
                        current_page_text += page_text + "\n\n"
                else:
                    current_page_text += "(No text could be extracted from this page)\n\n"

                all_text += current_page_text

                # If total text is too long, break
                if len(all_text) > 8000:
                    all_text += "...(truncating remaining pages; PDF is too large)...\n\n"
                    break

            except Exception as page_err:
                logging.error(f"Error processing PDF page {i+1}: {str(page_err)}")
                all_text += f"### Page {i+1}\n\n(Error extracting content: {str(page_err)})\n\n"

        if len(reader.pages) > max_pages:
            all_text += f"\nNote: Only the first {max_pages} pages are shown out of {len(reader.pages)} total.\n\n"

        result += "### PDF Content\n\n" + all_text
        return result

    except Exception as e:
        logging.error(f"PDF file processing error: {str(e)}")
        return f"## PDF File: {file.name}\n\nError occurred: {str(e)}\n\nThis PDF file cannot be processed."

def process_uploaded_files(files):
    """Combine the contents of all uploaded files into one string."""
    if not files:
        return None

    result = "# Uploaded File Contents\n\n"
    result += "Below is the content from the files provided by the user. Integrate this data as a main source of information for your response.\n\n"

    for file in files:
        try:
            ext = file.name.split('.')[-1].lower()
            if ext == 'txt':
                result += process_text_file(file) + "\n\n---\n\n"
            elif ext == 'csv':
                result += process_csv_file(file) + "\n\n---\n\n"
            elif ext == 'pdf':
                result += process_pdf_file(file) + "\n\n---\n\n"
            else:
                result += f"### Unsupported File: {file.name}\n\n---\n\n"
        except Exception as e:
            logging.error(f"File processing error {file.name}: {e}")
            result += f"### File processing error: {file.name}\n\nError: {e}\n\n---\n\n"

    return result

# ──────────────────────────────── Image & Utility ─────────────────────────
def get_images_for_query(query, count=5):
    """
    Simulate getting relevant images for a query.
    In a real implementation, this would call an image search API.
    """
    # This is a placeholder - in production, you would use a real image search API
    sample_images = [
        "https://source.unsplash.com/random/800x600/?"+query.replace(" ", "+"),
        "https://source.unsplash.com/random/600x400/?"+query.replace(" ", "+"),
        "https://source.unsplash.com/random/400x300/?"+query.replace(" ", "+"),
    ]
    return sample_images[:min(count, len(sample_images))]

def get_videos_for_query(query, count=2):
    """
    Simulate getting relevant videos for a query.
    In a real implementation, this would call a video search API.
    """
    # This is a placeholder - in production, you would use a real video search API
    sample_videos = [
        {"title": f"Introduction to {query}", "url": "https://www.youtube.com/results?search_query="+query.replace(" ", "+")},
        {"title": f"Detailed explanation of {query}", "url": "https://www.youtube.com/results?search_query=advanced+"+query.replace(" ", "+")}
    ]
    return sample_videos[:min(count, len(sample_videos))]

def generate_image(prompt, w=768, h=768, g=3.5, steps=30, seed=3):
    """Image generation function."""
    if not prompt:
        return None, "Insufficient prompt"
    try:
        res = Client(IMAGE_API_URL).predict(
            prompt=prompt, width=w, height=h, guidance=g,
            inference_steps=steps, seed=seed,
            do_img2img=False, init_image=None,
            image2image_strength=0.8, resize_img=True,
            api_name="/generate_image"
        )
        return res[0], f"Seed: {res[1]}"
    except Exception as e:
        logging.error(e)
        return None, str(e)

def extract_image_prompt(response_text: str, topic: str):
    """
    Generate a single-line English image prompt from the response content.
    """
    client = get_openai_client()
    
    try:
        response = client.chat.completions.create(
            model="gpt-4.1-mini",
            messages=[
                {"role": "system", "content": "Generate a single-line English image prompt from the following text. Return only the prompt text, nothing else."},
                {"role": "user", "content": f"Topic: {topic}\n\n---\n{response_text}\n\n---"}
            ],
            temperature=1,
            max_tokens=80,
            top_p=1
        )
        
        return response.choices[0].message.content.strip()
    except Exception as e:
        logging.error(f"OpenAI image prompt generation error: {e}")
        return f"A professional photo related to {topic}, high quality"

def md_to_html(md: str, title="Perplexity-like Response"):
    """Convert Markdown to HTML."""
    return f"<!DOCTYPE html><html><head><title>{title}</title><meta charset='utf-8'></head><body>{markdown.markdown(md)}</body></html>"

def keywords(text: str, top=5):
    """Simple keyword extraction."""
    cleaned = re.sub(r"[^κ°€-힣a-zA-Z0-9\s]", "", text)
    return " ".join(cleaned.split()[:top])

# ──────────────────────────────── Streamlit UI ────────────────────────────
def perplexity_app():
    st.title("Perplexity-like AI Assistant")

    # Set default session state
    if "ai_model" not in st.session_state:
        st.session_state.ai_model = "gpt-4.1-mini"  # κ³ μ • λͺ¨λΈ μ„€μ •
    if "messages" not in st.session_state:
        st.session_state.messages = []
    if "auto_save" not in st.session_state:
        st.session_state.auto_save = True
    if "generate_image" not in st.session_state:
        st.session_state.generate_image = False
    if "web_search_enabled" not in st.session_state:
        st.session_state.web_search_enabled = True
    if "search_mode" not in st.session_state:
        st.session_state.search_mode = "comprehensive"
    if "response_style" not in st.session_state:
        st.session_state.response_style = "professional"

    # Sidebar UI
    sb = st.sidebar
    sb.title("Search Settings")
    
    sb.subheader("Response Configuration")
    sb.selectbox(
        "Search Mode", 
        options=list(SEARCH_MODES.keys()), 
        format_func=lambda x: SEARCH_MODES[x],
        key="search_mode"
    )
    
    sb.selectbox(
        "Response Style",
        options=list(RESPONSE_STYLES.keys()),
        format_func=lambda x: RESPONSE_STYLES[x],
        key="response_style"
    )
    
    # Example queries
    sb.subheader("Example Queries")
    c1, c2, c3 = sb.columns(3)
    if c1.button("Quantum Computing", key="ex1"):
        process_example(EXAMPLE_QUERIES["example1"])
    if c2.button("Climate Change", key="ex2"):
        process_example(EXAMPLE_QUERIES["example2"])
    if c3.button("AI Economics", key="ex3"):
        process_example(EXAMPLE_QUERIES["example3"])
    
    sb.subheader("Other Settings")
    sb.toggle("Auto Save", key="auto_save")
    sb.toggle("Auto Image Generation", key="generate_image")
    
    web_search_enabled = sb.toggle("Use Web Search", value=st.session_state.web_search_enabled)
    st.session_state.web_search_enabled = web_search_enabled
    
    if web_search_enabled:
        st.sidebar.info("βœ… Web search results will be integrated into the response.")

    # Download the latest response
    latest_response = next(
        (m["content"] for m in reversed(st.session_state.messages) 
         if m["role"] == "assistant" and m["content"].strip()), 
        None
    )
    if latest_response:
        # Extract a title from the response - first heading or first line
        title_match = re.search(r"# (.*?)(\n|$)", latest_response)
        if title_match:
            title = title_match.group(1).strip()
        else:
            first_line = latest_response.split('\n', 1)[0].strip()
            title = first_line[:40] + "..." if len(first_line) > 40 else first_line
        
        sb.subheader("Download Latest Response")
        d1, d2 = sb.columns(2)
        d1.download_button("Download as Markdown", latest_response, 
                           file_name=f"{title}.md", mime="text/markdown")
        d2.download_button("Download as HTML", md_to_html(latest_response, title),
                           file_name=f"{title}.html", mime="text/html")

    # JSON conversation record upload
    up = sb.file_uploader("Load Conversation History (.json)", type=["json"], key="json_uploader")
    if up:
        try:
            st.session_state.messages = json.load(up)
            sb.success("Conversation history loaded successfully")
        except Exception as e:
            sb.error(f"Failed to load: {e}")

    # JSON conversation record download
    if sb.button("Download Conversation as JSON"):
        sb.download_button(
            "Save",
            data=json.dumps(st.session_state.messages, ensure_ascii=False, indent=2),
            file_name="conversation_history.json",
            mime="application/json"
        )

    # File Upload
    st.subheader("Upload Files")
    uploaded_files = st.file_uploader(
        "Upload files to be used as reference (txt, csv, pdf)",
        type=["txt", "csv", "pdf"],
        accept_multiple_files=True,
        key="file_uploader"
    )
    
    if uploaded_files:
        file_count = len(uploaded_files)
        st.success(f"{file_count} files uploaded. They will be used as sources for your query.")
        
        with st.expander("Preview Uploaded Files", expanded=False):
            for idx, file in enumerate(uploaded_files):
                st.write(f"**File Name:** {file.name}")
                ext = file.name.split('.')[-1].lower()
                
                if ext == 'txt':
                    preview = file.read(1000).decode('utf-8', errors='ignore')
                    file.seek(0)
                    st.text_area(
                        f"Preview of {file.name}",
                        preview + ("..." if len(preview) >= 1000 else ""),
                        height=150
                    )
                elif ext == 'csv':
                    try:
                        df = pd.read_csv(file)
                        file.seek(0)
                        st.write("CSV Preview (up to 5 rows)")
                        st.dataframe(df.head(5))
                    except Exception as e:
                        st.error(f"CSV preview failed: {e}")
                elif ext == 'pdf':
                    try:
                        file_bytes = file.read()
                        file.seek(0)
                        
                        pdf_file = io.BytesIO(file_bytes)
                        reader = PyPDF2.PdfReader(pdf_file, strict=False)
                        
                        pc = len(reader.pages)
                        st.write(f"PDF File: {pc} pages")
                        
                        if pc > 0:
                            try:
                                page_text = reader.pages[0].extract_text()
                                preview = page_text[:500] if page_text else "(No text extracted)"
                                st.text_area("Preview of the first page", preview + "...", height=150)
                            except:
                                st.warning("Failed to extract text from the first page")
                    except Exception as e:
                        st.error(f"PDF preview failed: {e}")

                if idx < file_count - 1:
                    st.divider()

    # Display existing messages
    for m in st.session_state.messages:
        with st.chat_message(m["role"]):
            # Process markdown to allow clickable links and properly rendered content
            st.markdown(m["content"], unsafe_allow_html=True)
            
            # Display images if present
            if "images" in m and m["images"]:
                st.subheader("Related Images")
                cols = st.columns(min(3, len(m["images"])))
                for i, (img_url, caption) in enumerate(m["images"]):
                    col_idx = i % len(cols)
                    with cols[col_idx]:
                        st.image(img_url, caption=caption, use_column_width=True)
            
            # Display videos if present
            if "videos" in m and m["videos"]:
                st.subheader("Related Videos")
                for video in m["videos"]:
                    st.markdown(f"[🎬 {video['title']}]({video['url']})", unsafe_allow_html=True)

    # User input
    query = st.chat_input("Enter your query or question here.")
    if query:
        process_input(query, uploaded_files)

    # μ‚¬μ΄λ“œλ°” ν•˜λ‹¨ λ°°μ§€(링크) μΆ”κ°€
    sb.markdown("---")
    sb.markdown("Created by [https://ginigen.com](https://ginigen.com) | [YouTube Channel](https://www.youtube.com/@ginipickaistudio)")

def process_example(topic):
    """Process the selected example query."""
    process_input(topic, [])

def process_input(query: str, uploaded_files):
    # Add user's message
    if not any(m["role"] == "user" and m["content"] == query for m in st.session_state.messages):
        st.session_state.messages.append({"role": "user", "content": query})

    with st.chat_message("user"):
        st.markdown(query)
    
    with st.chat_message("assistant"):
        placeholder = st.empty()
        message_placeholder = st.empty()
        full_response = ""

        use_web_search = st.session_state.web_search_enabled
        has_uploaded_files = bool(uploaded_files) and len(uploaded_files) > 0
        
        try:
            # μƒνƒœ ν‘œμ‹œλ₯Ό μœ„ν•œ μƒνƒœ μ»΄ν¬λ„ŒνŠΈ
            status = st.status("Preparing to answer your query...")
            status.update(label="Initializing client...")
            
            client = get_openai_client()
            
            # Prepare conversation messages
            messages = []
            
            # Web search
            search_content = None
            if use_web_search:
                status.update(label="Performing web search...")
                with st.spinner("Searching the web..."):
                    search_content = do_web_search(keywords(query, top=5))
            
            # Process uploaded files β†’ content
            file_content = None
            if has_uploaded_files:
                status.update(label="Processing uploaded files...")
                with st.spinner("Analyzing files..."):
                    file_content = process_uploaded_files(uploaded_files)
            
            # Get relevant images and videos (before generating response)
            status.update(label="Finding related media...")
            related_images = get_images_for_query(query) if use_web_search else []
            related_videos = get_videos_for_query(query) if use_web_search else []
            
            # Build system prompt
            status.update(label="Preparing comprehensive answer...")
            sys_prompt = get_system_prompt(
                mode=st.session_state.search_mode,
                style=st.session_state.response_style,
                include_search_results=use_web_search,
                include_uploaded_files=has_uploaded_files
            )

            # OpenAI API 호좜 μ€€λΉ„
            status.update(label="Generating response...")
            
            # λ©”μ‹œμ§€ ꡬ성
            api_messages = [
                {"role": "system", "content": sys_prompt}
            ]
            
            user_content = query
            
            # 검색 κ²°κ³Όκ°€ 있으면 μ‚¬μš©μž ν”„λ‘¬ν”„νŠΈμ— μΆ”κ°€
            if search_content:
                user_content += "\n\n" + search_content
            
            # 파일 λ‚΄μš©μ΄ 있으면 μ‚¬μš©μž ν”„λ‘¬ν”„νŠΈμ— μΆ”κ°€
            if file_content:
                user_content += "\n\n" + file_content
            
            # Add image and video information to the prompt
            if related_images:
                user_content += "\n\n# Related Images\n"
                for i, img_url in enumerate(related_images):
                    user_content += f"\n![Image {i+1}]({img_url})"
            
            if related_videos:
                user_content += "\n\n# Related Videos\n"
                for video in related_videos:
                    user_content += f"\n[Video: {video['title']}]({video['url']})"
            
            # μ‚¬μš©μž λ©”μ‹œμ§€ μΆ”κ°€
            api_messages.append({"role": "user", "content": user_content})
            
            # OpenAI API 슀트리밍 호좜 - κ³ μ • λͺ¨λΈ "gpt-4.1-mini" μ‚¬μš©
            try:
                # 슀트리밍 λ°©μ‹μœΌλ‘œ API 호좜
                stream = client.chat.completions.create(
                    model="gpt-4.1-mini",  # κ³ μ • λͺ¨λΈ μ‚¬μš©
                    messages=api_messages,
                    temperature=1,
                    max_tokens=MAX_TOKENS,
                    top_p=1,
                    stream=True  # 슀트리밍 ν™œμ„±ν™”
                )
                
                # 슀트리밍 응닡 처리
                for chunk in stream:
                    if chunk.choices and len(chunk.choices) > 0 and chunk.choices[0].delta.content is not None:
                        content_delta = chunk.choices[0].delta.content
                        full_response += content_delta
                        message_placeholder.markdown(full_response + "β–Œ", unsafe_allow_html=True)
                
                # μ΅œμ’… 응닡 ν‘œμ‹œ (μ»€μ„œ 제거)
                message_placeholder.markdown(full_response, unsafe_allow_html=True)
                
                # Display related images if available
                if related_images:
                    image_captions = [f"Related image {i+1}" for i in range(len(related_images))]
                    images_with_captions = list(zip(related_images, image_captions))
                    
                    cols = st.columns(min(3, len(related_images)))
                    for i, (img_url, caption) in enumerate(images_with_captions):
                        col_idx = i % len(cols)
                        with cols[col_idx]:
                            st.image(img_url, caption=caption, use_column_width=True)
                
                # Display related videos if available
                if related_videos:
                    st.subheader("Related Videos")
                    for video in related_videos:
                        st.markdown(f"[🎬 {video['title']}]({video['url']})", unsafe_allow_html=True)
                
                status.update(label="Response completed!", state="complete")
                
                # Save the response with images and videos in the session state
                st.session_state.messages.append({
                    "role": "assistant", 
                    "content": full_response,
                    "images": list(zip(related_images, image_captions)) if related_images else [],
                    "videos": related_videos
                })
                
            except Exception as api_error:
                error_message = str(api_error)
                logging.error(f"API error: {error_message}")
                status.update(label=f"Error: {error_message}", state="error")
                raise Exception(f"Response generation error: {error_message}")
            
            # Additional image generation if enabled
            if st.session_state.generate_image and full_response:
                with st.spinner("Generating custom image..."):
                    try:
                        ip = extract_image_prompt(full_response, query)
                        img, cap = generate_image(ip)
                        if img:
                            st.subheader("AI-Generated Image")
                            st.image(img, caption=cap)
                    except Exception as img_error:
                        logging.error(f"Image generation error: {str(img_error)}")
                        st.warning("Custom image generation failed. Using web images only.")

            # Download buttons
            if full_response:
                st.subheader("Download This Response")
                c1, c2 = st.columns(2)
                c1.download_button(
                    "Markdown", 
                    data=full_response, 
                    file_name=f"{query[:30]}.md",
                    mime="text/markdown"
                )
                c2.download_button(
                    "HTML",
                    data=md_to_html(full_response, query[:30]),
                    file_name=f"{query[:30]}.html",
                    mime="text/html"
                )

            # Auto save
            if st.session_state.auto_save and st.session_state.messages:
                try:
                    fn = f"conversation_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
                    with open(fn, "w", encoding="utf-8") as fp:
                        json.dump(st.session_state.messages, fp, ensure_ascii=False, indent=2)
                except Exception as e:
                    logging.error(f"Auto-save failed: {e}")

        except Exception as e:
            error_message = str(e)
            placeholder.error(f"An error occurred: {error_message}")
            logging.error(f"Process input error: {error_message}")
            ans = f"An error occurred while processing your request: {error_message}"
            st.session_state.messages.append({"role": "assistant", "content": ans})


# ──────────────────────────────── main ────────────────────────────────────
def main():
    perplexity_app()

if __name__ == "__main__":
    main()