ginipick commited on
Commit
4a3975f
ยท
verified ยท
1 Parent(s): 990edf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -27
app.py CHANGED
@@ -4,14 +4,9 @@ import os
4
  import requests
5
  from typing import List, Dict, Union
6
  import traceback
7
-
8
- from selenium import webdriver
9
- from selenium.common.exceptions import WebDriverException
10
  from PIL import Image
11
  from io import BytesIO
12
 
13
- from playwright.sync_api import sync_playwright
14
- from selenium.webdriver.chrome.options import Options
15
 
16
 
17
  HF_TOKEN = os.getenv("HF_TOKEN")
@@ -98,37 +93,30 @@ def on_select(space):
98
  try:
99
  summary = summarize_space(space)
100
  app_content = get_app_py_content(space['id'])
101
- screenshot = take_screenshot(space['url'])
102
  info = f"์„ ํƒ๋œ Space: {space['name']} (ID: {space['id']})\n"
103
  info += f"Author: {space['author']}\n"
104
  info += f"Likes: {space['likes']}\n"
105
  info += f"URL: {space['url']}\n\n"
106
  info += f"์š”์•ฝ:\n{summary}"
107
- return info, app_content, screenshot
108
  except Exception as e:
109
  print(f"Error in on_select: {str(e)}")
110
  print(traceback.format_exc())
111
- return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", Image.new('RGB', (1, 1))
112
 
113
- def take_screenshot(url):
114
  try:
115
- chrome_options = Options()
116
- chrome_options.add_argument("--headless")
117
- chrome_options.add_argument("--no-sandbox")
118
- chrome_options.add_argument("--disable-dev-shm-usage")
119
- chrome_options.add_argument("--disable-gpu")
120
- chrome_options.add_argument("--window-size=1080,720")
121
-
122
- driver = webdriver.Chrome(options=chrome_options)
123
-
124
- driver.get(url)
125
- screenshot = driver.get_screenshot_as_png()
126
- driver.quit()
127
-
128
- return Image.open(BytesIO(screenshot))
129
  except Exception as e:
130
- print(f"Screenshot error: {str(e)}")
131
- return Image.new('RGB', (1, 1))
132
 
133
  def create_ui():
134
  try:
@@ -158,14 +146,14 @@ def create_ui():
158
 
159
  with gr.Column(scale=1):
160
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", lines=20)
161
- screenshot_output = gr.Image(type="pil", label="Live ํ™”๋ฉด", height=360, width=540)
162
  app_py_content = gr.Code(language="python", label="app.py ๋‚ด์šฉ")
163
 
164
  for _, button, space in space_rows:
165
  button.click(
166
  lambda s=space: on_select(s),
167
  inputs=[],
168
- outputs=[info_output, app_py_content, screenshot_output]
169
  )
170
 
171
 
 
4
  import requests
5
  from typing import List, Dict, Union
6
  import traceback
 
 
 
7
  from PIL import Image
8
  from io import BytesIO
9
 
 
 
10
 
11
 
12
  HF_TOKEN = os.getenv("HF_TOKEN")
 
93
  try:
94
  summary = summarize_space(space)
95
  app_content = get_app_py_content(space['id'])
96
+ thumbnail = get_space_thumbnail(space['id'])
97
  info = f"์„ ํƒ๋œ Space: {space['name']} (ID: {space['id']})\n"
98
  info += f"Author: {space['author']}\n"
99
  info += f"Likes: {space['likes']}\n"
100
  info += f"URL: {space['url']}\n\n"
101
  info += f"์š”์•ฝ:\n{summary}"
102
+ return info, app_content, thumbnail
103
  except Exception as e:
104
  print(f"Error in on_select: {str(e)}")
105
  print(traceback.format_exc())
106
+ return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", Image.new('RGB', (400, 300), color='lightgray')
107
 
108
+ def get_space_thumbnail(space_id: str) -> Image.Image:
109
  try:
110
+ thumbnail_url = f"https://huggingface.co/spaces/{space_id}/resolve/main/thumbnail.png"
111
+ response = requests.get(thumbnail_url)
112
+ if response.status_code == 200:
113
+ return Image.open(BytesIO(response.content))
114
+ else:
115
+ print(f"Thumbnail not found for space: {space_id}")
116
+ return Image.new('RGB', (400, 300), color='lightgray')
 
 
 
 
 
 
 
117
  except Exception as e:
118
+ print(f"Error fetching thumbnail: {str(e)}")
119
+ return Image.new('RGB', (400, 300), color='lightgray')
120
 
121
  def create_ui():
122
  try:
 
146
 
147
  with gr.Column(scale=1):
148
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", lines=20)
149
+ thumbnail_output = gr.Image(type="pil", label="Space ์ธ๋„ค์ผ", height=300, width=400)
150
  app_py_content = gr.Code(language="python", label="app.py ๋‚ด์šฉ")
151
 
152
  for _, button, space in space_rows:
153
  button.click(
154
  lambda s=space: on_select(s),
155
  inputs=[],
156
+ outputs=[info_output, app_py_content, thumbnail_output]
157
  )
158
 
159