Spaces:
Running
Running
Delete app-backup9.py
Browse files- app-backup9.py +0 -158
app-backup9.py
DELETED
@@ -1,158 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient, HfApi
|
3 |
-
import os
|
4 |
-
import requests
|
5 |
-
from typing import List, Dict, Union
|
6 |
-
import traceback
|
7 |
-
from PIL import Image
|
8 |
-
from io import BytesIO
|
9 |
-
import asyncio
|
10 |
-
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)
|
17 |
-
hf_api = HfApi(token=HF_TOKEN)
|
18 |
-
|
19 |
-
def get_headers():
|
20 |
-
if not HF_TOKEN:
|
21 |
-
raise ValueError("Hugging Face token not found in environment variables")
|
22 |
-
return {"Authorization": f"Bearer {HF_TOKEN}"}
|
23 |
-
|
24 |
-
def get_app_py_content(space_id: str) -> str:
|
25 |
-
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
26 |
-
try:
|
27 |
-
response = requests.get(app_py_url, headers=get_headers())
|
28 |
-
if response.status_code == 200:
|
29 |
-
return response.text
|
30 |
-
else:
|
31 |
-
return f"app.py file not found or inaccessible for space: {space_id}"
|
32 |
-
except requests.RequestException:
|
33 |
-
return f"Error fetching app.py content for space: {space_id}"
|
34 |
-
|
35 |
-
def summarize_code(app_content: str) -> str:
|
36 |
-
system_message = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์์ฝํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ 3์ค ์ด๋ด๋ก ๊ฐ๊ฒฐํ๊ฒ ์์ฝํด์ฃผ์ธ์."
|
37 |
-
user_message = f"๋ค์ Python ์ฝ๋๋ฅผ 3์ค ์ด๋ด๋ก ์์ฝํด์ฃผ์ธ์:\n\n{app_content}"
|
38 |
-
|
39 |
-
messages = [
|
40 |
-
{"role": "system", "content": system_message},
|
41 |
-
{"role": "user", "content": user_message}
|
42 |
-
]
|
43 |
-
|
44 |
-
try:
|
45 |
-
response = hf_client.chat_completion(messages, max_tokens=200, temperature=0.7)
|
46 |
-
return response.choices[0].message.content
|
47 |
-
except Exception as e:
|
48 |
-
return f"์์ฝ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
49 |
-
|
50 |
-
def analyze_code(app_content: str) -> str:
|
51 |
-
system_message = """๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ถ์ํ์ฌ ๋ค์ ํญ๋ชฉ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์:
|
52 |
-
A. ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ
|
53 |
-
B. ๊ธฐ๋ฅ์ ํจ์ฉ์ฑ ๋ฐ ๊ฐ์น
|
54 |
-
C. ํน์ฅ์
|
55 |
-
D. ์ ์ฉ ๋์ ๋ฐ ํ๊ฒ
|
56 |
-
E. ๊ธฐ๋ํจ๊ณผ
|
57 |
-
๊ธฐ์กด ๋ฐ ์ ์ฌ ํ๋ก์ ํธ์ ๋น๊ตํ์ฌ ๋ถ์ํด์ฃผ์ธ์."""
|
58 |
-
user_message = f"๋ค์ Python ์ฝ๋๋ฅผ ๋ถ์ํด์ฃผ์ธ์:\n\n{app_content}"
|
59 |
-
|
60 |
-
messages = [
|
61 |
-
{"role": "system", "content": system_message},
|
62 |
-
{"role": "user", "content": user_message}
|
63 |
-
]
|
64 |
-
|
65 |
-
try:
|
66 |
-
response = hf_client.chat_completion(messages, max_tokens=1000, temperature=0.7)
|
67 |
-
return response.choices[0].message.content
|
68 |
-
except Exception as e:
|
69 |
-
return f"๋ถ์ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
70 |
-
|
71 |
-
def explain_usage(app_content: str) -> str:
|
72 |
-
system_message = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ์ฌ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ๋ง์น ํ๋ฉด์ ๋ณด๋ ๊ฒ์ฒ๋ผ ์ฌ์ฉ๋ฒ์ ์์ธํ ์ค๋ช
ํด์ฃผ์ธ์."
|
73 |
-
user_message = f"๋ค์ Python ์ฝ๋์ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํด์ฃผ์ธ์:\n\n{app_content}"
|
74 |
-
|
75 |
-
messages = [
|
76 |
-
{"role": "system", "content": system_message},
|
77 |
-
{"role": "user", "content": user_message}
|
78 |
-
]
|
79 |
-
|
80 |
-
try:
|
81 |
-
response = hf_client.chat_completion(messages, max_tokens=800, temperature=0.7)
|
82 |
-
return response.choices[0].message.content
|
83 |
-
except Exception as e:
|
84 |
-
return f"์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
85 |
-
|
86 |
-
def analyze_space(url: str):
|
87 |
-
try:
|
88 |
-
# URL์์ space_id ์ถ์ถ
|
89 |
-
space_id = url.split('spaces/')[-1]
|
90 |
-
|
91 |
-
app_content = get_app_py_content(space_id)
|
92 |
-
summary = summarize_code(app_content)
|
93 |
-
analysis = analyze_code(app_content)
|
94 |
-
usage = explain_usage(app_content)
|
95 |
-
|
96 |
-
return summary, analysis, usage, app_content
|
97 |
-
except Exception as e:
|
98 |
-
print(f"Error in analyze_space: {str(e)}")
|
99 |
-
print(traceback.format_exc())
|
100 |
-
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", "", ""
|
101 |
-
|
102 |
-
def create_ui():
|
103 |
-
try:
|
104 |
-
css = """
|
105 |
-
footer {visibility: hidden;}
|
106 |
-
.output-group {
|
107 |
-
border: 1px solid #ddd;
|
108 |
-
border-radius: 5px;
|
109 |
-
padding: 10px;
|
110 |
-
margin-bottom: 20px;
|
111 |
-
}
|
112 |
-
.scroll-lock {
|
113 |
-
overflow: auto !important;
|
114 |
-
max-height: 400px !important;
|
115 |
-
}
|
116 |
-
"""
|
117 |
-
|
118 |
-
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
119 |
-
gr.Markdown("# HuggingFace Space Analyzer")
|
120 |
-
|
121 |
-
with gr.Row():
|
122 |
-
with gr.Column(scale=6): # ์ผ์ชฝ 60%
|
123 |
-
url_input = gr.Textbox(label="HuggingFace Space URL")
|
124 |
-
analyze_button = gr.Button("๋ถ์")
|
125 |
-
|
126 |
-
with gr.Group(elem_classes="output-group scroll-lock"):
|
127 |
-
summary_output = gr.Textbox(label="์์ฝ (3์ค ์ด๋ด)", lines=3)
|
128 |
-
|
129 |
-
with gr.Group(elem_classes="output-group scroll-lock"):
|
130 |
-
analysis_output = gr.Textbox(label="๋ถ์", lines=15)
|
131 |
-
|
132 |
-
with gr.Group(elem_classes="output-group scroll-lock"):
|
133 |
-
usage_output = gr.Textbox(label="์ฌ์ฉ๋ฒ", lines=10)
|
134 |
-
|
135 |
-
with gr.Column(scale=4): # ์ค๋ฅธ์ชฝ 40%
|
136 |
-
with gr.Group(elem_classes="output-group scroll-lock"):
|
137 |
-
app_py_content = gr.Code(language="python", label="๋ฉ์ธ ์์ค์ฝ๋", lines=30)
|
138 |
-
|
139 |
-
analyze_button.click(
|
140 |
-
analyze_space,
|
141 |
-
inputs=[url_input],
|
142 |
-
outputs=[summary_output, analysis_output, usage_output, app_py_content]
|
143 |
-
)
|
144 |
-
|
145 |
-
return demo
|
146 |
-
|
147 |
-
except Exception as e:
|
148 |
-
print(f"Error in create_ui: {str(e)}")
|
149 |
-
print(traceback.format_exc())
|
150 |
-
raise
|
151 |
-
|
152 |
-
if __name__ == "__main__":
|
153 |
-
try:
|
154 |
-
demo = create_ui()
|
155 |
-
demo.launch()
|
156 |
-
except Exception as e:
|
157 |
-
print(f"Error in main: {str(e)}")
|
158 |
-
print(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|