ZeroGPU-Leader / app.py
seawolf2357's picture
Update app.py
ec0e5a1 verified
raw
history blame
8.76 kB
from flask import Flask, render_template, request, redirect, url_for, jsonify, session
import requests
import os
import json
from datetime import timedelta
import logging
# ๋กœ๊น… ์„ค์ •
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.secret_key = os.urandom(24) # ์„ธ์…˜ ์•”ํ˜ธํ™”๋ฅผ ์œ„ํ•œ ๋น„๋ฐ€ ํ‚ค
app.permanent_session_lifetime = timedelta(days=7) # ์„ธ์…˜ ์œ ์ง€ ๊ธฐ๊ฐ„ ์„ค์ •
# ํ—ˆ๊น…ํŽ˜์ด์Šค URL ๋ชฉ๋ก (๊ฐ„๋žตํ™”)
HUGGINGFACE_URLS = [
"https://huggingface.co/spaces/ginipick/Tech_Hangman_Game",
"https://huggingface.co/spaces/openfree/deepseek_r1_API",
"https://huggingface.co/spaces/ginipick/open_Deep-Research"
]
# ํ—ˆ๊น…ํŽ˜์ด์Šค ์ธ์ฆ ํ™•์ธ (๋””๋ฒ„๊น… ๊ฐœ์„ )
def validate_token(token):
logger.debug(f"ํ† ํฐ ๊ฒ€์ฆ ์‹œ๋„ (ํ† ํฐ ์ผ๋ถ€: {token[:4]}...)")
headers = {"Authorization": f"Bearer {token}"}
# ์—ฌ๋Ÿฌ ์—”๋“œํฌ์ธํŠธ ์‹œ๋„
api_endpoints = [
"https://huggingface.co/api/whoami",
"https://huggingface.co/api/whoami-v2"
]
for endpoint in api_endpoints:
try:
logger.debug(f"API ์—”๋“œํฌ์ธํŠธ ์‹œ๋„: {endpoint}")
response = requests.get(endpoint, headers=headers)
# ์‘๋‹ต ๋กœ๊น…
logger.debug(f"API ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}")
logger.debug(f"API ์‘๋‹ต ํ—ค๋”: {response.headers}")
if response.ok:
logger.debug("ํ† ํฐ ๊ฒ€์ฆ ์„ฑ๊ณต!")
return True, response.json()
else:
logger.debug(f"ํ† ํฐ ๊ฒ€์ฆ ์‹คํŒจ - ์‘๋‹ต: {response.text}")
except Exception as e:
logger.error(f"API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
# ๋ชจ๋“  API ํ˜ธ์ถœ ์‹คํŒจ
return False, None
# ํ† ํฐ ๊ฒ€์ฆ ํ…Œ์ŠคํŠธ API
@app.route('/api/test-token', methods=['POST'])
def test_token():
token = request.form.get('token', '')
if not token:
return jsonify({'success': False, 'message': 'ํ† ํฐ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.'})
# ์ƒ์„ธ ๋””๋ฒ„๊น… ์ •๋ณด ์ˆ˜์ง‘
debug_info = {}
# ํ† ํฐ ํ˜•์‹ ํ™•์ธ
is_valid_format = token.startswith('hf_')
debug_info['token_format'] = 'hf_ ์ ‘๋‘์‚ฌ ์žˆ์Œ' if is_valid_format else '์ ‘๋‘์‚ฌ ์—†์Œ'
# ์ง์ ‘ API ํ˜ธ์ถœ ํ…Œ์ŠคํŠธ
test_endpoints = [
"https://huggingface.co/api/whoami",
"https://huggingface.co/api/whoami-v2",
"https://huggingface.co/api/models/meta/llama"
]
endpoint_results = {}
for endpoint in test_endpoints:
try:
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(endpoint, headers=headers, timeout=5)
endpoint_results[endpoint] = {
'status_code': response.status_code,
'success': response.ok,
'content_type': response.headers.get('Content-Type', 'unknown'),
'response_sample': str(response.text)[:100] + '...' if response.text else 'Empty response'
}
except Exception as e:
endpoint_results[endpoint] = {'error': str(e)}
debug_info['endpoint_tests'] = endpoint_results
# ์ตœ์ข… ํŒ๋‹จ
is_valid = any(result.get('success', False) for result in endpoint_results.values())
return jsonify({
'success': is_valid,
'debug_info': debug_info,
'message': 'ํ† ํฐ์ด ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.' if is_valid else '์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.'
})
# ๊ธฐ๋ณธ ํ™ˆํŽ˜์ด์ง€ (๋””๋ฒ„๊น… ํ™”๋ฉด ํฌํ•จ)
@app.route('/')
def home():
return render_template('debug_index.html')
if __name__ == '__main__':
# templates ํด๋” ์ƒ์„ฑ
os.makedirs('templates', exist_ok=True)
# ๋””๋ฒ„๊น…์šฉ HTML ์ƒ์„ฑ
with open('templates/debug_index.html', 'w', encoding='utf-8') as f:
f.write('''
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํ—ˆ๊น…ํŽ˜์ด์Šค ํ† ํฐ ๋””๋ฒ„๊ฑฐ</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
color: #333;
}
h1, h2 {
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
}
.card {
border: 1px solid #ddd;
border-radius: 5px;
padding: 20px;
margin-bottom: 20px;
background-color: #f9f9f9;
}
input[type="password"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
padding: 15px;
border-radius: 5px;
display: none;
}
.success {
background-color: #dff0d8;
color: #3c763d;
}
.error {
background-color: #f2dede;
color: #a94442;
}
pre {
background-color: #f5f5f5;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
white-space: pre-wrap;
}
.loading {
display: none;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>ํ—ˆ๊น…ํŽ˜์ด์Šค ํ† ํฐ ๋””๋ฒ„๊ฑฐ</h1>
<div class="card">
<h2>ํ† ํฐ ํ…Œ์ŠคํŠธ</h2>
<p>ํ—ˆ๊น…ํŽ˜์ด์Šค API ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ์œ ํšจ์„ฑ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.</p>
<input type="password" id="tokenInput" placeholder="ํ—ˆ๊น…ํŽ˜์ด์Šค API ํ† ํฐ ์ž…๋ ฅ (hf_๋กœ ์‹œ์ž‘)" />
<button id="testButton">ํ† ํฐ ํ…Œ์ŠคํŠธ</button>
<div id="loading" class="loading">ํ…Œ์ŠคํŠธ ์ค‘...</div>
<div id="result" class="result">
<h3>ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ</h3>
<p id="resultMessage"></p>
<div id="detailsContainer">
<h4>์ƒ์„ธ ์ •๋ณด</h4>
<pre id="resultDetails"></pre>
</div>
</div>
</div>
<div class="card">
<h2>๋””๋ฒ„๊น… ํŒ</h2>
<ul>
<li>ํ† ํฐ์€ <code>hf_</code>๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.</li>
<li>ํ† ํฐ์€ ๊ณต๋ฐฑ ์—†์ด ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.</li>
<li>ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.</li>
<li>ํ† ํฐ์— ์ ์ ˆํ•œ ๊ถŒํ•œ(์ตœ์†Œ READ)์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.</li>
<li>๋„คํŠธ์›Œํฌ ์ œํ•œ์ด ์žˆ๋Š” ํ™˜๊ฒฝ์ธ์ง€ ํ™•์ธํ•˜์„ธ์š”.</li>
</ul>
</div>
</div>
<script>
// ์š”์†Œ ์ฐธ์กฐ
const tokenInput = document.getElementById('tokenInput');
const testButton = document.getElementById('testButton');
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const resultMessage = document.getElementById('resultMessage');
const resultDetails = document.getElementById('resultDetails');
// ํ† ํฐ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜
async function testToken() {
const token = tokenInput.value.trim();
if (!token) {
alert('ํ† ํฐ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}
// UI ์—…๋ฐ์ดํŠธ
loading.style.display = 'block';
result.style.display = 'none';
try {
// FormData ์ƒ์„ฑ
const formData = new FormData();
formData.append('token', token);
// API ํ˜ธ์ถœ
const response = await fetch('/api/test-token', {
method: 'POST',
body: formData
});
const data = await response.json();
// ๊ฒฐ๊ณผ ํ‘œ์‹œ
result.className = `result ${data.success ? 'success' : 'error'}`;
resultMessage.textContent = data.message;
resultDetails.textContent = JSON.stringify(data.debug_info, null, 2);
result.style.display = 'block';
} catch (error) {
result.className = 'result error';
resultMessage.textContent = `์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${error.message}`;
resultDetails.textContent = error.stack || '์ƒ์„ธ ์ •๋ณด ์—†์Œ';
result.style.display = 'block';
} finally {
loading.style.display = 'none';
}
}
// ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ
testButton.addEventListener('click', testToken);
tokenInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
testToken();
}
});
</script>
</body>
</html>
''')
# ํ—ˆ๊น…ํŽ˜์ด์Šค ์ŠคํŽ˜์ด์Šค์—์„œ๋Š” 7860 ํฌํŠธ ์‚ฌ์šฉ
app.run(host='0.0.0.0', port=7860, debug=True)