spaces-research-korea / app-backup10.py
ginipick's picture
Update app-backup10.py
38876fc verified
raw
history blame
16.8 kB
import gradio as gr
from huggingface_hub import InferenceClient, HfApi
import os
import requests
from typing import List, Dict, Union, Tuple
import traceback
from PIL import Image
from io import BytesIO
import asyncio
from gradio_client import Client
import time
import threading
import json
import re
HF_TOKEN = os.getenv("HF_TOKEN")
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
hf_api = HfApi(token=HF_TOKEN)
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 summarize_code(app_content: str):
system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ์š”์•ฝํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
]
try:
response = hf_client.chat_completion(messages, max_tokens=200, temperature=0.7)
return response.choices[0].message.content
except Exception as e:
return f"์š”์•ฝ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
def analyze_code(app_content: str):
system_message = """๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:
A. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ
B. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜
C. ํŠน์žฅ์ 
D. ์ ์šฉ ๋Œ€์ƒ ๋ฐ ํƒ€๊ฒŸ
E. ๊ธฐ๋Œ€ํšจ๊ณผ
๊ธฐ์กด ๋ฐ ์œ ์‚ฌ ํ”„๋กœ์ ํŠธ์™€ ๋น„๊ตํ•˜์—ฌ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."""
user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
]
try:
response = hf_client.chat_completion(messages, max_tokens=1000, temperature=0.7)
return response.choices[0].message.content
except Exception as e:
return f"๋ถ„์„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
def explain_usage(app_content: str):
system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
]
try:
response = hf_client.chat_completion(messages, max_tokens=800, temperature=0.7)
return response.choices[0].message.content
except Exception as e:
return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
"""
์ฝ”๋“œ ๋‚ด์šฉ์— ๋”ฐ๋ผ lines ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
Parameters:
- code_content (str): ์ฝ”๋“œ ํ…์ŠคํŠธ ๋‚ด์šฉ
- min_lines (int): ์ตœ์†Œ lines ์ˆ˜
- max_lines (int): ์ตœ๋Œ€ lines ์ˆ˜
Returns:
- int: ์„ค์ •๋œ lines ์ˆ˜
"""
# ์ฝ”๋“œ์˜ ์ค„ ์ˆ˜ ๊ณ„์‚ฐ
num_lines = len(code_content.split('\n'))
# ์ค„ ์ˆ˜๊ฐ€ min_lines๋ณด๋‹ค ์ ๋‹ค๋ฉด min_lines ์‚ฌ์šฉ, max_lines๋ณด๋‹ค ํฌ๋ฉด max_lines ์‚ฌ์šฉ
return min(max(num_lines, min_lines), max_lines)
def analyze_space(url: str, progress=gr.Progress()):
try:
space_id = url.split('spaces/')[-1]
# Space ID ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ˆ˜์ •
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)
# ์ค„ ์ˆ˜ ๊ณ„์‚ฐํ•˜์—ฌ lines ์„ค์ •
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
def respond(
message: str,
history: List[Tuple[str, str]],
system_message: str = "",
max_tokens: int = 1024,
temperature: float = 0.7,
top_p: float = 0.9,
):
system_prefix = """๋‹น์‹ ์€ ํ—ˆ๊น…ํŽ˜์ด์Šค์— ํŠนํ™”๋œ AI ์ฝ”๋”ฉ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ์นœ์ ˆํ•˜๊ณ  ์ƒ์„ธํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”.
Gradio ํŠน์„ฑ์„ ์ •ํ™•ํžˆ ์ธ์‹ํ•˜๊ณ  Requirements.txt ๋ˆ„๋ฝ์—†์ด ์ฝ”๋”ฉ๊ณผ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜์„ธ์š”."""
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
response = ""
for message in hf_client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.get('content', None)
if token:
response += token.strip("")
yield response
def create_ui():
try:
css = """
footer {visibility: hidden;}
.output-group {
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin-bottom: 20px;
}
.scroll-lock {
overflow-y: auto !important;
max-height: calc((100vh - 200px) / 5) !important;
}
.tree-view-scroll {
overflow-y: auto !important;
max-height: calc((100vh - 200px) / 2) !important;
}
.full-height {
height: calc(200em * 1.2) !important;
overflow-y: auto !important;
}
.code-box {
overflow-x: auto !important;
overflow-y: auto !important;
white-space: pre !important;
word-wrap: normal !important;
height: 100% !important;
}
.code-box > div {
min-width: 100% !important;
}
.code-box > div > textarea {
word-break: normal !important;
overflow-wrap: normal !important;
}
.tab-nav {
background-color: #2c3e50;
border-radius: 5px 5px 0 0;
overflow: hidden;
}
.tab-nav button {
color: #ecf0f1 !important;
background-color: #34495e;
border: none;
padding: 10px 20px;
margin: 0;
transition: background-color 0.3s;
font-size: 16px;
font-weight: bold;
}
.tab-nav button:hover {
background-color: #2980b9;
}
.tab-nav button.selected {
color: #2c3e50 !important;
background-color: #ecf0f1;
}
input[type="text"], textarea {
color: #2c3e50 !important;
background-color: #ecf0f1 !important;
}
"""
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
gr.Markdown("# Mouse: HuggingFace")
with gr.Tabs() as tabs:
with gr.TabItem("๋ถ„์„"):
with gr.Row():
with gr.Column(scale=6): # ์™ผ์ชฝ 60%
url_input = gr.Textbox(label="HuggingFace Space URL")
analyze_button = gr.Button("๋ถ„์„")
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): # ์˜ค๋ฅธ์ชฝ 40%
with gr.Group(elem_classes="output-group full-height"):
code_tabs = gr.Tabs()
with code_tabs:
app_py_tab = gr.TabItem("app.py")
with app_py_tab:
app_py_content = gr.Code(
language="python",
label="app.py",
lines=200,
elem_classes="full-height code-box"
)
requirements_tab = gr.TabItem("requirements.txt")
with requirements_tab:
requirements_content = gr.Textbox(
label="requirements.txt",
lines=200,
elem_classes="full-height code-box"
)
with gr.TabItem("AI ์ฝ”๋”ฉ"):
chatbot = gr.Chatbot(label="๋Œ€ํ™”")
msg = gr.Textbox(label="๋ฉ”์‹œ์ง€")
# ์ˆจ๊ฒจ์ง„ ์ƒํƒœ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •
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ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ."],
["๊ณ„์† ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜๋ผ"],
]
gr.Examples(examples, inputs=msg)
def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
bot_message = ""
for response in respond(message, chat_history, max_tokens=max_tokens, temperature=temperature, top_p=top_p):
bot_message = response # ๋งˆ์ง€๋ง‰ ์‘๋‹ต์„ ์ €์žฅ
yield "", chat_history + [(message, bot_message)]
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
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]
)
# lines ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์„ค์ •
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