ginipick commited on
Commit
ca6e8dc
ยท
verified ยท
1 Parent(s): 9c9264e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -43
app.py CHANGED
@@ -108,26 +108,16 @@ def create_ui():
108
  formatted_spaces = format_spaces(spaces_list)
109
  print(f"Total spaces loaded: {len(formatted_spaces)}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
110
 
111
- space_choices = {f"{space['name']} by {space['author']} (Likes: {space['likes']})": space['id'] for space in formatted_spaces}
112
 
113
- with gr.Blocks(css="#space-list { max-height: 400px; overflow-y: auto; }") as demo:
114
  gr.Markdown("# Hugging Face Most Liked Spaces")
115
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
- with gr.Column(elem_id="space-list"):
119
- space_radios = []
120
- for space in formatted_spaces:
121
- with gr.Row():
122
- radio = gr.Radio(
123
- choices=[f"{space['name']} by {space['author']} (Likes: {space['likes']})"],
124
- label="",
125
- value=None,
126
- elem_id=f"radio-{space['id']}"
127
- )
128
- space_radios.append(radio)
129
- gr.Button("๐Ÿ”—", elem_id=f"link-{space['id']}")
130
  summarize_btn = gr.Button("์š”์•ฝ")
 
131
 
132
  with gr.Column(scale=2):
133
  output = gr.Textbox(label="Space ์ •๋ณด", lines=10)
@@ -136,14 +126,13 @@ def create_ui():
136
  def on_select(choice):
137
  try:
138
  print(f"Selected: {choice}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
139
- selected_id = space_choices[choice[0]] # choice is now a list with one item
140
- selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
141
  if selected_space:
142
- app_content = get_app_py_content(selected_id)
143
- print(f"Selected space: {selected_space['name']} (ID: {selected_id})") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
144
- return f"์„ ํƒ๋œ Space: {selected_space['name']} (ID: {selected_id})\nURL: {selected_space['url']}", app_content
145
  else:
146
- print(f"Space not found for ID: {selected_id}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
147
  return "์„ ํƒ๋œ space๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", ""
148
  except Exception as e:
149
  print(f"Error in on_select: {str(e)}")
@@ -153,8 +142,7 @@ def create_ui():
153
  def on_summarize(choice):
154
  try:
155
  if choice:
156
- selected_id = space_choices[choice[0]] # choice is now a list with one item
157
- selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
158
  if selected_space:
159
  summary = summarize_space(selected_space)
160
  print(f"Summarizing space: {selected_space['name']}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
@@ -166,28 +154,25 @@ def create_ui():
166
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
167
  return f"์š”์•ฝ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
168
 
169
- for radio in space_radios:
170
- radio.change(on_select, radio, [output, app_py_content])
171
-
172
- summarize_btn.click(on_summarize, inputs=[gr.State(lambda: next((radio for radio in space_radios if radio.value), None))], outputs=[output])
173
-
174
- # JavaScript to open links in new tabs
175
- link_script = """
176
- function setupLinkButtons() {
177
- document.querySelectorAll('button[id^="link-"]').forEach(button => {
178
- button.addEventListener('click', function() {
179
- var spaceId = this.id.split('-')[1];
180
- window.open('https://huggingface.co/spaces/' + spaceId, '_blank');
181
- });
182
- });
183
- }
184
- if (document.readyState === 'complete') {
185
- setupLinkButtons();
186
- } else {
187
- document.addEventListener('DOMContentLoaded', setupLinkButtons);
188
  }
189
- """
190
- demo.load(None, None, None, _js=link_script)
191
 
192
  return demo
193
 
 
108
  formatted_spaces = format_spaces(spaces_list)
109
  print(f"Total spaces loaded: {len(formatted_spaces)}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
110
 
111
+ space_choices = [f"{space['name']} by {space['author']} (Likes: {space['likes']})" for space in formatted_spaces]
112
 
113
+ with gr.Blocks() as demo:
114
  gr.Markdown("# Hugging Face Most Liked Spaces")
115
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
+ space_dropdown = gr.Dropdown(choices=space_choices, label="Select a Space")
 
 
 
 
 
 
 
 
 
 
 
119
  summarize_btn = gr.Button("์š”์•ฝ")
120
+ link_btn = gr.Button("๐Ÿ”— Open Space")
121
 
122
  with gr.Column(scale=2):
123
  output = gr.Textbox(label="Space ์ •๋ณด", lines=10)
 
126
  def on_select(choice):
127
  try:
128
  print(f"Selected: {choice}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
129
+ selected_space = next((space for space in formatted_spaces if f"{space['name']} by {space['author']} (Likes: {space['likes']})" == choice), None)
 
130
  if selected_space:
131
+ app_content = get_app_py_content(selected_space['id'])
132
+ print(f"Selected space: {selected_space['name']} (ID: {selected_space['id']})") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
133
+ return f"์„ ํƒ๋œ Space: {selected_space['name']} (ID: {selected_space['id']})\nURL: {selected_space['url']}", app_content
134
  else:
135
+ print(f"Space not found for choice: {choice}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
136
  return "์„ ํƒ๋œ space๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", ""
137
  except Exception as e:
138
  print(f"Error in on_select: {str(e)}")
 
142
  def on_summarize(choice):
143
  try:
144
  if choice:
145
+ selected_space = next((space for space in formatted_spaces if f"{space['name']} by {space['author']} (Likes: {space['likes']})" == choice), None)
 
146
  if selected_space:
147
  summary = summarize_space(selected_space)
148
  print(f"Summarizing space: {selected_space['name']}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
 
154
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
155
  return f"์š”์•ฝ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
156
 
157
+ def open_space_link(choice):
158
+ if choice:
159
+ selected_space = next((space for space in formatted_spaces if f"{space['name']} by {space['author']} (Likes: {space['likes']})" == choice), None)
160
+ if selected_space:
161
+ return f"<script>window.open('{selected_space['url']}', '_blank');</script>"
162
+ return ""
163
+
164
+ space_dropdown.change(on_select, space_dropdown, [output, app_py_content])
165
+ summarize_btn.click(on_summarize, inputs=[space_dropdown], outputs=[output])
166
+ link_btn.click(open_space_link, inputs=[space_dropdown], outputs=[gr.HTML()])
167
+
168
+ # Add JavaScript to handle link opening
169
+ gr.HTML("""
170
+ <script>
171
+ function openSpaceLink(url) {
172
+ window.open(url, '_blank');
 
 
 
173
  }
174
+ </script>
175
+ """)
176
 
177
  return demo
178