Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ from gradio_client import Client
|
|
11 |
import time
|
12 |
import threading
|
13 |
import json
|
|
|
14 |
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
@@ -61,77 +62,15 @@ def get_space_structure(space_id: str) -> Dict:
|
|
61 |
return {"error": f"API request error: {str(e)}"}
|
62 |
|
63 |
def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
formatted += format_tree_structure(child, indent + " ")
|
68 |
return formatted
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
def analyze_space(url: str, progress=gr.Progress()):
|
77 |
-
try:
|
78 |
-
space_id = url.split('spaces/')[-1]
|
79 |
-
|
80 |
-
progress(0.1, desc="νμΌ κ΅¬μ‘° λΆμ μ€...")
|
81 |
-
tree_structure = get_space_structure(space_id)
|
82 |
-
tree_view = format_tree_structure(tree_structure)
|
83 |
-
|
84 |
-
progress(0.3, desc="app.py λ΄μ© κ°μ Έμ€λ μ€...")
|
85 |
-
app_content = get_file_content(space_id, "app.py")
|
86 |
-
|
87 |
-
progress(0.4, desc="μ½λ μμ½ μ€...")
|
88 |
-
summary = summarize_code(app_content)
|
89 |
-
|
90 |
-
progress(0.6, desc="μ½λ λΆμ μ€...")
|
91 |
-
analysis = analyze_code(app_content)
|
92 |
-
|
93 |
-
progress(0.8, desc="μ¬μ©λ² μ€λͺ
μμ± μ€...")
|
94 |
-
usage = explain_usage(app_content)
|
95 |
-
|
96 |
-
progress(1.0, desc="μλ£")
|
97 |
-
return summary, analysis, usage, app_content, tree_view, tree_structure, space_id
|
98 |
-
except Exception as e:
|
99 |
-
print(f"Error in analyze_space: {str(e)}")
|
100 |
-
print(traceback.format_exc())
|
101 |
-
return f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", "", "", "", None, ""
|
102 |
-
|
103 |
-
def respond(
|
104 |
-
message: str,
|
105 |
-
history: List[Tuple[str, str]],
|
106 |
-
system_message: str = "",
|
107 |
-
max_tokens: int = 4000,
|
108 |
-
temperature: float = 0.7,
|
109 |
-
top_p: float = 0.9,
|
110 |
-
):
|
111 |
-
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§ μμ€μ½λλ₯Ό κΈ°λ°μΌλ‘ "μλΉμ€ μ¬μ© μ€λͺ
λ° μλ΄, qnaλ₯Ό νλ μν μ΄λ€". μμ£Ό μΉμ νκ³ μμΈνκ² 4000ν ν° μ΄μ μμ±νλΌ. λλ μ½λλ₯Ό κΈ°λ°μΌλ‘ μ¬μ© μ€λͺ
λ° μ§μ μλ΅μ μ§ννλ©°, μ΄μ©μμκ² λμμ μ£Όμ΄μΌ νλ€. μ΄μ©μκ° κΆκΈν΄ ν λ§ ν λ΄μ©μ μΉμ νκ² μλ €μ£Όλλ‘ νλΌ. μ½λ μ 체 λ΄μ©μ λν΄μλ 보μμ μ μ§νκ³ , ν€ κ° λ° μλν¬μΈνΈμ ꡬ체μ μΈ λͺ¨λΈμ 곡κ°νμ§ λ§λΌ."""
|
112 |
-
|
113 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
114 |
-
for user, assistant in history:
|
115 |
-
messages.append({"role": "user", "content": user})
|
116 |
-
messages.append({"role": "assistant", "content": assistant})
|
117 |
-
messages.append({"role": "user", "content": message})
|
118 |
-
|
119 |
-
response = ""
|
120 |
-
for message in hf_client.chat_completion(
|
121 |
-
messages,
|
122 |
-
max_tokens=max_tokens,
|
123 |
-
stream=True,
|
124 |
-
temperature=temperature,
|
125 |
-
top_p=top_p,
|
126 |
-
):
|
127 |
-
token = message.choices[0].delta.get('content', None)
|
128 |
-
if token:
|
129 |
-
response += token.strip("")
|
130 |
-
yield response
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
def summarize_code(app_content: str):
|
136 |
system_message = "λΉμ μ Python μ½λλ₯Ό λΆμνκ³ μμ½νλ AI μ‘°μμ
λλ€. μ£Όμ΄μ§ μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ κ°κ²°νκ² μμ½ν΄μ£ΌμΈμ."
|
137 |
user_message = f"λ€μ Python μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ μμ½ν΄μ£ΌμΈμ:\n\n{app_content}"
|
@@ -187,8 +126,14 @@ def analyze_space(url: str, progress=gr.Progress()):
|
|
187 |
try:
|
188 |
space_id = url.split('spaces/')[-1]
|
189 |
|
|
|
|
|
|
|
|
|
190 |
progress(0.1, desc="νμΌ κ΅¬μ‘° λΆμ μ€...")
|
191 |
tree_structure = get_space_structure(space_id)
|
|
|
|
|
192 |
tree_view = format_tree_structure(tree_structure)
|
193 |
|
194 |
progress(0.3, desc="app.py λ΄μ© κ°μ Έμ€λ μ€...")
|
@@ -201,8 +146,6 @@ def analyze_space(url: str, progress=gr.Progress()):
|
|
201 |
print(traceback.format_exc())
|
202 |
return f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", None, ""
|
203 |
|
204 |
-
|
205 |
-
|
206 |
def create_ui():
|
207 |
try:
|
208 |
css = """
|
@@ -244,7 +187,7 @@ def create_ui():
|
|
244 |
"""
|
245 |
|
246 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
247 |
-
gr.Markdown("#
|
248 |
|
249 |
with gr.Tabs(elem_classes="main-tabs") as tabs:
|
250 |
with gr.TabItem("λΆμ"):
|
@@ -289,7 +232,9 @@ def create_ui():
|
|
289 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
290 |
|
291 |
examples = [
|
292 |
-
|
|
|
|
|
293 |
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ
νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
|
294 |
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
|
295 |
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
@@ -407,4 +352,4 @@ if __name__ == "__main__":
|
|
407 |
)
|
408 |
except Exception as e:
|
409 |
print(f"Error in main: {str(e)}")
|
410 |
-
print(traceback.format_exc())
|
|
|
11 |
import time
|
12 |
import threading
|
13 |
import json
|
14 |
+
import re
|
15 |
|
16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
|
|
62 |
return {"error": f"API request error: {str(e)}"}
|
63 |
|
64 |
def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
|
65 |
+
if "error" in tree_data:
|
66 |
+
return tree_data["error"]
|
67 |
+
|
68 |
+
formatted = f"{indent}{'π' if tree_data.get('type') == 'directory' else 'π'} {tree_data.get('name', 'Unknown')}\n"
|
69 |
+
if tree_data.get("type") == "directory":
|
70 |
+
for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
|
71 |
formatted += format_tree_structure(child, indent + " ")
|
72 |
return formatted
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
def summarize_code(app_content: str):
|
75 |
system_message = "λΉμ μ Python μ½λλ₯Ό λΆμνκ³ μμ½νλ AI μ‘°μμ
λλ€. μ£Όμ΄μ§ μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ κ°κ²°νκ² μμ½ν΄μ£ΌμΈμ."
|
76 |
user_message = f"λ€μ Python μ½λλ₯Ό 3μ€ μ΄λ΄λ‘ μμ½ν΄μ£ΌμΈμ:\n\n{app_content}"
|
|
|
126 |
try:
|
127 |
space_id = url.split('spaces/')[-1]
|
128 |
|
129 |
+
# Space ID μ ν¨μ± κ²μ¬
|
130 |
+
if not re.match(r'^[\w.-]{1,96}$', space_id) or space_id.startswith(('-', '.')) or space_id.endswith(('-', '.')):
|
131 |
+
raise ValueError(f"Invalid Space ID: {space_id}")
|
132 |
+
|
133 |
progress(0.1, desc="νμΌ κ΅¬μ‘° λΆμ μ€...")
|
134 |
tree_structure = get_space_structure(space_id)
|
135 |
+
if "error" in tree_structure:
|
136 |
+
raise ValueError(tree_structure["error"])
|
137 |
tree_view = format_tree_structure(tree_structure)
|
138 |
|
139 |
progress(0.3, desc="app.py λ΄μ© κ°μ Έμ€λ μ€...")
|
|
|
146 |
print(traceback.format_exc())
|
147 |
return f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", None, ""
|
148 |
|
|
|
|
|
149 |
def create_ui():
|
150 |
try:
|
151 |
css = """
|
|
|
187 |
"""
|
188 |
|
189 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
190 |
+
gr.Markdown("# HuggingFace Space Analyzer")
|
191 |
|
192 |
with gr.Tabs(elem_classes="main-tabs") as tabs:
|
193 |
with gr.TabItem("λΆμ"):
|
|
|
232 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
233 |
|
234 |
examples = [
|
235 |
+
["ν¨μ
μ½λ μ€ν"],
|
236 |
+
["UHD μ΄λ―Έμ§ μ½λ μ€ν"],
|
237 |
+
["MixGEN μ½λ μ€ν"],
|
238 |
["μμΈν μ¬μ© λ°©λ²μ λ§μΉ νλ©΄μ 보면μ μ€λͺ
νλ―μ΄ 4000 ν ν° μ΄μ μμΈν μ€λͺ
νλΌ"],
|
239 |
["FAQ 20건μ μμΈνκ² μμ±νλΌ. 4000ν ν° μ΄μ μ¬μ©νλΌ."],
|
240 |
["μ¬μ© λ°©λ²κ³Ό μ°¨λ³μ , νΉμ§, κ°μ μ μ€μ¬μΌλ‘ 4000 ν ν° μ΄μ μ νλΈ μμ μ€ν¬λ¦½νΈ ννλ‘ μμ±νλΌ"],
|
|
|
352 |
)
|
353 |
except Exception as e:
|
354 |
print(f"Error in main: {str(e)}")
|
355 |
+
print(traceback.format_exc())
|