ginipick commited on
Commit
eafe0e0
Β·
verified Β·
1 Parent(s): 44bda5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -594,6 +594,25 @@ def process_uploaded_files(files):
594
  return result
595
 
596
  # ──────────────────────────────── Image & Utility ─────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
  def generate_image(prompt, w=768, h=768, g=3.5, steps=30, seed=3):
598
  """Image generation function (via Gradio endpoint)."""
599
  if not prompt:
@@ -815,7 +834,8 @@ def perplexity_app():
815
  img_url = img_data.get('url', '')
816
  caption = img_data.get('title', 'Related image')
817
  if img_url:
818
- st.image(img_url, caption=caption, use_column_width=True)
 
819
  if img_data.get('source'):
820
  st.markdown(f"[Source]({img_data['source']})")
821
  except Exception as img_err:
@@ -834,7 +854,8 @@ def perplexity_app():
834
  col1, col2 = st.columns([1, 3])
835
  with col1:
836
  try:
837
- st.image(thumbnail, width=120)
 
838
  except:
839
  st.write("🎬")
840
  with col2:
@@ -908,7 +929,6 @@ def process_input(query: str, uploaded_files):
908
  file_content = process_uploaded_files(uploaded_files)
909
 
910
  # μ΅œμ’…μ μœΌλ‘œ μ‚¬μš©ν•  이미지/λΉ„λ””μ˜€ λͺ©λ‘ ꡬ성
911
- # (μ΄λ²ˆμ—λŠ” "fallback" 없이 Brave 검색 결과만 μ‚¬μš©)
912
  valid_images = []
913
  for img in image_results:
914
  url = img.get('image_url')
@@ -991,7 +1011,7 @@ def process_input(query: str, uploaded_files):
991
  # μ΅œμ’… 응닡 ν‘œμ‹œ
992
  message_placeholder.markdown(full_response, unsafe_allow_html=True)
993
 
994
- # μ‹€μ œ κ²€μƒ‰λœ 이미지λ₯Ό UI에 ν‘œμ‹œ
995
  if valid_images:
996
  st.subheader("Related Images")
997
  image_cols = st.columns(min(3, len(valid_images)))
@@ -1002,13 +1022,13 @@ def process_input(query: str, uploaded_files):
1002
  with image_cols[col_idx]:
1003
  img_url = img_data['url']
1004
  caption = img_data['title']
1005
- st.image(img_url, caption=caption, use_column_width=True)
1006
  if img_data.get('source'):
1007
  st.markdown(f"[Source]({img_data['source']})")
1008
  except Exception as img_err:
1009
  logging.warning(f"Error displaying image: {img_err}")
1010
 
1011
- # μ‹€μ œ κ²€μƒ‰λœ λΉ„λ””μ˜€λ₯Ό UI에 ν‘œμ‹œ
1012
  if valid_videos:
1013
  st.subheader("Related Videos")
1014
  for video in valid_videos:
@@ -1021,7 +1041,7 @@ def process_input(query: str, uploaded_files):
1021
  col1, col2 = st.columns([1, 3])
1022
  with col1:
1023
  try:
1024
- st.image(thumbnail, width=120)
1025
  except:
1026
  st.write("🎬")
1027
  with col2:
 
594
  return result
595
 
596
  # ──────────────────────────────── Image & Utility ─────────────────────────
597
+
598
+ def load_and_show_image(img_url: str, caption: str = "Image"):
599
+ """
600
+ 이미지 URL이 κΉ¨μ Έμžˆκ±°λ‚˜ 직접 ν‘œμ‹œκ°€ μ•ˆ λ˜λŠ” 경우λ₯Ό λŒ€λΉ„ν•΄
601
+ requests둜 λ‹€μš΄λ‘œλ“œ ν›„ ν‘œμ‹œν•˜λŠ” ν•¨μˆ˜.
602
+ """
603
+ try:
604
+ # 이미지 λ‹€μš΄λ‘œλ“œ
605
+ response = requests.get(img_url, timeout=10)
606
+ response.raise_for_status()
607
+
608
+ # λ©”λͺ¨λ¦¬μ—μ„œ 이미지 μ—΄κΈ°
609
+ image = Image.open(BytesIO(response.content))
610
+
611
+ # Streamlit으둜 ν‘œμ‹œ
612
+ st.image(image, caption=caption, use_column_width=True)
613
+ except Exception as e:
614
+ st.warning(f"이미지 λ‘œλ”© μ‹€νŒ¨: {e}")
615
+
616
  def generate_image(prompt, w=768, h=768, g=3.5, steps=30, seed=3):
617
  """Image generation function (via Gradio endpoint)."""
618
  if not prompt:
 
834
  img_url = img_data.get('url', '')
835
  caption = img_data.get('title', 'Related image')
836
  if img_url:
837
+ # β–Ά μ—¬κΈ°μ„œ load_and_show_image ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μš΄λ‘œλ“œ ν›„ ν‘œμ‹œ
838
+ load_and_show_image(img_url, caption=caption)
839
  if img_data.get('source'):
840
  st.markdown(f"[Source]({img_data['source']})")
841
  except Exception as img_err:
 
854
  col1, col2 = st.columns([1, 3])
855
  with col1:
856
  try:
857
+ # 썸넀일도 λ‹€μš΄λ‘œλ“œ ν›„ ν‘œμ‹œ
858
+ load_and_show_image(thumbnail, caption="Video Thumbnail")
859
  except:
860
  st.write("🎬")
861
  with col2:
 
929
  file_content = process_uploaded_files(uploaded_files)
930
 
931
  # μ΅œμ’…μ μœΌλ‘œ μ‚¬μš©ν•  이미지/λΉ„λ””μ˜€ λͺ©λ‘ ꡬ성
 
932
  valid_images = []
933
  for img in image_results:
934
  url = img.get('image_url')
 
1011
  # μ΅œμ’… 응닡 ν‘œμ‹œ
1012
  message_placeholder.markdown(full_response, unsafe_allow_html=True)
1013
 
1014
+ # μ‹€μ œ κ²€μƒ‰λœ 이미지λ₯Ό UI에 ν‘œμ‹œ (load_and_show_image μ‚¬μš©)
1015
  if valid_images:
1016
  st.subheader("Related Images")
1017
  image_cols = st.columns(min(3, len(valid_images)))
 
1022
  with image_cols[col_idx]:
1023
  img_url = img_data['url']
1024
  caption = img_data['title']
1025
+ load_and_show_image(img_url, caption=caption)
1026
  if img_data.get('source'):
1027
  st.markdown(f"[Source]({img_data['source']})")
1028
  except Exception as img_err:
1029
  logging.warning(f"Error displaying image: {img_err}")
1030
 
1031
+ # μ‹€μ œ κ²€μƒ‰λœ λΉ„λ””μ˜€λ₯Ό UI에 ν‘œμ‹œ (load_and_show_image둜 썸넀일 처리)
1032
  if valid_videos:
1033
  st.subheader("Related Videos")
1034
  for video in valid_videos:
 
1041
  col1, col2 = st.columns([1, 3])
1042
  with col1:
1043
  try:
1044
+ load_and_show_image(thumbnail, caption="Video Thumbnail")
1045
  except:
1046
  st.write("🎬")
1047
  with col2: