|
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: |
|
|
|
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): |
|
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): |
|
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()) |