File size: 35,339 Bytes
ec10cdd
 
 
 
 
 
 
 
e8e136b
ec10cdd
 
 
2139f3d
 
ec10cdd
e8e136b
 
 
ec10cdd
 
 
 
 
 
 
e8e136b
 
 
 
ec10cdd
 
e8e136b
ec10cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
ec10cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
ec10cdd
 
 
 
 
 
e8e136b
ec10cdd
 
 
 
e8e136b
c2dc880
e8e136b
 
 
 
 
 
 
 
 
 
 
 
 
c2dc880
e8e136b
 
c2dc880
e8e136b
 
ec10cdd
86df61a
 
e8e136b
 
 
 
 
 
 
 
 
 
 
86df61a
 
 
e8e136b
 
86df61a
e8e136b
 
 
86df61a
e8e136b
86df61a
 
 
 
e8e136b
86df61a
e8e136b
 
 
c2dc880
e8e136b
86df61a
e8e136b
 
 
86df61a
 
 
 
 
 
 
 
 
e8e136b
86df61a
e8e136b
 
 
c2dc880
e8e136b
 
86df61a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec10cdd
 
e8e136b
ec10cdd
2139f3d
e8e136b
2139f3d
 
 
 
e8e136b
2139f3d
f8a9484
2139f3d
 
 
 
e8e136b
f8a9484
 
 
e8e136b
 
f8a9484
a8b97b6
 
e8e136b
2139f3d
e8e136b
f8a9484
2139f3d
f8a9484
 
 
 
 
 
 
2139f3d
f8a9484
a8b97b6
e8e136b
 
 
2139f3d
ec10cdd
 
2139f3d
ec10cdd
e8e136b
2139f3d
e8e136b
2139f3d
 
f8a9484
e8e136b
f8a9484
 
 
e8e136b
f8a9484
2139f3d
 
 
 
e8e136b
 
ec10cdd
e8e136b
ec10cdd
 
 
 
e8e136b
2139f3d
 
 
 
 
ec10cdd
e8e136b
ec10cdd
 
 
 
 
 
 
 
 
e8e136b
ec10cdd
e8e136b
 
2139f3d
 
 
 
6161b12
 
 
2139f3d
 
6161b12
e8e136b
ec10cdd
e8e136b
ec10cdd
e8e136b
ec10cdd
e8e136b
 
ec10cdd
e8e136b
ec10cdd
e8e136b
ec10cdd
 
 
 
e8e136b
 
 
 
 
2b21d70
e8e136b
2b21d70
 
 
 
 
 
 
ec10cdd
 
 
 
 
 
 
2139f3d
ec10cdd
2139f3d
ec10cdd
 
 
7c6d1b0
2139f3d
 
7c6d1b0
ec10cdd
7c6d1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
2139f3d
7c6d1b0
e8e136b
 
2139f3d
595bc38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec10cdd
2139f3d
 
 
 
 
 
 
 
 
ec10cdd
f8a9484
 
 
 
 
 
 
 
 
 
2139f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec10cdd
2139f3d
 
 
 
 
f044f7b
 
 
ec10cdd
2139f3d
ec10cdd
 
 
9ed84d0
ec10cdd
2139f3d
703b5ad
0621187
195826f
 
 
a4bd0c0
 
 
ec10cdd
195826f
ec10cdd
 
 
0621187
ec10cdd
0621187
195826f
ec10cdd
195826f
ec10cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
a4bd0c0
e8e136b
195826f
e8e136b
 
 
1d7d721
 
e8e136b
1d7d721
e8e136b
 
a4bd0c0
195826f
 
 
 
a4bd0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d7d721
a4bd0c0
 
1d7d721
e8e136b
1d7d721
 
 
 
a4bd0c0
 
1d7d721
 
 
 
ec10cdd
1d7d721
a4bd0c0
 
1d7d721
 
 
 
 
 
 
703b5ad
60091af
ec10cdd
e8e136b
 
ec10cdd
 
e8e136b
 
 
 
 
 
 
ec10cdd
 
e8e136b
 
 
 
 
 
 
 
 
 
 
195826f
e8e136b
195826f
 
e8e136b
 
195826f
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195826f
e8e136b
195826f
 
e8e136b
 
195826f
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
 
 
 
 
 
 
 
 
 
 
 
ec10cdd
60091af
ec10cdd
e8e136b
0621187
 
 
ec10cdd
60091af
 
0621187
ec10cdd
aa9f474
4b8b3a8
 
 
 
 
aa9f474
 
 
4b8b3a8
aa9f474
 
4b8b3a8
aa9f474
4b8b3a8
aa9f474
 
 
4b8b3a8
aa9f474
 
4b8b3a8
 
aa9f474
4b8b3a8
 
 
aa9f474
4b8b3a8
aa9f474
 
e8e136b
aa9f474
e8e136b
 
 
 
 
aa9f474
 
 
e8e136b
aa9f474
 
 
 
 
 
e8e136b
aa9f474
e8e136b
 
 
 
 
aa9f474
4b8b3a8
e8e136b
4b8b3a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e136b
 
aa9f474
4b8b3a8
 
 
 
 
aa9f474
 
 
 
 
 
 
 
 
 
e8e136b
aa9f474
 
 
 
e8e136b
aa9f474
 
e8e136b
 
 
 
 
 
 
 
aa9f474
 
e8e136b
aa9f474
 
e8e136b
6cbd21d
4b8b3a8
ec10cdd
4b8b3a8
 
 
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
import re
import os
import io
import json
import requests
import base64
from PIL import Image
from gtts import gTTS
from duckduckgo_search import DDGS
import warnings
warnings.filterwarnings('ignore')

# -----------------------------------------------------------------------------
# 1. PAGE CONFIGURATION
# -----------------------------------------------------------------------------
st.set_page_config(
    page_title="H2 Physics Feynman Bot",
    page_icon="⚛️",
    layout="centered",
    initial_sidebar_state="expanded"
)

# -----------------------------------------------------------------------------
# 2. HELPER FUNCTIONS
# -----------------------------------------------------------------------------

@st.cache_data(show_spinner=False, ttl=3600)
def generate_audio(text):
    """Generates MP3 audio from text."""
    clean_text = re.sub(r'```.*?```', 'I have generated a graph.', text, flags=re.DOTALL)
    clean_text = re.sub(r'\[IMAGE:.*?\]', 'Here is a diagram.', clean_text)
    
    if len(clean_text) > 1000:
        clean_text = clean_text[:1000] + "..."
    
    try:
        tts = gTTS(text=clean_text, lang='en', slow=False)
        audio_fp = io.BytesIO()
        tts.write_to_fp(audio_fp)
        audio_fp.seek(0)
        return audio_fp
    except Exception as e:
        return None

def google_search_api(query, api_key, cx):
    """Google Custom Search API."""
    try:
        url = "https://www.googleapis.com/customsearch/v1"
        params = {
            "q": query,
            "cx": cx,
            "key": api_key,
            "searchType": "image",
            "num": 3,
            "safe": "active"
        }
        
        response = requests.get(url, params=params, timeout=10)
        
        if response.status_code in [403, 429]:
            return None
            
        data = response.json()
        
        if "items" in data and len(data["items"]) > 0:
            for item in data["items"]:
                link = item["link"]
                if link.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')):
                    return link
            return data["items"][0]["link"]
            
    except Exception as e:
        return None
    return None

def duckduckgo_search_api(query):
    """DuckDuckGo image search."""
    try:
        with DDGS() as ddgs:
            results = list(ddgs.images(query, max_results=1))
            if results:
                return results[0]['image']
    except Exception as e:
        return None
    return None

@st.cache_data(show_spinner=False, ttl=300)
def search_image(query):
    """Image search with fallback."""
    try:
        # Check secrets
        if hasattr(st, 'secrets'):
            cx = st.secrets.get("GOOGLE_CX", "")
            key1 = st.secrets.get("GOOGLE_SEARCH_KEY", "")
            key2 = st.secrets.get("GOOGLE_SEARCH_KEY_2", "")
            
            if key1 and cx:
                url = google_search_api(query, key1, cx)
                if url: return url
            
            if key2 and cx:
                url = google_search_api(query, key2, cx)
                if url: return url
        
        # Fallback to DuckDuckGo
        return duckduckgo_search_api(query)
        
    except Exception as e:
        return None

def analyze_image_with_huggingface(image, query="What physics concepts are shown?", hf_token=None):
    """Analyze image using Hugging Face Inference API."""
    try:
        # Convert image to base64
        buffered = io.BytesIO()
        
        # Convert to RGB if necessary
        if image.mode != 'RGB':
            image = image.convert('RGB')
            
        image.save(buffered, format="JPEG", quality=85)
        img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        
        # Use Hugging Face Inference API
        # Model: Qwen/Qwen-VL-Chat
        api_url = "https://api-inference.huggingface.co/models/Qwen/Qwen-VL-Chat"
        
        headers = {
            "Authorization": f"Bearer {hf_token}",
            "Content-Type": "application/json"
        }
        
        # Prepare the payload for Qwen-VL
        payload = {
            "inputs": {
                "question": query,
                "image": img_base64
            },
            "parameters": {
                "max_new_tokens": 500,
                "temperature": 0.7
            }
        }
        
        # Make API call
        response = requests.post(api_url, json=payload, headers=headers, timeout=60)
        
        if response.status_code == 200:
            result = response.json()
            
            # Handle different response formats
            if isinstance(result, dict) and "generated_text" in result:
                return result["generated_text"]
            elif isinstance(result, list) and len(result) > 0:
                if "generated_text" in result[0]:
                    return result[0]["generated_text"]
                else:
                    return str(result[0])[:500]
            else:
                return str(result)[:500]
        else:
            return f"API Error {response.status_code}: {response.text[:200]}"
            
    except Exception as e:
        return f"Image analysis error: {str(e)[:100]}"

