ginipick commited on
Commit
0841e14
ยท
verified ยท
1 Parent(s): d134242

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -34
app.py CHANGED
@@ -16,8 +16,6 @@ HF_TOKEN = os.getenv("HF_TOKEN")
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
17
  hf_api = HfApi(token=HF_TOKEN)
18
 
19
-
20
-
21
  def get_headers():
22
  if not HF_TOKEN:
23
  raise ValueError("Hugging Face token not found in environment variables")
@@ -39,28 +37,27 @@ def get_trending_spaces(limit: int = 300) -> Union[List[Dict], str]:
39
  print(f"API Response (first 2 items): {json.dumps(data[:2], indent=2)}")
40
  return data
41
  except requests.RequestException as e:
42
- return f"API request error: {str(e)}"
43
- except ValueError as e:
44
- return f"JSON decoding error: {str(e)}"
45
-
46
-
47
- def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
48
- url = "https://huggingface.co/api/spaces"
49
- params = {
50
- "sort": "likes",
51
- "direction": -1,
52
- "limit": limit,
53
- "full": "true"
54
- }
55
-
56
- try:
57
- response = requests.get(url, params=params, headers=get_headers())
58
- response.raise_for_status()
59
- return response.json()
60
- except requests.RequestException as e:
61
- return f"API request error: {str(e)}"
62
- except ValueError as e:
63
- return f"JSON decoding error: {str(e)}"
64
 
65
  def format_space(space: Dict) -> Dict:
66
  print(f"Formatting space: {json.dumps(space, indent=2)}")
@@ -73,7 +70,7 @@ def format_space(space: Dict) -> Dict:
73
  space_author = space_author.get('user', space_author.get('name', 'Unknown'))
74
 
75
  space_likes = space.get('likes', 'N/A')
76
- space_url = f"https://huggingface.co/spaces/{space_id}"
77
 
