Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,7 @@ import traceback
|
|
7 |
from PIL import Image
|
8 |
from io import BytesIO
|
9 |
import asyncio
|
10 |
-
from
|
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 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
@@ -92,11 +89,11 @@ def get_app_py_content(space_id: str) -> str:
|
|
92 |
except requests.RequestException:
|
93 |
return f"Error fetching app.py content for space: {space_id}"
|
94 |
|
95 |
-
|
96 |
try:
|
97 |
summary = summarize_space(space)
|
98 |
app_content = get_app_py_content(space['id'])
|
99 |
-
screenshot =
|
100 |
info = f"์ ํ๋ Space: {space['name']} (ID: {space['id']})\n"
|
101 |
info += f"Author: {space['author']}\n"
|
102 |
info += f"Likes: {space['likes']}\n"
|
@@ -107,27 +104,15 @@ async def on_select(space):
|
|
107 |
print(f"Error in on_select: {str(e)}")
|
108 |
print(traceback.format_exc())
|
109 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", Image.new('RGB', (1080, 720), color='lightgray')
|
110 |
-
|
111 |
-
|
112 |
-
options = Options()
|
113 |
-
options.add_argument('--headless')
|
114 |
-
options.add_argument('--no-sandbox')
|
115 |
-
options.add_argument('--disable-dev-shm-usage')
|
116 |
-
|
117 |
try:
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
driver.get(url)
|
122 |
-
await asyncio.sleep(5) # Wait for the page to load
|
123 |
-
screenshot = driver.get_screenshot_as_png()
|
124 |
-
return Image.open(BytesIO(screenshot))
|
125 |
except Exception as e:
|
126 |
print(f"Screenshot error: {str(e)}")
|
127 |
return Image.new('RGB', (1080, 720), color='lightgray')
|
128 |
-
finally:
|
129 |
-
if 'driver' in locals():
|
130 |
-
driver.quit()
|
131 |
|
132 |
def create_ui():
|
133 |
try:
|
@@ -162,11 +147,12 @@ def create_ui():
|
|
162 |
|
163 |
for _, button, space in space_rows:
|
164 |
button.click(
|
165 |
-
lambda s=space:
|
166 |
-
|
167 |
-
|
168 |
)
|
169 |
|
|
|
170 |
return demo
|
171 |
|
172 |
except Exception as e:
|
|
|
7 |
from PIL import Image
|
8 |
from io import BytesIO
|
9 |
import asyncio
|
10 |
+
from gradio_client import Client
|
|
|
|
|
|
|
11 |
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
|
|
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 |
+
screenshot = take_screenshot(space['url'])
|
97 |
info = f"์ ํ๋ Space: {space['name']} (ID: {space['id']})\n"
|
98 |
info += f"Author: {space['author']}\n"
|
99 |
info += f"Likes: {space['likes']}\n"
|
|
|
104 |
print(f"Error in on_select: {str(e)}")
|
105 |
print(traceback.format_exc())
|
106 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", Image.new('RGB', (1080, 720), color='lightgray')
|
107 |
+
|
108 |
+
def take_screenshot(url):
|
|
|
|
|
|
|
|
|
|
|
109 |
try:
|
110 |
+
client = Client("ginipick/selenium-screenshot-gradio")
|
111 |
+
result = client.predict(url=url, api_name="/predict")
|
112 |
+
return Image.open(result)
|
|
|
|
|
|
|
|
|
113 |
except Exception as e:
|
114 |
print(f"Screenshot error: {str(e)}")
|
115 |
return Image.new('RGB', (1080, 720), color='lightgray')
|
|
|
|
|
|
|
116 |
|
117 |
def create_ui():
|
118 |
try:
|
|
|
147 |
|
148 |
for _, button, space in space_rows:
|
149 |
button.click(
|
150 |
+
lambda s=space: on_select(s),
|
151 |
+
inputs=[],
|
152 |
+
outputs=[info_output, app_py_content, screenshot_output]
|
153 |
)
|
154 |
|
155 |
+
|
156 |
return demo
|
157 |
|
158 |
except Exception as e:
|