def analyze_image_with_openrouter(image, query="What physics concepts are shown?", api_key=None):
    """Analyze image using OpenRouter API (multiple providers)."""
    try:
        # Convert image to base64
        buffered = io.BytesIO()
        
        if image.mode != 'RGB':
            image = image.convert('RGB')
            
        image.save(buffered, format="JPEG", quality=85)
        img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        
        api_url = "https://openrouter.ai/api/v1/chat/completions"
        
        headers = {
            "Authorization": f"Bearer {api_key}",
            "HTTP-Referer": "https://h2-feynman-bot.vercel.app",
            "X-Title": "H2 Physics Feynman Bot"
        }
        
        # Try different vision models available on OpenRouter
        payload = {
            "model": "google/gemini-pro-vision",  # Free tier available
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": query
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"data:image/jpeg;base64,{img_base64}"
                            }
                        }
                    ]
                }
            ],
            "max_tokens": 1000,
            "temperature": 0.7
        }
        
        response = requests.post(api_url, json=payload, headers=headers, timeout=60)
        
        if response.status_code == 200:
            result = response.json()
            return result["choices"][0]["message"]["content"]
        else:
            # Fallback to another model
            payload["model"] = "claude-3-haiku-20240307"
            response = requests.post(api_url, json=payload, headers=headers, timeout=60)
            if response.status_code == 200:
                result = response.json()
                return result["choices"][0]["message"]["content"]
            else:
                return f"API Error: {response.status_code}"
                
    except Exception as e:
        return f"Image analysis error: {str(e)[:100]}"

def analyze_image_with_togetherai(image, query="What physics concepts are shown?", api_key=None):
    """Analyze image using Together.ai API (supports Qwen-VL)."""
    try:
        # Convert image to base64
        buffered = io.BytesIO()
        
        if image.mode != 'RGB':
            image = image.convert('RGB')
            
        image.save(buffered, format="JPEG", quality=85)
        img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
        
        api_url = "https://api.together.xyz/v1/chat/completions"
        
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }
        
        # Together.ai supports Qwen-VL models
        payload = {
            "model": "Qwen/Qwen-VL-Chat",  # or "Qwen/Qwen-VL-Chat-Int4" for faster
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": query
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"data:image/jpeg;base64,{img_base64}"
                            }
                        }
                    ]
                }
            ],
            "max_tokens": 1000,
            "temperature": 0.7
        }
        
        response = requests.post(api_url, json=payload, headers=headers, timeout=60)
        
        if response.status_code == 200:
            result = response.json()
            return result["choices"][0]["message"]["content"]
        else:
            return f"API Error {response.status_code}: {response.text[:200]}"
            
    except Exception as e:
        return f"Image analysis error: {str(e)[:100]}"

        
        
def analyze_image_local_fallback(image, query):
    """Simple local image analysis fallback."""
    try:
        # Get basic image info
        width, height = image.size
        format_info = image.format if image.format else "Unknown"
        mode = image.mode
        
        analysis = f"Image analysis: {width}x{height} pixels, format: {format_info}, mode: {mode}. "
        
        # Try to detect if it's a physics-related image
        if width > height:
            analysis += "The image appears to be landscape orientation. "
        else:
            analysis += "The image appears to be portrait orientation. "
        
        # Add physics context
        analysis += "For physics analysis, please describe what you see in the image, and I'll help explain the physics concepts."
        
        return analysis
    except:
        return "Image received. Please describe what you see in the image for physics analysis."

# -----------------------------------------------------------------------------
# 4. GRAPH FUNCTIONS
# -----------------------------------------------------------------------------

def execute_plotting_code(code_snippet):
    """Execute Python plotting code."""
    try:
        plt.close('all')
        fig, ax = plt.subplots(figsize=(10, 6))
        
        namespace = {
            'plt': plt,
            'np': np,
            'ax': ax,
            'fig': fig,
            'math': __import__('math')
        }
        
        cleaned_code = code_snippet.strip()
        
        # Ensure imports
        if 'import matplotlib' not in cleaned_code:
            cleaned_code = 'import matplotlib.pyplot as plt\nimport numpy as np\n' + cleaned_code
        
        # Ensure plotting
        plot_keywords = ['plt.plot(', 'ax.plot(', 'plt.scatter(', 'ax.scatter(']
        has_plot = any(keyword in cleaned_code for keyword in plot_keywords)
        
        if not has_plot:
            cleaned_code += '\n\n# Sample plot\nx = np.linspace(0, 10, 100)\ny = np.sin(x)\nplt.plot(x, y, "b-", linewidth=2, label="sin(x)")\n'
        
        # Remove plt.show
        cleaned_code = cleaned_code.replace('plt.show()', '')
        
        # Add labels if missing
        if 'plt.xlabel' not in cleaned_code:
            cleaned_code += '\nplt.xlabel("X-axis", fontsize=12)'
        if 'plt.ylabel' not in cleaned_code:
            cleaned_code += '\nplt.ylabel("Y-axis", fontsize=12)'
        if 'plt.grid' not in cleaned_code:
            cleaned_code += '\nplt.grid(True, linestyle="--", alpha=0.6)'
        
        exec(cleaned_code, namespace)
        
        ax = plt.gca()
        if not ax.get_title():
            ax.set_title('Physics Graph', fontsize=14)
        
        st.pyplot(fig)
        plt.close(fig)
        
    except Exception as e:
        st.error(f"Graph Error: {str(e)[:100]}")
        
        # Fallback plot
        try:
            fig, ax = plt.subplots(figsize=(10, 6))
            x = np.linspace(0, 10, 100)
            y = x**2
            ax.plot(x, y, 'b-', linewidth=2, label='y = x²')
            ax.set_xlabel('X', fontsize=12)
            ax.set_ylabel('Y', fontsize=12)
            ax.set_title('Sample Graph', fontsize=14)
            ax.grid(True, linestyle='--', alpha=0.6)
            ax.legend()
            st.pyplot(fig)
            plt.close(fig)
        except:
            st.warning("Could not generate graph.")

def display_message(role, content, enable_voice=False):
    """Display chat message."""
    with st.chat_message(role):
        
        text_to_display = content
        
        # Extract code blocks
        code_pattern = r'```python\s*(.*?)```'
        code_matches = list(re.finditer(code_pattern, content, re.DOTALL))
        
        for match in reversed(code_matches):
            text_to_display = text_to_display.replace(match.group(0), "")
        
        # Check for image tags
        image_match = re.search(r'\[IMAGE:\s*(.*?)\]', text_to_display, re.IGNORECASE)
        image_result = None
        
        if image_match and role == "assistant":
            search_query = image_match.group(1)
            text_to_display = text_to_display.replace(image_match.group(0), "")
            with st.spinner(f"Searching for '{search_query}'..."):
                image_result = search_image(search_query)

        # Display text
        st.markdown(text_to_display)

        # Show code and graph
        if code_matches and role == "assistant":
            for match in code_matches:
                code_content = match.group(1).strip()
                if code_content:
                    with st.expander("📝 View Python Code", expanded=False):
                        st.code(code_content, language='python')
                    
                    with st.expander("📊 Generated Graph", expanded=True):
                        execute_plotting_code(code_content)
        
        # Show image
        if image_match and role == "assistant":
            if image_result and "http" in str(image_result):
                try:
                    st.image(image_result, caption=f"📷 {image_match.group(1)}", width=500)
                    st.markdown(f"🔗 [Open Image]({image_result})")
                except:
                    st.warning("Could not display image.")
            else:
                st.warning(f"Image search failed.")

        # Audio output
        if enable_voice and role == "assistant" and len(text_to_display.strip()) > 10:
            audio_bytes = generate_audio(text_to_display)
            if audio_bytes:
                st.audio(audio_bytes, format='audio/mp3')

# -----------------------------------------------------------------------------
# 5. GROQ API FUNCTION
# -----------------------------------------------------------------------------

