ginipick commited on
Commit
ee1b709
ยท
verified ยท
1 Parent(s): 120baa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -13,9 +13,6 @@ HF_TOKEN = os.getenv("HF_TOKEN")
13
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
14
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
15
 
16
- # ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์„ ํƒ๋œ space ์ •๋ณด ์ €์žฅ
17
- selected_space_info = None
18
-
19
  def get_headers():
20
  if not HF_TOKEN:
21
  raise ValueError("Hugging Face token not found in environment variables")
@@ -111,14 +108,16 @@ def create_ui():
111
  formatted_spaces = format_spaces(spaces_list)
112
  print(f"Total spaces loaded: {len(formatted_spaces)}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
113
 
 
 
114
  with gr.Blocks() as demo:
115
  gr.Markdown("# Hugging Face Most Liked Spaces")
116
 
117
  with gr.Row():
118
  with gr.Column(scale=1):
119
- space_list = gr.List(
120
- [(f"{space['name']} by {space['author']} (Likes: {space['likes']})", space['id']) for space in formatted_spaces],
121
- label="Most Liked Spaces"
122
  )
123
  summarize_btn = gr.Button("์š”์•ฝ")
124
 
@@ -126,14 +125,12 @@ def create_ui():
126
  output = gr.Textbox(label="Space ์ •๋ณด", lines=10)
127
  app_py_content = gr.Code(language="python", label="app.py ๋‚ด์šฉ")
128
 
129
- def on_select(evt: gr.SelectData):
130
- global selected_space_info
131
  try:
132
- print(f"Selection event: {evt}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
133
- selected_id = evt.value[1] # ํŠœํ”Œ์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ space_id์ž…๋‹ˆ๋‹ค.
134
  selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
135
  if selected_space:
136
- selected_space_info = selected_space
137
  app_content = get_app_py_content(selected_id)
138
  print(f"Selected space: {selected_space['name']} (ID: {selected_id})") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
139
  return f"์„ ํƒ๋œ Space: {selected_space['name']} (ID: {selected_id})\nURL: {selected_space['url']}", app_content
@@ -145,13 +142,15 @@ def create_ui():
145
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
146
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", ""
147
 
148
- def on_summarize():
149
- global selected_space_info
150
  try:
151
- if selected_space_info:
152
- summary = summarize_space(selected_space_info)
153
- print(f"Summarizing space: {selected_space_info['name']}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
154
- return summary
 
 
 
155
  print("No space selected for summarization") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
156
  return "์„ ํƒ๋œ space๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฆฌ์ŠคํŠธ์—์„œ space๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
157
  except Exception as e:
@@ -159,8 +158,8 @@ def create_ui():
159
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
160
  return f"์š”์•ฝ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
161
 
162
- space_list.select(on_select, None, [output, app_py_content])
163
- summarize_btn.click(on_summarize, None, output)
164
 
165
  return demo
166
 
 
13
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
14
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
15
 
 
 
 
16
  def get_headers():
17
  if not HF_TOKEN:
18
  raise ValueError("Hugging Face token not found in environment variables")
 
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() as demo:
114
  gr.Markdown("# Hugging Face Most Liked Spaces")
115
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
+ space_radio = gr.Radio(
119
+ choices=list(space_choices.keys()),
120
+ label="Select a Space"
121
  )
122
  summarize_btn = gr.Button("์š”์•ฝ")
123
 
 
125
  output = gr.Textbox(label="Space ์ •๋ณด", lines=10)
126
  app_py_content = gr.Code(language="python", label="app.py ๋‚ด์šฉ")
127
 
128
+ def on_select(choice):
 
129
  try:
130
+ print(f"Selected: {choice}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
131
+ selected_id = space_choices[choice]
132
  selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
133
  if selected_space:
 
134
  app_content = get_app_py_content(selected_id)
135
  print(f"Selected space: {selected_space['name']} (ID: {selected_id})") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
136
  return f"์„ ํƒ๋œ Space: {selected_space['name']} (ID: {selected_id})\nURL: {selected_space['url']}", app_content
 
142
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
143
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", ""
144
 
145
+ def on_summarize(choice):
 
146
  try:
147
+ if choice:
148
+ selected_id = space_choices[choice]
149
+ selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
150
+ if selected_space:
151
+ summary = summarize_space(selected_space)
152
+ print(f"Summarizing space: {selected_space['name']}") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
153
+ return summary
154
  print("No space selected for summarization") # ๋””๋ฒ„๊น… ์ถœ๋ ฅ
155
  return "์„ ํƒ๋œ space๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฆฌ์ŠคํŠธ์—์„œ space๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
156
  except Exception as e:
 
158
  print(traceback.format_exc()) # ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ์ •๋ณด ์ถœ๋ ฅ
159
  return f"์š”์•ฝ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
160
 
161
+ space_radio.change(on_select, space_radio, [output, app_py_content])
162
+ summarize_btn.click(on_summarize, space_radio, output)
163
 
164
  return demo
165