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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -6,7 +6,11 @@ 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")
@@ -89,34 +93,42 @@ def get_app_py_content(space_id: str) -> str:
89
  except requests.RequestException:
90
  return f"Error fetching app.py content for space: {space_id}"
91
 
92
- def on_select(space):
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,18 +158,16 @@ def create_ui():
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
-
160
-
161
  return demo
162
 
163
  except Exception as e:
 
6
  import traceback
7
  from PIL import Image
8
  from io import BytesIO
9
+ import asyncio
10
+ from selenium import webdriver
11
+ from selenium.webdriver.chrome.service import Service
12
+ from webdriver_manager.chrome import ChromeDriverManager
13
+ from selenium.webdriver.chrome.options import Options
14
 
15
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
 
93
  except requests.RequestException:
94
  return f"Error fetching app.py content for space: {space_id}"
95
 
96
+ async def on_select(space):
97
  try:
98
  summary = summarize_space(space)
99
  app_content = get_app_py_content(space['id'])
100
+ screenshot = await take_screenshot(space['url'])
101
  info = f"์„ ํƒ๋œ Space: {space['name']} (ID: {space['id']})\n"
102
  info += f"Author: {space['author']}\n"
103
  info += f"Likes: {space['likes']}\n"
104
  info += f"URL: {space['url']}\n\n"
105
  info += f"์š”์•ฝ:\n{summary}"
106
+ return info, app_content, screenshot
107
  except Exception as e:
108
  print(f"Error in on_select: {str(e)}")
109
  print(traceback.format_exc())
110
+ return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", Image.new('RGB', (1080, 720), color='lightgray')
111
+
112
+ async def take_screenshot(url):
113
+ options = webdriver.ChromeOptions()
114
+ options.add_argument('--headless')
115
+ options.add_argument('--no-sandbox')
116
+ options.add_argument('--disable-dev-shm-usage')
117
 
 
118
  try:
119
+ service = Service(ChromeDriverManager().install())
120
+ driver = webdriver.Chrome(service=service, options=options)
121
+ driver.set_window_size(1080, 720)
122
+ driver.get(url)
123
+ await asyncio.sleep(5) # Wait for the page to load
124
+ screenshot = driver.get_screenshot_as_png()
125
+ return Image.open(BytesIO(screenshot))
126
  except Exception as e:
127
+ print(f"Screenshot error: {str(e)}")
128
+ return Image.new('RGB', (1080, 720), color='lightgray')
129
+ finally:
130
+ if 'driver' in locals():
131
+ driver.quit()
132
 
133
  def create_ui():
134
  try:
 
158
 
159
  with gr.Column(scale=1):
160
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", lines=20)
161
+ screenshot_output = gr.Image(type="pil", label="Space ์Šคํฌ๋ฆฐ์ƒท", 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: asyncio.run(on_select(s)),
167
  inputs=[],
168
+ outputs=[info_output, app_py_content, screenshot_output]
169
  )
170
 
 
 
171
  return demo
172
 
173
  except Exception as e: