Spaces:
Running
Running
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 | |
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 'μ ν¨νμ§ μμ ν ν°μ λλ€.' | |
}) | |
# κΈ°λ³Έ ννμ΄μ§ (λλ²κΉ νλ©΄ ν¬ν¨) | |
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) |