Update app.py
Browse files
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 |
-
|
| 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,
|
| 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', (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
def get_space_thumbnail(space_id: str) -> Image.Image:
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
except Exception as e:
|
| 118 |
-
print(f"
|
| 119 |
-
return Image.new('RGB', (
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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,
|
| 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:
|