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