78
  formatted_space = {
79
  "id": space_id,
@@ -88,7 +85,7 @@ def format_space(space: Dict) -> Dict:
88
  def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
89
  if isinstance(spaces, str):
90
  print(f"Error in spaces data: {spaces}")
91
- return [{"error": spaces}]
92
 
93
  formatted = []
94
  for space in spaces:
@@ -99,7 +96,7 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
99
  print(f"Error formatting space: {str(e)}")
100
  print(f"Problematic space data: {json.dumps(space, indent=2)}")
101
  print(f"Total formatted spaces: {len(formatted)}")
102
- return formatted
103
 
104
  def on_select(space):
105
  try:
@@ -126,7 +123,7 @@ def on_select(space):
126
 
127
  def summarize_space(space: Dict) -> str:
128
  system_message = "๋‹น์‹ ์€ Hugging Face Space์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์š”์•ฝ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”."
129
- user_message = f"๋‹ค์Œ Hugging Face Space๋ฅผ ์š”์•ฝํ•ด์ฃผ์„ธ์š”: {space['name']} by {space['author']}. ์ข‹์•„์š” ์ˆ˜: {space['likes']}. URL: {space['url']}"
130
 
131
  messages = [
132
  {"role": "system", "content": system_message},
@@ -150,7 +147,6 @@ def get_app_py_content(space_id: str) -> str:
150
  except requests.RequestException:
151
  return f"Error fetching app.py content for space: {space_id}"
152
 
153
-
154
  def get_space_structure(space_id: str) -> Dict:
155
  try:
156
  # space_id์—์„œ owner์™€ repo_name์„ ๋ถ„๋ฆฌ
@@ -204,8 +200,6 @@ def format_list_structure(tree_data: Dict) -> List[str]:
204
  formatted.extend(format_list_structure(item))
205
  return formatted
206
 
207
-
208
-
209
  def update_screenshot(url, last_url, force_update=False):
210
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
211
  if url and (url != last_url or force_update):
@@ -290,7 +284,6 @@ def create_ui():
290
  }
291
  """
292
 
293
-
294
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
295
  gr.Markdown("# 300: HuggingFace Trending Spaces")
296
 
@@ -312,6 +305,10 @@ def create_ui():
312
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", elem_id="info-output", lines=20, max_lines=30)
313
  url_state = gr.State("")
314
  last_url_state = gr.State("")
 
 
 
 
315
 
316
  screenshot_output = gr.Image(type="pil", label="Live ํ™”๋ฉด", height=360, width=600)
317
  refresh_button = gr.Button("๐Ÿ”„ ์„œ๋น„์Šค ํ™”๋ฉด", elem_id="refresh-button")
@@ -349,7 +346,6 @@ def create_ui():
349
  outputs=[screenshot_output, last_url_state]
350
  )
351
 
352
-
353
  def open_space_in_browser(url):
354
  if url:
355
  import webbrowser
@@ -406,4 +402,4 @@ if __name__ == "__main__":
406
  demo.launch()
407
  except Exception as e:
408
  print(f"Error in main: {str(e)}")
409
- print(traceback.format_exc())
 
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
17
  hf_api = HfApi(token=HF_TOKEN)
18
 
 
 
19
  def get_headers():
20
  if not HF_TOKEN:
21
  raise ValueError("Hugging Face token not found in environment variables")
 
37
  print(f"API Response (first 2 items): {json.dumps(data[:2], indent=2)}")
38
  return data
39
  except requests.RequestException as e:
40
+ print(f"API request error: {str(e)}")
41
+ return get_fallback_spaces()
42
+
43
+ def get_fallback_spaces() -> List[Dict]:
44
+ return [
45
+ {
46
+ "id": "facebook/llama-2-7b-chat",
47
+ "name": "LLaMA 2 7B Chat",
48
+ "author": "Meta AI",
49
+ "likes": 1000,
50
+ "url": "https://huggingface.co/spaces/facebook/llama-2-7b-chat"
51
+ },
52
+ {
53
+ "id": "openai/whisper-large-v3",
54
+ "name": "Whisper Large V3",
55
+ "author": "OpenAI",
56
+ "likes": 800,
57
+ "url": "https://huggingface.co/spaces/openai/whisper-large-v3"
58
+ },
59
+ # Add more fallback spaces as needed
60
+ ]
 
61
 
62
  def format_space(space: Dict) -> Dict:
63
  print(f"Formatting space: {json.dumps(space, indent=2)}")
 
70
  space_author = space_author.get('user', space_author.get('name', 'Unknown'))
71
 
72
  space_likes = space.get('likes', 'N/A')
73
+ space_url = space.get('url', f"https://huggingface.co/spaces/{space_id}")
74
 
75
  formatted_space = {
76
  "id": space_id,
 
85
  def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
86
  if isinstance(spaces, str):
87
  print(f"Error in spaces data: {spaces}")
88
+ return get_fallback_spaces()
89
 
90
  formatted = []
91
  for space in spaces:
 
96
  print(f"Error formatting space: {str(e)}")
97
  print(f"Problematic space data: {json.dumps(space, indent=2)}")
98
  print(f"Total formatted spaces: {len(formatted)}")
99
+ return formatted if formatted else get_fallback_spaces()
100
 
101
  def on_select(space):
102
  try:
 
123
 
124
  def summarize_space(space: Dict) -> str:
125
  system_message = "๋‹น์‹ ์€ Hugging Face Space์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์š”์•ฝ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”."
126
+ user_message = f"๋‹ค์Œ Hugging Face Space๋ฅผ ์š”์•ฝํ•ด์ฃผ์„ธ์š”: {space.get('name', 'Unknown')} by {space.get('author', 'Unknown')}. ์ข‹์•„์š” ์ˆ˜: {space.get('likes', 'N/A')}. URL: {space.get('url', 'Unknown')}"
127
 
128
  messages = [
129
  {"role": "system", "content": system_message},
 
147
  except requests.RequestException:
148
  return f"Error fetching app.py content for space: {space_id}"
149
 
 
150
  def get_space_structure(space_id: str) -> Dict:
151
  try:
152
  # space_id์—์„œ owner์™€ repo_name์„ ๋ถ„๋ฆฌ
 
200
  formatted.extend(format_list_structure(item))
201
  return formatted
202
 
 
 
203
  def update_screenshot(url, last_url, force_update=False):
204
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
205
  if url and (url != last_url or force_update):
 
284
  }
285
  """
286
 
 
287
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
288
  gr.Markdown("# 300: HuggingFace Trending Spaces")
289
 
 
305
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", elem_id="info-output", lines=20, max_lines=30)
306
  url_state = gr.State("")
307
  last_url_state = gr.State("")
308
+
309
+
310
+
311
+
312
 
313
  screenshot_output = gr.Image(type="pil", label="Live ํ™”๋ฉด", height=360, width=600)
314
  refresh_button = gr.Button("๐Ÿ”„ ์„œ๋น„์Šค ํ™”๋ฉด", elem_id="refresh-button")
 
346
  outputs=[screenshot_output, last_url_state]
347
  )
348
 
 
349
  def open_space_in_browser(url):
350
  if url:
351
  import webbrowser
 
402
  demo.launch()
403
  except Exception as e:
404
  print(f"Error in main: {str(e)}")
405
+ print(traceback.format_exc())