def call_groq_api(api_key, messages, max_tokens=2000):
    """Call Groq API."""
    url = "https://api.groq.com/openai/v1/chat/completions"
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    formatted_messages = []
    for msg in messages:
        if msg["role"] == "system":
            formatted_messages.append({
                "role": "system",
                "content": msg["content"]
            })
        elif msg["role"] in ["user", "assistant"]:
            formatted_messages.append({
                "role": msg["role"],
                "content": msg["content"]
            })
    
    models_to_try = [
        "llama-3.1-8b-instant",
        "llama-3.2-3b-preview",
    ]
    
    for model in models_to_try:
        data = {
            "model": model,
            "messages": formatted_messages,
            "max_tokens": max_tokens,
            "temperature": 0.7,
            "stream": False
        }
        
        try:
            response = requests.post(url, headers=headers, json=data, timeout=30)
            
            if response.status_code == 200:
                result = response.json()
                return result["choices"][0]["message"]["content"]
            elif response.status_code == 429:
                continue
                
        except:
            continue
    
    return "Service temporarily unavailable. Please try again."
    
# System Instructions for the AI - Part 5a (Start)
SYSTEM_INSTRUCTIONS = """
**Identity:** Richard Feynman. Tutor for Singapore H2 Physics (9478).
**CORE DIRECTIVE:** STRICTLY adhere to the Syllabus 9478 topics below. Reject non-included topics from UK A-level syllabus.
**✅ SYLLABUS TOPICS & FORMULAS (9478):**
1. **Measurement:** SI units (mass, length, time, current, temp, mol), prefixes (p to T), homogeneity, scalars/vectors (resolution/addition), errors (random/systematic), uncertainty.
2. **Forces:** Normal, buoyant, drag (qualitative only; no viscosity coeff), Hooke’s Law ($F=kx$), Moments/Torque (couples, center of gravity), Equilibrium (no resultant F or Torque).
3. **Motion:** Kinematics ($s, u, v, a, t$ graphs & equations), Newton’s Laws (1, 2, 3), Momentum ($p=mv$), Impulse, $F_{net}=ma$ (const mass).
4. **Energy:** Stores/Transfers, Work ($W=Fs$), $E_k=\frac{1}{2}mv^2$, $E_p$ (grav/elastic/electric), Power ($P=Fv$), Efficiency, Conservation of Energy.
5. **Projectile:** Parabolic motion, $\Delta E_p=mg\Delta h$, Terminal velocity.
6. **Collisions:** Conservation of Momentum, Elastic vs Inelastic, Relative speeds (elastic). *Excluded: Coeff of restitution.*
7. **Circular Motion:** Radians, $\omega$, $v=r\omega$, $a=r\omega^2=v^2/r$, $F_c=mv^2/r$.
8. **Gravitation:** $F=G\frac{Mm}{r^2}$, Field $g=G\frac{M}{r^2}$, Potential $\phi=-\frac{GM}{r}$, $U=-\frac{GMm}{r}$, $g=-\frac{d\phi}{dr}$, Escape velocity, Orbits ($F_g=F_c$), Geostationary satellites.
9. **Oscillations (SHM):** $a=-\omega^2x$, $x=x_0\sin\omega t$, $v=\pm\omega\sqrt{x_0^2-x^2}$, Energy interchange, Damping (light/critical/heavy), Resonance (frequency response).
10. **Waves:** Transverse/Longitudinal, $v=f\lambda$, Intensity $\propto A^2$, Inverse square law, Polarization (Malus’ Law $I \propto \cos^2\theta$).
11. **Superposition:** Standing waves (nodes/antinodes), Path/Phase difference, Coherence, Double-slit ($\lambda=\frac{ax}{D}$), Diffraction grating ($d\sin\theta=n\lambda$), Single slit ($b\sin\theta=\lambda$ for min), Rayleigh criterion ($\theta \approx \lambda/b$).
12. **Thermal:** Kelvin ($T_K = T_C + 273.15$), Ideal Gas ($pV=NkT$), Avogadro ($N_A$), Kinetic Theory assumptions, $pV=\frac{1}{3}Nm\langle c^2\rangle$, Mean $E_k = \frac{3}{2}kT$.
13. **Thermodynamics:** Internal Energy ($U$), 1st Law ($\Delta U = Q+W$), Work on gas ($W=-p\Delta V$ implied) or by gas ($W=p\Delta V$), Specific Heat/Latent Heat.
14. **E-Fields:** Coulomb's $F=\frac{Q_1Q_2}{4\pi\varepsilon_0 r^2}$, Field $E=\frac{Q}{4\pi\varepsilon_0 r^2}$, Potential $V=\frac{Q}{4\pi\varepsilon_0 r}$, $U=\frac{Q_1Q_2}{4\pi\varepsilon_0 r}$, $E=-\frac{dV}{dr}$, Uniform field $E=V/d$, Capacitance $C=Q/V$, Energy $U=\frac{1}{2}CV^2$.
15. **Currents:** $I=Q/t$, $I=nAvq$, $V=W/Q$, $P=VI=I^2R$, EMF vs PD, AC (rms $I_0/\sqrt{2}$), Half-wave rectification.
16. **Circuits:** Symbols, $V=IR$, $R=\rho l/A$, I-V graphs (diode, lamp, NTC), Int. Resistance, Series/Parallel R & C, Potential Divider, Charging/Discharging ($x=x_0 e^{-t/RC}$).
17. **EM Forces:** B-fields (wire, coil, solenoid), Flux density $B$, Force on wire ($F=BIl\sin\theta$), Force on charge ($F=Bqv\sin\theta$), Velocity selector, Hall effect concept.
18. **EM Induction:** Flux $\Phi=BA$, Linkage $N\Phi$, Faraday’s & Lenz’s Laws, Transformers ($N_s/N_p = V_s/V_p = I_p/I_s$).
19. **Modern Physics:** Photoelectric ($E=hf$, Work function), Photon momentum ($p=h/\lambda$), De Broglie ($\lambda=h/p$), Wavefunction $\psi$, Uncertainty ($\Delta x \Delta p \gtrsim h$), Infinite well ($E_n = \frac{n^2 h^2}{8 m L^2}$), Line spectra.
20. **Nuclear:** Rutherford, Notation $^A_Z X$, Decay ($A=\lambda N$, $x=x_0e^{-\lambda t}$, $t_{1/2}=\ln 2/\lambda$), $E=mc^2$, Mass defect, Binding energy (curve), Fusion/Fission.

**GRAPH GENERATION RULES (CRITICAL):**
When asked to create a graph, you MUST write COMPLETE, EXECUTABLE Python code that:
1. Starts with: import matplotlib.pyplot as plt, import numpy as np
2. Creates a figure: plt.figure(figsize=(10, 6)) or fig, ax = plt.subplots(figsize=(10, 6))
3. Generates or uses appropriate physics data
4. Plots the data with plt.plot(), plt.scatter(), etc.
5. Adds proper labels: plt.xlabel(), plt.ylabel(), plt.title()
6. Adds grid: plt.grid(True, linestyle='--', alpha=0.6)
7. Adds legend if needed: plt.legend()

**IMPORTANT GRAPH RULES:**
- You MUST include ACTUAL DATA in your graph code
- Example of BAD code (won't show graph):
  ```python
  import matplotlib.pyplot as plt
  plt.figure(figsize=(10, 6))
  # Missing actual plot() command!
  plt.show()


**EXAMPLE OF GOOD GRAPH CODE:**
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(10, 6))
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, 'b-', linewidth=2, label='sin(x)')
plt.xlabel('X Variable', fontsize=12)
plt.ylabel('Y Variable', fontsize=12)
plt.title('Physics Graph', fontsize=14)
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()

**DIAGRAMS:**
When you need to show a diagram, use: [IMAGE: search query]
Example: "Here's the setup: [IMAGE: double slit experiment diagram]"

**TEACHING METHOD:**
1. Ask ONE question at a time
2. Use analogies to explain complex concepts
3. Guide, don't give answers immediately
4. Validate understanding frequently
5. Teach with clarity, enthusiasm, and the Feynman method
6. Only give full solutions when student says "I give up"
7. Summarize each concept with a clear summary in > blockquote
**FORMATTING:**
- Use LaTeX for equations: $F = ma$, $E = mc^2$
- Use **bold** for key terms
- Keep responses concise but thorough
- Be enthusiastic and encouraging
"""

