laichai commited on
Commit
0621187
·
verified ·
1 Parent(s): 8b877e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -157
app.py CHANGED
@@ -432,15 +432,16 @@ Example: "Here's the setup: [IMAGE: double slit experiment diagram]"
432
 
433
  ## **PART 6: SIDEBAR CONFIGURATION**
434
  # Sidebar Configuration
 
435
  with st.sidebar:
436
  # Header with Feynman image
437
  col1, col2 = st.columns([1, 3])
438
  with col1:
439
  st.image("https://upload.wikimedia.org/wikipedia/en/4/42/Richard_Feynman_Nobel.jpg",
440
- use_container_width=True)
441
  with col2:
442
- st.markdown("### ⚛️ H2 Physics Tutor")
443
- st.caption("Richard Feynman")
444
 
445
  st.divider()
446
 
@@ -470,135 +471,7 @@ with st.sidebar:
470
 
471
  st.divider()
472
 
473
- # API Configuration
474
- st.header("🔑 API Configuration")
475
-
476
- # Groq API Key
477
- api_key = None
478
- if "GROQ_API_KEY" in os.environ:
479
- api_key = os.environ["GROQ_API_KEY"]
480
- st.success("✓ API Key loaded from environment")
481
- elif "GROQ_API_KEY" in st.secrets:
482
- api_key = st.secrets["GROQ_API_KEY"]
483
- st.success("✓ API Key loaded from secrets")
484
- else:
485
- api_key = st.text_input(
486
- "Groq API Key",
487
- type="password",
488
- help="Get your free API key from console.groq.com",
489
- placeholder="gsk_xxxxxxxxxxxxxxxxxxxxxxxx"
490
- )
491
-
492
- # API Key Info - SIMPLIFIED VERSION WITHOUT EMOJI
493
- st.info(
494
- "**FREE TIER AVAILABLE!**\n\n"
495
- "**How to get your FREE API key:**\n"
496
- "1. Visit [console.groq.com](https://console.groq.com)\n"
497
- "2. Sign up with Google/GitHub (free)\n"
498
- "3. Go to API Keys\n"
499
- "4. Create new key\n"
500
- "5. Copy and paste here\n\n"
501
- "**Free Tier:** 10,000 requests/month\n"
502
- "**Models:** Llama 3.1 8B Instant (fast & free)"
503
- )
504
-
505
- # Image Search Status
506
- # Image Search Status - FIXED DETECTION
507
- st.divider()
508
- st.header("🖼️ Image Search")
509
-
510
- # Debug: Let's see what secrets are actually available
511
- google_key_found = False
512
- google_cx_found = False
513
-
514
- # Check in multiple ways
515
- try:
516
- # Method 1: Check st.secrets directly
517
- if hasattr(st, 'secrets'):
518
- st.write("**Debug - Checking st.secrets:**")
519
- all_secrets = list(st.secrets.keys())
520
- st.write(f"Available secrets: {all_secrets}")
521
-
522
- google_key_found = "GOOGLE_SEARCH_KEY" in st.secrets
523
- google_cx_found = "GOOGLE_CX" in st.secrets
524
-
525
- if google_key_found:
526
- key_preview = str(st.secrets["GOOGLE_SEARCH_KEY"])[:10] + "..."
527
- st.write(f"✓ GOOGLE_SEARCH_KEY: {key_preview}")
528
- else:
529
- st.write("✗ GOOGLE_SEARCH_KEY not found")
530
-
531
- if google_cx_found:
532
- cx_preview = str(st.secrets["GOOGLE_CX"])[:10] + "..."
533
- st.write(f"✓ GOOGLE_CX: {cx_preview}")
534
- else:
535
- st.write("✗ GOOGLE_CX not found")
536
-
537
- except Exception as e:
538
- st.write(f"Error checking secrets: {e}")
539
-
540
- # Also check environment variables
541
- env_key = os.environ.get("GOOGLE_SEARCH_KEY")
542
- env_cx = os.environ.get("GOOGLE_CX")
543
-
544
- if env_key:
545
- google_key_found = True
546
- st.write(f"✓ GOOGLE_SEARCH_KEY found in environment")
547
- if env_cx:
548
- google_cx_found = True
549
- st.write(f"✓ GOOGLE_CX found in environment")
550
-
551
- # Determine actual status
552
- if google_key_found and google_cx_found:
553
- st.success("✅ Google Image Search IS CONFIGURED!")
554
- st.caption("Your secrets are working. Images come from Google.")
555
-
556
- # Quick test
557
- with st.spinner("Testing connection..."):
558
- try:
559
- test_url = search_image("physics")
560
- if test_url and "http" in str(test_url):
561
- st.success("✓ Google API connection successful!")
562
- st.caption(f"Test result: {test_url[:50]}...")
563
- else:
564
- st.warning("⚠️ API returned no image")
565
- except Exception as e:
566
- st.error(f"❌ API test failed: {str(e)[:100]}")
567
- else:
568
- st.warning("⚠️ Google Image Search NOT configured")
569
-
570
- if google_key_found and not google_cx_found:
571
- st.error("Missing: GOOGLE_CX (Search Engine ID)")
572
- elif not google_key_found and google_cx_found:
573
- st.error("Missing: GOOGLE_SEARCH_KEY (API Key)")
574
- else:
575
- st.error("Missing both: GOOGLE_SEARCH_KEY and GOOGLE_CX")
576
-
577
- st.info("""
578
- **Your secrets should be named exactly:**
579
- - `GOOGLE_SEARCH_KEY` = "AIzaSyB...YourAPIKey"
580
- - `GOOGLE_CX` = "0123456789abcdef:your-cx-id"
581
-
582
- **Not:** `GOOGLE_API_KEY`, `GOOGLE_KEY`, `CX_ID`, etc.
583
- """)
584
-
585
- st.caption("Tip: Ask for diagrams like '[IMAGE: double slit experiment]'")
586
- # Quick test button
587
- if api_key and st.button("�� Test API Key", use_container_width=True):
588
- with st.spinner("Testing connection..."):
589
- test_messages = [
590
- {"role": "system", "content": "You are a test assistant."},
591
- {"role": "user", "content": "Say 'API is working!' if you can hear me."}
592
- ]
593
- test_response = call_groq_api(api_key, test_messages, max_tokens=50)
594
- if "API is working" in test_response or "working" in test_response.lower():
595
- st.success("✅ API Key is working!")
596
- else:
597
- st.error(f"❌ API test failed: {test_response[:100]}")
598
-
599
- st.divider()
600
-
601
- # Media Input
602
  st.header("📤 Upload Content")
