ginipick commited on
Commit
7edd620
ยท
verified ยท
1 Parent(s): 2fb3625

Delete app-backup9.py

Browse files
Files changed (1) hide show
  1. 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())