## **PART 6: SIDEBAR CONFIGURATION**

# Sidebar Configuration - SIMPLIFIED
# -----------------------------------------------------------------------------
# 6. SIDEBAR CONFIGURATION
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# 6. SIDEBAR CONFIGURATION
# -----------------------------------------------------------------------------
with st.sidebar:
    # Header
    col1, col2 = st.columns([1, 3])
    with col1:
        st.image("https://upload.wikimedia.org/wikipedia/en/4/42/Richard_Feynman_Nobel.jpg", 
                width=80)
    with col2:
        st.markdown("### ⚛️ H2 Physics")
        st.caption("Feynman Tutor")
    
    st.divider()
    
    # Settings
    st.header("⚙️ Settings")
    topic = st.selectbox(
        "Topic Focus:",
        [
            "General / Any",
            "Measurement & Uncertainty", 
            "Kinematics & Dynamics",
            "Forces & Turning Effects", 
            "Work, Energy, Power",
            "Circular Motion", 
            "Gravitational Fields",
            "Thermal Physics", 
            "Oscillations & Waves",
            "Electricity & DC Circuits", 
            "Electromagnetism (EMI/AC)",
            "Modern Physics (Quantum/Nuclear)",
            "Paper 4: Practical Skills"
        ]
    )
    
    enable_voice = st.toggle("🗣️ Text-to-Speech", value=False)
    
    st.divider()
    
    # Image Analysis Settings
    st.header("🖼️ Image Analysis")
    
    image_analysis_mode = st.radio(
        "Analysis Service:",
        ["Hugging Face", "Together.ai", "OpenRouter", "Basic"],
        index=0,
        help="Choose image analysis service"
    )
    
    # Initialize variables - FIXED SECRETS CHECKING
    hf_token = None
    together_key = None
    openrouter_key = None
    
    # Safe secrets checking function
    def get_secret(key, default=None):
        """Safely get a secret from st.secrets or environment."""
        try:
            # First try st.secrets (Hugging Face Spaces)
            if hasattr(st, 'secrets'):
                # Check if secrets is loaded and has the key
                secrets_dict = dict(st.secrets)
                if key in secrets_dict:
                    return secrets_dict[key]
        except:
            pass
        
        # Fallback to environment variables
        return os.environ.get(key, default)
    
    if image_analysis_mode == "Hugging Face":
        hf_token = get_secret("HF_TOKEN")
        if hf_token:
            st.success("✓ Hugging Face Token configured")
        else:
            st.warning("Add HF_TOKEN to secrets")
            st.info("Get token: huggingface.co/settings/tokens")
    
    elif image_analysis_mode == "Together.ai":
        together_key = get_secret("TOGETHER_API_KEY")
        if together_key:
            st.success("✓ Together.ai API configured")
        else:
            st.warning("Add TOGETHER_API_KEY to secrets")
            st.info("Get key: together.ai (free credits available)")
    
    elif image_analysis_mode == "OpenRouter":
        openrouter_key = get_secret("OPENROUTER_API_KEY")
        if openrouter_key:
            st.success("✓ OpenRouter API configured")
        else:
            st.warning("Add OPENROUTER_API_KEY to secrets")
            st.info("Get key: openrouter.ai (free credits available)")
    
    else:  # Basic
        st.info("Basic image analysis mode")
    #st.divider()
    st.divider()
    
    # Media Input
    st.header("📤 Input Methods")
    
    visual_content = None
    image_analysis = None
    
    # Method selection
    input_method = st.radio(
        "Choose input method:",
        ["Camera", "Upload"],
        horizontal=True
    )
    
    if input_method == "Camera":
        st.subheader("📷 Camera")
        camera_photo = st.camera_input("Take a photo of physics problem")
        
        if camera_photo:
            try:
                image = Image.open(camera_photo)
                visual_content = image
                st.image(image, caption="Camera Capture", width=200)
                st.success("✓ Photo captured")
                
                # Analyze immediately based on selected mode
                with st.spinner("Analyzing image..."):
                    if image_analysis_mode == "Hugging Face" and hf_token:
                        image_analysis = analyze_image_with_huggingface(
                            image, 
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            hf_token
                        )
                    elif image_analysis_mode == "Together.ai" and together_key:
                        image_analysis = analyze_image_with_togetherai(
                            image,
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            together_key
                        )
                    elif image_analysis_mode == "OpenRouter" and openrouter_key:
                        image_analysis = analyze_image_with_openrouter(
                            image,
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            openrouter_key
                        )
                    else:
                        image_analysis = analyze_image_local_fallback(
                            image,
                            "Analyze this physics image"
                        )
                        
                if image_analysis:
                    st.info(f"📋 Analysis: {image_analysis[:150]}...")
                    
            except Exception as e:
                st.error(f"Camera error: {e}")
    
    else:  # Upload
        st.subheader("📁 Upload")
        uploaded_file = st.file_uploader(
            "Choose image", 
            type=["jpg", "jpeg", "png"],
            help="Upload physics diagrams or problems"
        )
        
        if uploaded_file:
            try:
                image = Image.open(uploaded_file)
                visual_content = image
                st.image(image, caption="Uploaded Image", width=200)
                st.success("✓ Image loaded")
                
                # Analyze immediately based on selected mode
                with st.spinner("Analyzing image..."):
                    if image_analysis_mode == "Hugging Face" and hf_token:
                        image_analysis = analyze_image_with_huggingface(
                            image, 
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            hf_token
                        )
                    elif image_analysis_mode == "Together.ai" and together_key:
                        image_analysis = analyze_image_with_togetherai(
                            image,
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            together_key
                        )
                    elif image_analysis_mode == "OpenRouter" and openrouter_key:
                        image_analysis = analyze_image_with_openrouter(
                            image,
                            "Analyze this physics image. What concepts, diagrams, or equations do you see?",
                            openrouter_key
                        )
                    else:
                        image_analysis = analyze_image_local_fallback(
                            image,
                            "Analyze this physics image"
                        )
                        
                if image_analysis:
                    st.info(f"📋 Analysis: {image_analysis[:150]}...")
                    
            except Exception as e:
                st.error(f"Upload error: {e}")
    
    st.divider()
    
    # Controls
    if st.button("🧹 Clear Chat History", use_container_width=True):
        st.session_state.messages = []
        st.rerun()
    
    st.divider()
    st.caption("H2 Physics 9478 | AI Image Analysis")
    st.caption("Made with ❤️ for JPJC H2 Physics students | Powered by Groq AI")

