laichai commited on
Commit
c2dc880
·
verified ·
1 Parent(s): f8a9484

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -14
app.py CHANGED
@@ -86,22 +86,49 @@ def duckduckgo_search_api(query):
86
 
87
  @st.cache_data(show_spinner=False, ttl=300)
88
  def search_image(query):
89
- """MASTER FUNCTION: Google Search -> DuckDuckGo"""
90
- cx = os.environ.get("GOOGLE_CX", st.secrets.get("GOOGLE_CX", ""))
91
-
92
- key1 = os.environ.get("GOOGLE_SEARCH_KEY", st.secrets.get("GOOGLE_SEARCH_KEY", ""))
93
- if key1 and cx:
94
- url = google_search_api(query, key1, cx)
95
- if url:
96
- return url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- key2 = os.environ.get("GOOGLE_SEARCH_KEY_2", st.secrets.get("GOOGLE_SEARCH_KEY_2", ""))
99
- if key2 and cx:
100
- url = google_search_api(query, key2, cx)
101
- if url:
102
- return url
103
 
104
- return duckduckgo_search_api(query)
 
 
 
 
105
 
106
  # Graph plotting function - REORDERED AND FIXED
107
  def execute_plotting_code(code_snippet):
@@ -457,7 +484,37 @@ with st.sidebar:
457
  "**Free Tier:** 10,000 requests/month\n"
458
  "**Models:** Llama 3.1 8B Instant (fast & free)"
459
  )
 
 
 
 
460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461
  # Quick test button
462
  if api_key and st.button("🔍 Test API Key", use_container_width=True):
463
  with st.spinner("Testing connection..."):
 
86
 
87
  @st.cache_data(show_spinner=False, ttl=300)
88
  def search_image(query):
89
+ """MASTER FUNCTION: Google Search -> DuckDuckGo - FIXED FOR HUGGING FACE"""
90
+ try:
91
+ # Try to get secrets from Hugging Face Spaces
92
+ # Method 1: Check st.secrets (Hugging Face Spaces method)
93
+ try:
94
+ if hasattr(st, 'secrets'):
95
+ # Hugging Face Spaces stores secrets in st.secrets
96
+ cx = st.secrets.get("GOOGLE_CX", "")
97
+ key1 = st.secrets.get("GOOGLE_SEARCH_KEY", "")
98
+ key2 = st.secrets.get("GOOGLE_SEARCH_KEY_2", "")
99
+
100
+ if key1 and cx:
101
+ url = google_search_api(query, key1, cx)
102
+ if url:
103
+ return url
104
+
105
+ if key2 and cx:
106
+ url = google_search_api(query, key2, cx)
107
+ if url:
108
+ return url
109
+ except:
110
+ pass
111
+
112
+ # Method 2: Check environment variables
113
+ cx = os.environ.get("GOOGLE_CX", "")
114
+ key1 = os.environ.get("GOOGLE_SEARCH_KEY", "")
115
+ key2 = os.environ.get("GOOGLE_SEARCH_KEY_2", "")
116
+
117
+ if key1 and cx:
118
+ url = google_search_api(query, key1, cx)
119
+ if url:
120
+ return url
121
 
122
+ if key2 and cx:
123
+ url = google_search_api(query, key2, cx)
124
+ if url:
125
+ return url
 
126
 
127
+ # Method 3: Fallback to DuckDuckGo
128
+ return duckduckgo_search_api(query)
129
+
130
+ except Exception as e:
131
+ return f"Search Error: {str(e)[:100]}"
132
 
133
  # Graph plotting function - REORDERED AND FIXED
134
  def execute_plotting_code(code_snippet):
 
484
  "**Free Tier:** 10,000 requests/month\n"
485
  "**Models:** Llama 3.1 8B Instant (fast & free)"
486
  )
487
+
488
+ # Image Search Status
489
+ st.divider()
490
+ st.header("🖼️ Image Search")
491
 
492
+ # Check if Google Search is configured
493
+ google_configured = False
494
+ try:
495
+ if hasattr(st, 'secrets'):
496
+ if st.secrets.get("GOOGLE_SEARCH_KEY") and st.secrets.get("GOOGLE_CX"):
497
+ google_configured = True
498
+ elif os.environ.get("GOOGLE_SEARCH_KEY") and os.environ.get("GOOGLE_CX"):
499
+ google_configured = True
500
+ except:
501
+ pass
502
+
503
+ if google_configured:
504
+ st.success("✅ Google Image Search configured")
505
+ st.caption("Will use Google Custom Search API")
506
+ else:
507
+ st.warning("⚠️ Google Image Search not configured")
508
+ st.caption("Will use DuckDuckGo (free but limited)")
509
+ st.info("""
510
+ To enable Google Image Search:
511
+ 1. Go to [Google Cloud Console](https://console.cloud.google.com)
512
+ 2. Enable Custom Search API
513
+ 3. Get API Key and Search Engine ID (CX)
514
+ 4. Add to Hugging Face Secrets:
515
+ - GOOGLE_SEARCH_KEY = "your-api-key"
516
+ - GOOGLE_CX = "your-search-engine-id"
517
+ """)
518
  # Quick test button
519
  if api_key and st.button("🔍 Test API Key", use_container_width=True):
520
  with st.spinner("Testing connection..."):