ginipick commited on
Commit
730989a
Β·
verified Β·
1 Parent(s): d361381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -3
app.py CHANGED
@@ -5,6 +5,11 @@ import requests
5
  from typing import List, Dict, Union
6
  import traceback
7
 
 
 
 
 
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
10
 
@@ -86,17 +91,39 @@ def on_select(space):
86
  try:
87
  summary = summarize_space(space)
88
  app_content = get_app_py_content(space['id'])
 
89
  info = f"μ„ νƒλœ Space: {space['name']} (ID: {space['id']})\n"
90
  info += f"Author: {space['author']}\n"
91
  info += f"Likes: {space['likes']}\n"
92
  info += f"URL: {space['url']}\n\n"
93
  info += f"μš”μ•½:\n{summary}"
94
- return info, app_content
95
  except Exception as e:
96
  print(f"Error in on_select: {str(e)}")
97
  print(traceback.format_exc())
98
- return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}", ""
 
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  def create_ui():
101
  spaces_list = get_most_liked_spaces()
102
  print(f"Type of spaces_list: {type(spaces_list)}")
@@ -133,8 +160,19 @@ def create_ui():
133
  outputs=[info_output, app_py_content]
134
  )
135
 
136
- return demo
 
 
 
137
 
 
 
 
 
 
 
 
 
138
  if __name__ == "__main__":
139
  demo = create_ui()
140
  demo.launch()
 
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
  HF_TOKEN = os.getenv("HF_TOKEN")
14
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
15
 
 
91
  try:
92
  summary = summarize_space(space)
93
  app_content = get_app_py_content(space['id'])
94
+ screenshot = take_screenshot(space['url'])
95
  info = f"μ„ νƒλœ Space: {space['name']} (ID: {space['id']})\n"
96
  info += f"Author: {space['author']}\n"
97
  info += f"Likes: {space['likes']}\n"
98
  info += f"URL: {space['url']}\n\n"
99
  info += f"μš”μ•½:\n{summary}"
100
+ return info, app_content, screenshot
101
  except Exception as e:
102
  print(f"Error in on_select: {str(e)}")
103
  print(traceback.format_exc())
104
+ return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}", "", Image.new('RGB', (1, 1))
105
+
106
 
107
+ def take_screenshot(url):
108
+ options = webdriver.ChromeOptions()
109
+ options.add_argument('--headless')
110
+ options.add_argument('--no-sandbox')
111
+ options.add_argument('--disable-dev-shm-usage')
112
+
113
+ try:
114
+ wd = webdriver.Chrome(options=options)
115
+ wd.set_window_size(1080, 720)
116
+ wd.get(url)
117
+ wd.implicitly_wait(10)
118
+ screenshot = wd.get_screenshot_as_png()
119
+ except WebDriverException as e:
120
+ return Image.new('RGB', (1, 1))
121
+ finally:
122
+ if wd:
123
+ wd.quit()
124
+
125
+ return Image.open(BytesIO(screenshot))
126
+
127
  def create_ui():
128
  spaces_list = get_most_liked_spaces()
129
  print(f"Type of spaces_list: {type(spaces_list)}")
 
160
  outputs=[info_output, app_py_content]
161
  )
162
 
163
+ with gr.Column(scale=1):
164
+ info_output = gr.Textbox(label="Space 정보 및 μš”μ•½", lines=16)
165
+ app_py_content = gr.Code(language="python", label="app.py λ‚΄μš©")
166
+ screenshot_output = gr.Image(type="pil", label="Live ν™”λ©΄", height=360, width=540)
167
 
168
+ for _, button, space in space_rows:
169
+ button.click(
170
+ lambda s=space: on_select(s),
171
+ inputs=[],
172
+ outputs=[info_output, app_py_content, screenshot_output]
173
+ )
174
+
175
+ return demo
176
  if __name__ == "__main__":
177
  demo = create_ui()
178
  demo.launch()