# 7 Main Chat Interface
# -----------------------------------------------------------------------------
# 7. MAIN CHAT INTERFACE
# -----------------------------------------------------------------------------

# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "system", "content": SYSTEM_INSTRUCTIONS},
        {"role": "assistant", "content": "**Hello! I'm Richard Feynman, ready to help you master H2 Physics!** ⚛️\n\nI can:\n- 📊 **Plot graphs** with Python\n- 🖼️ **Find diagrams** online\n- 📷 **Analyze images** from camera/upload\n- 💬 **Explain concepts** with analogies\n\n**Try:**\n1. Take a photo of a physics problem\n2. Ask me to plot a graph\n3. Request a diagram\n\n**What would you like to learn today?**"}
    ]

# Title
st.title("⚛️ H2 Physics Feynman Tutor")
st.caption(f"**Topic:** {topic} | **Image Analysis:** {image_analysis_mode}")

# Display chat history
for message in st.session_state.messages:
    if message["role"] != "system":
        display_message(message["role"], message["content"], enable_voice)

# Chat input
user_input = st.chat_input("Type your question or describe the image...")

# Process input
if user_input or visual_content or image_analysis:
    # Build user message
    user_message = ""
    
    if user_input:
        user_message += user_input + " "
    
    if visual_content:
        user_message += "[I have uploaded/taken a photo of a physics problem] "
    
    if image_analysis:
        user_message += f"[Image Analysis: {image_analysis}] "
    
    if topic != "General / Any":
        user_message += f"(Focus on: {topic})"
    
    # Add to history
    st.session_state.messages.append({"role": "user", "content": user_message})
    
    # Display user message
    with st.chat_message("user"):
        if user_input:
            st.markdown(user_input)
        
        if visual_content:
            st.image(visual_content, caption="Your Image", width=300)
        
        if image_analysis:
            with st.expander("📋 Image Analysis Details"):
                st.write(image_analysis)
    
    # Check for Groq API key - FIXED VERSION
    groq_key = None
    try:
        # First try environment variable (Hugging Face method)
        groq_key = os.environ.get("GROQ_API_KEY")
        
        # If not found in env, try to get from st.secrets (only if it exists)
        if not groq_key and hasattr(st, 'secrets'):
            try:
                # Convert secrets to dict safely
                secrets_dict = dict(st.secrets)
                if "GROQ_API_KEY" in secrets_dict:
                    groq_key = secrets_dict["GROQ_API_KEY"]
            except:
                # st.secrets exists but can't be accessed
                pass
    except:
        pass
    
    if not groq_key:
        st.error(
            "**❌ API Configuration Required**\n\n"
            "Groq API key not found. Please ensure GROQ_API_KEY is set in:\n"
            "1. Hugging Face Space Secrets (recommended)\n"
            "2. Environment variables\n\n"
            "Get free API key: console.groq.com"
        )
        st.stop()
    
    # Prepare API call
    try:
        conversation_context = []
        conversation_context.append({"role": "system", "content": SYSTEM_INSTRUCTIONS})
        
        recent_messages = st.session_state.messages[-8:]
        for msg in recent_messages:
            if msg["role"] != "system":
                conversation_context.append(msg)
        
        # Call Groq API
        with st.spinner("Feynman is thinking... ⚛️"):
            response_text = call_groq_api(groq_key, conversation_context)
        
        if response_text:
            st.session_state.messages.append({"role": "assistant", "content": response_text})
            display_message("assistant", response_text, enable_voice)
            
            # Clear image content after processing
            if 'visual_content' in locals():
                visual_content = None
            if 'image_analysis' in locals():
                image_analysis = None
                
        else:
            st.error("Failed to get response. Please try again.")
            
    except Exception as e:
        st.error(f"Error: {str(e)[:200]}")

# Footer
st.divider()
st.markdown("**H2 Physics Feynman Tutor** | Singapore H2 Physics (9478) Syllabus")
st.markdown("Powered by Groq AI + AI Image Analysis")
st.markdown("*AI tutoring assistant. Verify with official syllabus.*")