Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,16 @@
|
|
|
|
|
|
|
|
1 |
import requests
|
|
|
2 |
import concurrent.futures
|
3 |
-
from flask import Flask, render_template_string, jsonify
|
4 |
-
from typing import List, Dict, Union
|
5 |
-
import base64
|
6 |
-
import os
|
7 |
-
|
8 |
-
app = Flask(__name__)
|
9 |
|
10 |
# 환경 변수에서 토큰 가져오기
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
|
|
|
|
|
|
|
13 |
def get_headers():
|
14 |
if not HF_TOKEN:
|
15 |
raise ValueError("Hugging Face token not found in environment variables")
|
@@ -88,112 +89,51 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
88 |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
89 |
return list(executor.map(format_space, spaces))
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
html_template = """
|
100 |
-
<!DOCTYPE html>
|
101 |
-
<html lang="en">
|
102 |
-
<head>
|
103 |
-
<meta charset="UTF-8">
|
104 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
105 |
-
<title>Hugging Face Most Liked Spaces</title>
|
106 |
-
<style>
|
107 |
-
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
|
108 |
-
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
|
109 |
-
.space-item { margin-bottom: 20px; }
|
110 |
-
.space-item img { max-width: 200px; max-height: 200px; }
|
111 |
-
.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; }
|
112 |
-
.tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; }
|
113 |
-
.tab button:hover { background-color: #ddd; }
|
114 |
-
.tab button.active { background-color: #ccc; }
|
115 |
-
.tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
|
116 |
-
pre { white-space: pre-wrap; word-wrap: break-word; }
|
117 |
-
</style>
|
118 |
-
</head>
|
119 |
-
<body>
|
120 |
-
<div class="container">
|
121 |
-
<h1>Hugging Face Most Liked Spaces</h1>
|
122 |
-
<div class="tab">
|
123 |
-
<button class="tablinks active" onclick="openTab(event, 'MostLiked')">Most Liked</button>
|
124 |
-
<button class="tablinks" onclick="openTab(event, 'AppContent')">App.py Content</button>
|
125 |
-
</div>
|
126 |
-
<div id="MostLiked" class="tabcontent" style="display: block;">
|
127 |
-
<ol>
|
128 |
-
{% for space in spaces %}
|
129 |
-
<li class="space-item">
|
130 |
-
<a href="{{ space.url }}" target="_blank">
|
131 |
-
{{ space.name }} by {{ space.author }} (Likes: {{ space.likes }})
|
132 |
-
</a>
|
133 |
-
{% if space.thumbnail %}
|
134 |
-
<br>
|
135 |
-
<img src="data:image/jpeg;base64,{{ space.thumbnail }}" alt="{{ space.name }} thumbnail">
|
136 |
-
{% endif %}
|
137 |
-
</li>
|
138 |
-
{% endfor %}
|
139 |
-
</ol>
|
140 |
-
</div>
|
141 |
-
<div id="AppContent" class="tabcontent">
|
142 |
-
<h2>Select a space to view its app.py content:</h2>
|
143 |
-
<select id="spaceSelector" onchange="showAppContent()">
|
144 |
-
<option value="">Select a space</option>
|
145 |
-
{% for space in spaces %}
|
146 |
-
<option value="{{ space.id }}">{{ space.name }} by {{ space.author }}</option>
|
147 |
-
{% endfor %}
|
148 |
-
</select>
|
149 |
-
<pre id="appContent"></pre>
|
150 |
-
</div>
|
151 |
-
</div>
|
152 |
-
<script>
|
153 |
-
function openTab(evt, tabName) {
|
154 |
-
var i, tabcontent, tablinks;
|
155 |
-
tabcontent = document.getElementsByClassName("tabcontent");
|
156 |
-
for (i = 0; i < tabcontent.length; i++) {
|
157 |
-
tabcontent[i].style.display = "none";
|
158 |
-
}
|
159 |
-
tablinks = document.getElementsByClassName("tablinks");
|
160 |
-
for (i = 0; i < tablinks.length; i++) {
|
161 |
-
tablinks[i].className = tablinks[i].className.replace(" active", "");
|
162 |
-
}
|
163 |
-
document.getElementById(tabName).style.display = "block";
|
164 |
-
evt.currentTarget.className += " active";
|
165 |
-
}
|
166 |
-
|
167 |
-
function showAppContent() {
|
168 |
-
var selector = document.getElementById("spaceSelector");
|
169 |
-
var spaceId = selector.value;
|
170 |
-
if (spaceId) {
|
171 |
-
fetch('/app_content/' + encodeURIComponent(spaceId))
|
172 |
-
.then(response => response.json())
|
173 |
-
.then(data => {
|
174 |
-
document.getElementById("appContent").textContent = data.content;
|
175 |
-
})
|
176 |
-
.catch(error => {
|
177 |
-
document.getElementById("appContent").textContent = "Error fetching app.py content: " + error;
|
178 |
-
});
|
179 |
-
} else {
|
180 |
-
document.getElementById("appContent").textContent = "";
|
181 |
-
}
|
182 |
-
}
|
183 |
-
</script>
|
184 |
-
</body>
|
185 |
-
</html>
|
186 |
-
"""
|
187 |
|
188 |
-
|
|
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
if __name__ == "__main__":
|
199 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
import requests
|
5 |
+
from typing import List, Dict, Union, Tuple
|
6 |
import concurrent.futures
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# 환경 변수에서 토큰 가져오기
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
|
11 |
+
# 추론 API 클라이언트 설정
|
12 |
+
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
13 |
+
|
14 |
def get_headers():
|
15 |
if not HF_TOKEN:
|
16 |
raise ValueError("Hugging Face token not found in environment variables")
|
|
|
89 |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
90 |
return list(executor.map(format_space, spaces))
|
91 |
|
92 |
+
def summarize_space(space: Dict) -> str:
|
93 |
+
system_message = "당신은 Hugging Face Space의 내용을 요약하는 AI 조수입니다. 주어진 정보를 바탕으로 간결하고 명확한 요약을 제공해주세요."
|
94 |
+
user_message = f"다음 Hugging Face Space를 요약해주세요: {space['name']} by {space['author']}. 좋아요 수: {space['likes']}. URL: {space['url']}"
|
95 |
+
|
96 |
+
messages = [
|
97 |
+
{"role": "system", "content": system_message},
|
98 |
+
{"role": "user", "content": user_message}
|
99 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
response = hf_client.chat_completion(messages, max_tokens=150, temperature=0.7)
|
102 |
+
return response.choices[0].message.content
|
103 |
|
104 |
+
def create_ui():
|
105 |
+
spaces_list = get_most_liked_spaces()
|
106 |
+
formatted_spaces = format_spaces(spaces_list)
|
107 |
+
|
108 |
+
with gr.Blocks() as demo:
|
109 |
+
gr.Markdown("# Hugging Face Most Liked Spaces")
|
110 |
+
with gr.Row():
|
111 |
+
with gr.Column(scale=1):
|
112 |
+
space_list = gr.List(
|
113 |
+
[f"{space['name']} by {space['author']} (Likes: {space['likes']})" for space in formatted_spaces],
|
114 |
+
label="Most Liked Spaces"
|
115 |
+
)
|
116 |
+
summarize_btn = gr.Button("요약")
|
117 |
+
|
118 |
+
with gr.Column(scale=2):
|
119 |
+
output = gr.Textbox(label="Space 정보", lines=10)
|
120 |
+
app_py_content = gr.Code(language="python", label="app.py 내용")
|
121 |
+
|
122 |
+
def on_select(evt: gr.SelectData):
|
123 |
+
selected_space = formatted_spaces[evt.index]
|
124 |
+
app_content = get_app_py_content(selected_space['id'])
|
125 |
+
return selected_space['url'], app_content
|
126 |
+
|
127 |
+
def on_summarize(evt: gr.SelectData):
|
128 |
+
selected_space = formatted_spaces[evt.index]
|
129 |
+
summary = summarize_space(selected_space)
|
130 |
+
return summary
|
131 |
+
|
132 |
+
space_list.select(on_select, None, [output, app_py_content])
|
133 |
+
summarize_btn.click(on_summarize, None, output)
|
134 |
+
|
135 |
+
return demo
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
+
demo = create_ui()
|
139 |
+
demo.launch()
|