Spaces:
Running
Running
import os | |
import gradio as gr | |
from gradio import ChatMessage | |
from typing import Iterator, List, Dict, Tuple, Any | |
import google.generativeai as genai | |
from huggingface_hub import HfApi | |
import requests | |
import re | |
import traceback | |
import time | |
import threading | |
import json | |
# HuggingFace κ΄λ ¨ API ν€ (μ€νμ΄μ€ λΆμ μ©) | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
hf_api = HfApi(token=HF_TOKEN) | |
# Gemini 2.0 Flash Thinking λͺ¨λΈ κ΄λ ¨ API ν€ λ° ν΄λΌμ΄μΈνΈ (LLM μ©) | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
genai.configure(api_key=GEMINI_API_KEY) | |
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21") | |
# -------------------------------------------------- | |
# νμΌ λ° μ€νμ΄μ€ λΆμ κ΄λ ¨ ν¨μλ€ (κΈ°μ‘΄ μ½λ μ μ§) | |
# -------------------------------------------------- | |
def get_headers(): | |
if not HF_TOKEN: | |
raise ValueError("Hugging Face token not found in environment variables") | |
return {"Authorization": f"Bearer {HF_TOKEN}"} | |
def get_file_content(space_id: str, file_path: str) -> str: | |
file_url = f"https://huggingface.co/spaces/{space_id}/raw/main/{file_path}" | |
try: | |
response = requests.get(file_url, headers=get_headers()) | |
if response.status_code == 200: | |
return response.text | |
else: | |
return f"File not found or inaccessible: {file_path}" | |
except requests.RequestException: | |
return f"Error fetching content for file: {file_path}" | |
def get_space_structure(space_id: str) -> Dict: | |
try: | |
files = hf_api.list_repo_files(repo_id=space_id, repo_type="space") | |
tree = {"type": "directory", "path": "", "name": space_id, "children": []} | |
for file in files: | |
path_parts = file.split('/') | |
current = tree | |
for i, part in enumerate(path_parts): | |
if i == len(path_parts) - 1: # νμΌ | |
current["children"].append({"type": "file", "path": file, "name": part}) | |
else: | |
found = False | |
for child in current["children"]: | |
if child["type"] == "directory" and child["name"] == part: | |
current = child | |
found = True | |
break | |
if not found: | |
new_dir = {"type": "directory", "path": '/'.join(path_parts[:i+1]), "name": part, "children": []} | |
current["children"].append(new_dir) | |
current = new_dir | |
return tree | |
except Exception as e: | |
print(f"Error in get_space_structure: {str(e)}") | |
return {"error": f"API request error: {str(e)}"} | |
def format_tree_structure(tree_data: Dict, indent: str = "") -> str: | |
if "error" in tree_data: | |
return tree_data["error"] | |
formatted = f"{indent}{'π' if tree_data.get('type') == 'directory' else 'π'} {tree_data.get('name', 'Unknown')}\n" | |
if tree_data.get("type") == "directory": | |
for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))): | |
formatted += format_tree_structure(child, indent + " ") | |
return formatted | |
def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int: | |
num_lines = len(code_content.split('\n')) | |
return min(max(num_lines, min_lines), max_lines) | |
def analyze_space(url: str, progress=gr.Progress()): | |
try: | |
space_id = url.split('spaces/')[-1] | |
if not re.match(r'^[\w.-]+/[\w.-]+$', space_id): | |
raise ValueError(f"Invalid Space ID format: {space_id}") | |
progress(0.1, desc="νμΌ κ΅¬μ‘° λΆμ μ€...") | |
tree_structure = get_space_structure(space_id) | |
if "error" in tree_structure: | |
raise ValueError(tree_structure["error"]) | |
tree_view = format_tree_structure(tree_structure) | |
progress(0.3, desc="app.py λ΄μ© κ°μ Έμ€λ μ€...") | |
app_content = get_file_content(space_id, "app.py") | |
progress(0.5, desc="μ½λ μμ½ μ€...") | |
summary = summarize_code(app_content) | |
progress(0.7, desc="μ½λ λΆμ μ€...") | |
analysis = analyze_code(app_content) | |
progress(0.9, desc="μ¬μ©λ² μ€λͺ μμ± μ€...") | |
usage = explain_usage(app_content) | |
app_py_lines = adjust_lines_for_code(app_content) | |
progress(1.0, desc="μλ£") | |
return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, app_py_lines | |
except Exception as e: | |
print(f"Error in analyze_space: {str(e)}") | |
print(traceback.format_exc()) | |
return f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", None, "", "", "", "", 10 | |
# -------------------------------------------------- | |
# Gemini 2.0 Flash Thinking λͺ¨λΈ κ΄λ ¨ ν¬νΌ ν¨μλ€ | |
# -------------------------------------------------- | |
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]: | |
formatted_history = [] | |
for message in messages: | |
# thinking λ©μμ§(λ©νλ°μ΄ν° μλ λ©μμ§)λ 건λλλλ€. | |
if not (hasattr(message, "metadata") and message.metadata): | |
formatted_history.append({ | |
"role": "user" if message.role == "user" else "assistant", | |
"parts": [message.content or ""] | |
}) | |
return formatted_history | |
def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str: | |
initial_messages = [ | |
ChatMessage(role="system", content=system_message), | |
ChatMessage(role="user", content=user_message) | |
] | |
chat_history = format_chat_history(initial_messages) | |
chat = model.start_chat(history=chat_history) | |
final_response = "" | |
try: | |
for chunk in chat.send_message(user_message, stream=True): | |
parts = chunk.candidates[0].content.parts | |
if len(parts) == 2: | |
final_response += parts[1].text | |
else: | |
final_response += parts[0].text | |
return final_response.strip() | |
except Exception as e: | |
return f"LLM νΈμΆ μ€ μ€λ₯ λ°μ: {str(e)}" | |
def summarize_code(app_content: str) -> str: | |
system_message = "λΉμ μ Python μ½λλ₯Ό λΆμνκ³ μμ½νλ AI μ‘°μμ λλ€. μ£Όμ΄μ§ μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ κ°κ²°νκ² μμ½ν΄μ£ΌμΈμ." | |
user_message = f"λ€μ Python μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ μμ½ν΄μ£ΌμΈμ:\n\n{app_content}" | |
try: | |
return gemini_chat_completion(system_message, user_message, max_tokens=200, temperature=0.7) | |
except Exception as e: | |
return f"μμ½ μμ± μ€ μ€λ₯ λ°μ: {str(e)}" | |
def analyze_code(app_content: str) -> str: | |
system_message = ( | |
"λΉμ μ Python μ½λλ₯Ό λΆμνλ AI μ‘°μμ λλ€. μ£Όμ΄μ§ μ½λλ₯Ό λΆμνμ¬ λ€μ νλͺ©μ λν΄ μ€λͺ ν΄μ£ΌμΈμ:\n" | |
"A. λ°°κ²½ λ° νμμ±\n" | |
"B. κΈ°λ₯μ ν¨μ©μ± λ° κ°μΉ\n" | |
"C. νΉμ₯μ \n" | |
"D. μ μ© λμ λ° νκ²\n" | |
"E. κΈ°λν¨κ³Ό\n" | |
"κΈ°μ‘΄ λ° μ μ¬ νλ‘μ νΈμ λΉκ΅νμ¬ λΆμν΄μ£ΌμΈμ. Markdown νμμΌλ‘ μΆλ ₯νμΈμ." | |
) | |
user_message = f"λ€μ Python μ½λλ₯Ό λΆμν΄μ£ΌμΈμ:\n\n{app_content}" | |
try: | |
return gemini_chat_completion(system_message, user_message, max_tokens=1000, temperature=0.7) | |
except Exception as e: | |
return f"λΆμ μμ± μ€ μ€λ₯ λ°μ: {str(e)}" | |
def explain_usage(app_content: str) -> str: | |
system_message = "λΉμ μ Python μ½λλ₯Ό λΆμνμ¬ μ¬μ©λ²μ μ€λͺ νλ AI μ‘°μμ λλ€. μ£Όμ΄μ§ μ½λλ₯Ό λ°νμΌλ‘ λ§μΉ νλ©΄μ 보λ κ²μ²λΌ μ¬μ©λ²μ μμΈν μ€λͺ ν΄μ£ΌμΈμ. Markdown νμμΌλ‘ μΆλ ₯νμΈμ." | |
user_message = f"λ€μ Python μ½λμ μ¬μ©λ²μ μ€λͺ ν΄μ£ΌμΈμ:\n\n{app_content}" | |
try: | |
return gemini_chat_completion(system_message, user_message, max_tokens=800, temperature=0.7) | |
except Exception as e: | |
return f"μ¬μ©λ² μ€λͺ μμ± μ€ μ€λ₯ λ°μ: {str(e)}" | |
def convert_chat_history(messages: List[Any]) -> List[Tuple[str, str]]: | |
""" | |
λ©μμ§ λͺ©λ‘μ κ° νλͺ©μ΄ ChatMessage κ°μ²΄λΌλ©΄ (user, assistant) ννλ‘, | |
μ΄λ―Έ ννμΈ κ²½μ° κ·Έλλ‘ λ°νν©λλ€. | |
""" | |
conv = [] | |
i = 0 | |
while i < len(messages): | |
# λ§μ½ μ΄λ―Έ ννμ΄λ©΄ κ·Έλλ‘ μ¬μ© | |
if isinstance(messages[i], tuple): | |
conv.append(messages[i]) | |
i += 1 | |
# λ§μ½ ChatMessage κ°μ²΄λΌλ©΄ | |
elif hasattr(messages[i], "role"): | |
if messages[i].role == "user": | |
user_text = messages[i].content | |
bot_text = "" | |
if i + 1 < len(messages) and hasattr(messages[i+1], "role") and messages[i+1].role == "assistant": | |
bot_text = messages[i+1].content | |
i += 2 | |
else: | |
i += 1 | |
conv.append((user_text, bot_text)) | |
else: | |
conv.append(("", messages[i].content)) | |
i += 1 | |
else: | |
i += 1 | |
return conv | |
def convert_to_chatmessage(history: List[Tuple[str, str]]) -> List[ChatMessage]: | |
""" | |
νν λͺ©λ‘μ ChatMessage κ°μ²΄ λͺ©λ‘μΌλ‘ λ³νν©λλ€. | |
""" | |
new_history = [] | |
for tup in history: | |
if tup[0]: | |
new_history.append(ChatMessage(role="user", content=tup[0])) | |
if tup[1]: | |
new_history.append(ChatMessage(role="assistant", content=tup[1])) | |
return new_history | |
def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]: | |
if not user_message.strip(): | |
messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed.")) | |
yield messages | |
return | |
try: | |
print(f"\n=== New Request (Text) ===") | |
print(f"User message: {user_message}") | |
chat_history = format_chat_history(messages) | |
chat = model.start_chat(history=chat_history) | |
response = chat.send_message(user_message, stream=True) | |
thought_buffer = "" | |
response_buffer = "" | |
thinking_complete = False | |
messages.append( | |
ChatMessage( | |
role="assistant", | |
content="", | |
metadata={"title": "βοΈ Thinking: *The thoughts produced by the model are experimental"} | |
) | |
) | |
for chunk in response: | |
parts = chunk.candidates[0].content.parts | |
current_chunk = parts[0].text | |
if len(parts) == 2 and not thinking_complete: | |
thought_buffer += current_chunk | |
print(f"\n=== Complete Thought ===\n{thought_buffer}") | |
messages[-1] = ChatMessage( | |
role="assistant", | |
content=thought_buffer, | |
metadata={"title": "βοΈ Thinking: *The thoughts produced by the model are experimental"} | |
) | |
yield messages | |
response_buffer = parts[1].text | |
print(f"\n=== Starting Response ===\n{response_buffer}") | |
messages.append( | |
ChatMessage( | |
role="assistant", | |
content=response_buffer | |
) | |
) | |
thinking_complete = True | |
elif thinking_complete: | |
response_buffer += current_chunk | |
print(f"\n=== Response Chunk ===\n{current_chunk}") | |
messages[-1] = ChatMessage( | |
role="assistant", | |
content=response_buffer | |
) | |
else: | |
thought_buffer += current_chunk | |
print(f"\n=== Thinking Chunk ===\n{current_chunk}") | |
messages[-1] = ChatMessage( | |
role="assistant", | |
content=thought_buffer, | |
metadata={"title": "βοΈ Thinking: *The thoughts produced by the model are experimental"} | |
) | |
yield messages | |
print(f"\n=== Final Response ===\n{response_buffer}") | |
except Exception as e: | |
print(f"\n=== Error ===\n{str(e)}") | |
messages.append( | |
ChatMessage( | |
role="assistant", | |
content=f"I apologize, but I encountered an error: {str(e)}" | |
) | |
) | |
yield messages | |
def respond(message: str, history: List[ChatMessage]) -> Iterator[List[Tuple[str, str]]]: | |
""" | |
stream_gemini_response()λ₯Ό νΈμΆν ν, μΆλ ₯ κ²°κ³Όλ₯Ό νν λͺ©λ‘μΌλ‘ λ³ννμ¬ λ°νν©λλ€. | |
""" | |
for updated_messages in stream_gemini_response(message, history): | |
yield convert_chat_history(updated_messages) | |
def user_message(msg: str, history: List[ChatMessage]) -> Tuple[str, List[ChatMessage]]: | |
history.append(ChatMessage(role="user", content=msg)) | |
return "", history | |
def respond_wrapper(message, chat_history, max_tokens, temperature, top_p): | |
# chat_historyκ° νν λͺ©λ‘μ΄λΌλ©΄ ChatMessage κ°μ²΄λ‘ λ³ν | |
if chat_history and isinstance(chat_history[0], tuple): | |
chat_history = convert_to_chatmessage(chat_history) | |
for updated in stream_gemini_response(message, chat_history): | |
yield "", convert_chat_history(updated) | |
# -------------------------------------------------- | |
# Gradio UI κ΅¬μ± | |
# -------------------------------------------------- | |
def create_ui(): | |
try: | |
css = """ | |
/* μ 체 λ°°κ²½ λ° κΈ°λ³Έ κΈκΌ΄ μ€μ */ | |
body { | |
background-color: #f9f9f9; | |
font-family: 'Helvetica Neue', Arial, sans-serif; | |
color: #333; | |
} | |
/* νλ¨ νΈν° μ¨κΉ */ | |
footer { visibility: hidden; } | |
/* μΆλ ₯ κ·Έλ£Ή μ€νμΌ: λ°μ λ°°κ²½, λΆλλ¬μ΄ ν λ리μ κ·Έλ¦Όμ */ | |
.output-group { | |
border: 1px solid #ccc; | |
border-radius: 8px; | |
padding: 15px; | |
margin-bottom: 20px; | |
background-color: #ffffff; | |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
} | |
/* μ€ν¬λ‘€ μμ μ€μ */ | |
.scroll-lock { | |
overflow-y: auto !important; | |
max-height: 300px !important; | |
} | |
.tree-view-scroll { | |
overflow-y: auto !important; | |
max-height: 400px !important; | |
} | |
.full-height { | |
height: 80vh !important; | |
overflow-y: auto !important; | |
} | |
/* μ½λ λ°μ€ μ€νμΌ: λͺ¨λ Έμ€νμ΄μ€ ν°νΈμ λ°μ λ°°κ²½ */ | |
.code-box { | |
overflow-x: auto !important; | |
overflow-y: auto !important; | |
white-space: pre !important; | |
background-color: #f5f5f5; | |
border-radius: 4px; | |
padding: 10px; | |
font-family: 'Courier New', Courier, monospace; | |
} | |
.code-box > div { min-width: 100% !important; } | |
.code-box > div > textarea { | |
word-break: normal !important; | |
overflow-wrap: normal !important; | |
} | |
/* ν λ΄λΉκ²μ΄μ μ€νμΌ: λ¨μνκ³ κΉλν λμμΈ */ | |
.tab-nav { | |
background-color: #ffffff; | |
border-bottom: 1px solid #ccc; | |
display: flex; | |
} | |
.tab-nav button { | |
background: none; | |
border: none; | |
padding: 10px 20px; | |
margin: 0; | |
cursor: pointer; | |
font-size: 16px; | |
color: #555; | |
transition: color 0.3s, border-bottom 0.3s; | |
} | |
.tab-nav button:hover, | |
.tab-nav button.selected { | |
color: #000; | |
border-bottom: 2px solid #007BFF; | |
} | |
/* μ λ ₯μ°½ λ° ν μ€νΈ μμ μ€νμΌ */ | |
input[type="text"], textarea { | |
color: #333; | |
background-color: #fff; | |
border: 1px solid #ccc; | |
border-radius: 4px; | |
padding: 8px; | |
} | |
""" | |
with gr.Blocks(theme="default", css=css) as demo: | |
gr.Markdown("# MOUSE: Space Research Thinking", elem_classes="header-markdown") | |
with gr.Tabs() as tabs: | |
with gr.TabItem("λΆμ"): | |
with gr.Row(): | |
with gr.Column(scale=6): | |
url_input = gr.Textbox(label="HuggingFace Space URL", placeholder="μ: https://huggingface.co/spaces/username/space_name") | |
analyze_button = gr.Button("λΆμ", variant="primary") | |
with gr.Group(elem_classes="output-group scroll-lock"): | |
summary_output = gr.Markdown(label="μμ½ (3μ€ μ΄λ΄)") | |
with gr.Group(elem_classes="output-group scroll-lock"): | |
analysis_output = gr.Markdown(label="λΆμ") | |
with gr.Group(elem_classes="output-group scroll-lock"): | |
usage_output = gr.Markdown(label="μ¬μ©λ²") | |
with gr.Group(elem_classes="output-group tree-view-scroll"): | |
tree_view_output = gr.Textbox(label="νμΌ κ΅¬μ‘° (Tree View)", lines=30) | |
with gr.Column(scale=4): | |
with gr.Group(elem_classes="output-group full-height"): | |
code_tabs = gr.Tabs() | |
with code_tabs: | |
with gr.TabItem("app.py"): | |
app_py_content = gr.Code( | |
language="python", | |
label="app.py", | |
lines=200, | |
elem_classes="full-height code-box" | |
) | |
with gr.TabItem("requirements.txt"): | |
requirements_content = gr.Textbox( | |
label="requirements.txt", | |
lines=200, | |
elem_classes="full-height code-box" | |
) | |
with gr.TabItem("AI μ½λμ±"): | |
gr.Markdown("## : μμ λ₯Ό μ λ ₯/μ ννκ³ , μ΄μ΄μ 볡μ¬ν app.py μμ€μ½λλ₯Ό λΆμ¬ λ£μΌμΈμ", elem_classes="header-markdown") | |
# μ±ν λ°μ€ λμ΄λ₯Ό 400pxλ‘ μ§μ νμ¬ νλ©΄ λμ΄μ λ§κ² μ€μ. | |
chatbot = gr.Chatbot( | |
label="λν", | |
elem_classes="output-group", | |
height=400 | |
) | |
msg = gr.Textbox(label="λ©μμ§", placeholder="λ©μμ§λ₯Ό μ λ ₯νμΈμ...") | |
# μ¨κ²¨μ§ νλΌλ―Έν° | |
max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens", visible=False) | |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature", visible=False) | |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P", visible=False) | |
examples = [ | |
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ νλΌ"], | |
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."], | |
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"], | |
["λ³Έ μλΉμ€λ₯Ό SEO μ΅μ ννμ¬ λΈλ‘κ·Έ ν¬μ€νΈλ‘ 4000 ν ν° μ΄μ μμ±νλΌ"], | |
["νΉν μΆμμ νμ©ν νμ μ μΈ μ°½μ λ°λͺ λ΄μ©μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."], | |
["νμ μ μ΄κ³ λ Όλ¦¬μ μΈ μ λ¬Έ λ Όλ¬Έμ νμμΌλ‘ 4000 ν ν° μ΄μ μμ±νλΌ."], | |
["κ³μ μ΄μ΄μ λ΅λ³νλΌ"], | |
] | |
gr.Examples(examples, inputs=msg) | |
msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot]) | |
with gr.TabItem("Recommended Best"): | |
gr.Markdown( | |
"Discover the best recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard).", | |
elem_id="recommended-best" | |
) | |
# μν μ μ₯μ© λ³μ | |
space_id_state = gr.State() | |
tree_structure_state = gr.State() | |
app_py_content_lines = gr.State() | |
analyze_button.click( | |
analyze_space, | |
inputs=[url_input], | |
outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state, summary_output, analysis_output, usage_output, app_py_content_lines] | |
).then( | |
lambda space_id: get_file_content(space_id, "requirements.txt"), | |
inputs=[space_id_state], | |
outputs=[requirements_content] | |
) | |
app_py_content.change(lambda lines: gr.update(lines=lines), inputs=[app_py_content_lines], outputs=[app_py_content]) | |
return demo | |
except Exception as e: | |
print(f"Error in create_ui: {str(e)}") | |
print(traceback.format_exc()) | |
raise | |
if __name__ == "__main__": | |
try: | |
print("Starting HuggingFace Space Analyzer...") | |
demo = create_ui() | |
print("UI created successfully.") | |
print("Configuring Gradio queue...") | |
demo.queue() | |
print("Gradio queue configured.") | |
print("Launching Gradio app...") | |
demo.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False, | |
debug=True, | |
show_api=False | |
) | |
print("Gradio app launched successfully.") | |
except Exception as e: | |
print(f"Error in main: {str(e)}") | |
print("Detailed error information:") | |
print(traceback.format_exc()) | |
raise | |