603
 
604
  visual_content = None
@@ -612,37 +485,21 @@ with st.sidebar:
612
  try:
613
  image = Image.open(uploaded_file)
614
  visual_content = image
615
- st.image(image, caption="Uploaded Image", width="auto")
616
- st.success("Image loaded successfully")
617
  except Exception as e:
618
- st.error(f"Error loading image: {e}")
619
 
620
  st.divider()
621
 
622
- # Controls
623
- col1, col2 = st.columns(2)
624
- with col1:
625
- if st.button("🧹 Clear Chat", use_container_width=True):
626
- st.session_state.messages = []
627
- st.rerun()
628
-
629
- with col2:
630
- if st.button("📋 Export Chat", use_container_width=True):
631
- if "messages" in st.session_state:
632
- chat_text = "=== H2 Physics Chat History ===\n\n"
633
- for msg in st.session_state.messages:
634
- if msg["role"] != "system":
635
- chat_text += f"{msg['role'].upper()}: {msg['content']}\n\n"
636
- st.download_button(
637
- "Download Chat",
638
- chat_text,
639
- file_name="physics_chat_history.txt",
640
- mime="text/plain",
641
- use_container_width=True
642
- )
643
 
644
  st.divider()
645
- st.caption("Made with ❤️ for H2 Physics students | Powered by Groq AI")
 
646
 
647
  # 7 Main Chat Interface
648
  if "messages" not in st.session_state:
 
432
 
433
  ## **PART 6: SIDEBAR CONFIGURATION**
434
  # Sidebar Configuration
435
+ # Sidebar Configuration - SIMPLIFIED
436
  with st.sidebar:
437
  # Header with Feynman image
438
  col1, col2 = st.columns([1, 3])
439
  with col1:
440
  st.image("https://upload.wikimedia.org/wikipedia/en/4/42/Richard_Feynman_Nobel.jpg",
441
+ width=80)
442
  with col2:
443
+ st.markdown("### ⚛️ H2 Physics")
444
+ st.caption("Richard Feynman Tutor")
445
 
446
  st.divider()
447
 
 
471
 
472
  st.divider()
473
 
474
+ # Media Input (Kept simple)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
  st.header("📤 Upload Content")
476
 
477
  visual_content = None
 
485
  try:
486
  image = Image.open(uploaded_file)
487
  visual_content = image
488
+ st.image(image, caption="Uploaded Image", width=200)
489
+ st.success("Image loaded")
490
  except Exception as e:
491
+ st.error(f"Error: {e}")
492
 
493
  st.divider()
494
 
495
+ # Simple Controls
496
+ if st.button("🧹 Clear Chat History", use_container_width=True):
497
+ st.session_state.messages = []
498
+ st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
 
500
  st.divider()
501
+ #st.caption("H2 Physics (9478) Syllabus")
502
+ st.caption("Made with ❤️ for JPJC H2 Physics students | Powered by Groq AI")
503
 
504
  # 7 Main Chat Interface
505
  if "messages" not in st.session_state: