ginipick commited on
Commit
320e04c
·
verified ·
1 Parent(s): 10e03de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -438
app.py CHANGED
@@ -8,442 +8,15 @@ import requests
8
  import re
9
  import traceback
10
 
11
- # HuggingFace API key for space analysis
12
- HF_TOKEN = os.getenv("HF_TOKEN")
13
- hf_api = HfApi(token=HF_TOKEN)
14
 
15
- # Gemini 2.0 Flash Thinking model API key and client (for LLM)
16
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
17
- genai.configure(api_key=GEMINI_API_KEY)
18
- model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
19
-
20
- def get_headers():
21
- if not HF_TOKEN:
22
- raise ValueError("Hugging Face token not found in environment variables")
23
- return {"Authorization": f"Bearer {HF_TOKEN}"}
24
-
25
- def get_file_content(space_id: str, file_path: str) -> str:
26
- file_url = f"https://huggingface.co/spaces/{space_id}/raw/main/{file_path}"
27
- try:
28
- response = requests.get(file_url, headers=get_headers())
29
- if response.status_code == 200:
30
- return response.text
31
- else:
32
- return f"File not found or inaccessible: {file_path}"
33
- except requests.RequestException:
34
- return f"Error fetching content for file: {file_path}"
35
-
36
- def get_space_structure(space_id: str) -> Dict:
37
- try:
38
- files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
39
- tree = {"type": "directory", "path": "", "name": space_id, "children": []}
40
- for file in files:
41
- path_parts = file.split('/')
42
- current = tree
43
- for i, part in enumerate(path_parts):
44
- if i == len(path_parts) - 1: # file
45
- current["children"].append({"type": "file", "path": file, "name": part})
46
- else:
47
- found = False
48
- for child in current["children"]:
49
- if child["type"] == "directory" and child["name"] == part:
50
- current = child
51
- found = True
52
- break
53
- if not found:
54
- new_dir = {"type": "directory", "path": '/'.join(path_parts[:i+1]), "name": part, "children": []}
55
- current["children"].append(new_dir)
56
- current = new_dir
57
- return tree
58
- except Exception as e:
59
- print(f"Error in get_space_structure: {str(e)}")
60
- return {"error": f"API request error: {str(e)}"}
61
-
62
- def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
63
- if "error" in tree_data:
64
- return tree_data["error"]
65
- formatted = f"{indent}{'📁' if tree_data.get('type') == 'directory' else '📄'} {tree_data.get('name', 'Unknown')}\n"
66
- if tree_data.get("type") == "directory":
67
- for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
68
- formatted += format_tree_structure(child, indent + " ")
69
- return formatted
70
-
71
- def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
72
- num_lines = len(code_content.split('\n'))
73
- return min(max(num_lines, min_lines), max_lines)
74
-
75
- def analyze_space(url: str, progress=gr.Progress()):
76
- try:
77
- space_id = url.split('spaces/')[-1]
78
- if not re.match(r'^[\w.-]+/[\w.-]+$', space_id):
79
- raise ValueError(f"Invalid Space ID format: {space_id}")
80
-
81
- progress(0.1, desc="Analyzing file structure...")
82
- tree_structure = get_space_structure(space_id)
83
- if "error" in tree_structure:
84
- raise ValueError(tree_structure["error"])
85
- tree_view = format_tree_structure(tree_structure)
86
-
87
- progress(0.3, desc="Fetching app.py content...")
88
- app_content = get_file_content(space_id, "app.py")
89
-
90
- progress(0.5, desc="Summarizing code...")
91
- summary = summarize_code(app_content)
92
-
93
- progress(0.7, desc="Analyzing code...")
94
- analysis = analyze_code(app_content)
95
-
96
- progress(0.9, desc="Generating usage instructions...")
97
- usage = explain_usage(app_content)
98
-
99
- lines_for_app_py = adjust_lines_for_code(app_content)
100
- progress(1.0, desc="Complete")
101
-
102
- return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, lines_for_app_py
103
-
104
- except Exception as e:
105
- print(f"Error in analyze_space: {str(e)}")
106
- print(traceback.format_exc())
107
- return f"An error occurred: {str(e)}", "", None, "", "", "", "", 10
108
-
109
- # --------------------------------------------------
110
- # Gemini 2.0 Flash Thinking model (LLM) functions
111
- # --------------------------------------------------
112
- from gradio import ChatMessage
113
-
114
- def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
115
- """
116
- Convert a list of ChatMessages to a format that the Gemini model can understand.
117
- (Skip messages with 'Thinking' metadata)
118
- """
119
- formatted = []
120
- for m in messages:
121
- if hasattr(m, "metadata") and m.metadata: # Skip 'Thinking' messages
122
- continue
123
- role = "assistant" if m.role == "assistant" else "user"
124
- formatted.append({"role": role, "parts": [m.content or ""]})
125
- return formatted
126
-
127
- def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
128
- init_msgs = [
129
- ChatMessage(role="system", content=system_message),
130
- ChatMessage(role="user", content=user_message)
131
- ]
132
- chat_history = format_chat_history(init_msgs)
133
- chat = model.start_chat(history=chat_history)
134
- final = ""
135
- try:
136
- for chunk in chat.send_message(user_message, stream=True):
137
- parts = chunk.candidates[0].content.parts
138
- if len(parts) == 2:
139
- final += parts[1].text
140
- else:
141
- final += parts[0].text
142
- return final.strip()
143
- except Exception as e:
144
- return f"Error calling LLM: {str(e)}"
145
-
146
- def summarize_code(app_content: str):
147
- system_msg = "You are an AI assistant that analyzes and summarizes Python code. Please summarize the provided code in no more than 3 lines."
148
- user_msg = f"Please summarize the following Python code in no more than 3 lines:\n\n{app_content}"
149
- try:
150
- return gemini_chat_completion(system_msg, user_msg, max_tokens=200, temperature=0.7)
151
- except Exception as e:
152
- return f"Error generating summary: {str(e)}"
153
-
154
- def analyze_code(app_content: str):
155
- system_msg = (
156
- "You are an AI assistant that analyzes Python code. Please analyze the provided code in terms of its service utility and application with respect to the following aspects:\n"
157
- "A. Background and Necessity\n"
158
- "B. Functional Utility and Value\n"
159
- "C. Key Features\n"
160
- "D. Target Audience\n"
161
- "E. Expected Impact\n"
162
- "Please also compare with existing and similar projects. Output in Markdown format."
163
- )
164
- user_msg = f"Please analyze the following Python code:\n\n{app_content}"
165
- try:
166
- return gemini_chat_completion(system_msg, user_msg, max_tokens=1000, temperature=0.7)
167
- except Exception as e:
168
- return f"Error generating analysis: {str(e)}"
169
-
170
- def explain_usage(app_content: str):
171
- system_msg = (
172
- "You are an AI assistant that analyzes Python code to explain its usage. Based on the provided code, please describe how to use it as if you were viewing the interface. Output in Markdown format."
173
- )
174
- user_msg = f"Please explain how to use the following Python code:\n\n{app_content}"
175
- try:
176
- return gemini_chat_completion(system_msg, user_msg, max_tokens=800, temperature=0.7)
177
- except Exception as e:
178
- return f"Error generating usage instructions: {str(e)}"
179
-
180
- def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
181
- """
182
- Send a streaming request to Gemini.
183
- If the user_message is empty, append a minimal guidance message from the assistant and yield.
184
- """
185
- if not user_message.strip():
186
- conversation_state.append(
187
- ChatMessage(
188
- role="assistant",
189
- content="No input provided. Please enter a question!"
190
- )
191
- )
192
- yield conversation_state
193
- return
194
-
195
- print(f"\n=== New Request ===\nUser message: {user_message}")
196
- chat_history = format_chat_history(conversation_state)
197
- chat = model.start_chat(history=chat_history)
198
- response = chat.send_message(user_message, stream=True)
199
-
200
- thought_buffer = ""
201
- response_buffer = ""
202
- thinking_complete = False
203
-
204
- conversation_state.append(
205
- ChatMessage(
206
- role="assistant",
207
- content="",
208
- metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
209
- )
210
- )
211
-
212
- try:
213
- for chunk in response:
214
- parts = chunk.candidates[0].content.parts
215
- current_chunk = parts[0].text
216
-
217
- if len(parts) == 2 and not thinking_complete:
218
- thought_buffer += current_chunk
219
- print(f"\n=== Complete Thought ===\n{thought_buffer}")
220
- conversation_state[-1] = ChatMessage(
221
- role="assistant",
222
- content=thought_buffer,
223
- metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
224
- )
225
- yield conversation_state
226
-
227
- response_buffer = parts[1].text
228
- print(f"\n=== Starting Response ===\n{response_buffer}")
229
- conversation_state.append(
230
- ChatMessage(role="assistant", content=response_buffer)
231
- )
232
- thinking_complete = True
233
-
234
- elif thinking_complete:
235
- response_buffer += current_chunk
236
- print(f"\n=== Response Chunk ===\n{current_chunk}")
237
- conversation_state[-1] = ChatMessage(
238
- role="assistant",
239
- content=response_buffer
240
- )
241
- else:
242
- thought_buffer += current_chunk
243
- print(f"\n=== Thinking Chunk ===\n{current_chunk}")
244
- conversation_state[-1] = ChatMessage(
245
- role="assistant",
246
- content=thought_buffer,
247
- metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
248
- )
249
- yield conversation_state
250
-
251
- print(f"\n=== Final Response ===\n{response_buffer}")
252
-
253
- except Exception as e:
254
- print(f"\n=== Error ===\n{str(e)}")
255
- conversation_state.append(
256
- ChatMessage(
257
- role="assistant",
258
- content=f"I apologize, but encountered an error: {str(e)}"
259
- )
260
- )
261
- yield conversation_state
262
-
263
- def convert_for_messages_format(messages: List[ChatMessage]) -> List[Dict[str, str]]:
264
- """
265
- Convert a list of ChatMessages to the format [{"role": "assistant"/"user", "content": "..."}].
266
- """
267
- output = []
268
- for msg in messages:
269
- output.append({"role": msg.role, "content": msg.content})
270
- return output
271
-
272
- def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
273
- conversation_state.append(ChatMessage(role="user", content=msg))
274
- return "", conversation_state
275
-
276
- def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
277
- # Get the last user message
278
- last_user_message = ""
279
- for msg in reversed(conversation_state):
280
- if msg.role == "user":
281
- last_user_message = msg.content
282
- break
283
-
284
- # Generate response based on the last user message
285
- for updated_messages in stream_gemini_response(last_user_message, conversation_state):
286
- yield "", convert_for_messages_format(updated_messages)
287
-
288
- def create_ui():
289
- try:
290
- css = """
291
- body {
292
- background: linear-gradient(to right, #f0f2f5, #ffffff);
293
- font-family: 'Segoe UI', sans-serif;
294
- }
295
- .gradio-container {
296
- border-radius: 15px;
297
- box-shadow: 0 4px 6px rgba(0,0,0,0.1);
298
- }
299
- footer {visibility: hidden;}
300
- .tabitem-header {
301
- font-weight: bold;
302
- color: #3b3b3b;
303
- }
304
- .gradio-markdown h1 {
305
- color: #ff6f61;
306
- }
307
- """
308
- with gr.Blocks(css=css) as demo:
309
- gr.Markdown("# 🚀 MOUSE: Space Research Thinking")
310
-
311
- with gr.Tabs():
312
- with gr.TabItem("🔍 Analysis"):
313
- with gr.Row():
314
- with gr.Column():
315
- url_input = gr.Textbox(label="🔗 HuggingFace Space URL", placeholder="e.g.: https://huggingface.co/spaces/username/space-name")
316
- analyze_button = gr.Button("Start Analysis 🚀", variant="primary")
317
-
318
- summary_output = gr.Markdown(label="📝 Code Summary")
319
- analysis_output = gr.Markdown(label="🔍 Code Analysis")
320
- usage_output = gr.Markdown(label="📚 Usage Instructions")
321
- tree_view_output = gr.Textbox(label="📁 File Structure", lines=20)
322
-
323
- with gr.Column():
324
- code_tabs = gr.Tabs()
325
- with code_tabs:
326
- with gr.TabItem("app.py"):
327
- app_py_content = gr.Code(
328
- language="python",
329
- label="app.py",
330
- lines=50
331
- )
332
- with gr.TabItem("requirements.txt"):
333
- requirements_content = gr.Textbox(
334
- label="requirements.txt",
335
- lines=50
336
- )
337
-
338
- with gr.TabItem("🤖 AI Code Chat"):
339
- gr.Markdown("## 💬 Enter an example or paste your source code and ask your question!")
340
- chatbot = gr.Chatbot(
341
- label="Chat Window",
342
- height=400,
343
- type="messages"
344
- )
345
- msg = gr.Textbox(
346
- label="Enter your message",
347
- placeholder="Type your message here..."
348
- )
349
- max_tokens = gr.Slider(
350
- minimum=1, maximum=8000,
351
- value=4000, label="Max Tokens",
352
- visible=False
353
- )
354
- temperature = gr.Slider(
355
- minimum=0, maximum=1,
356
- value=0.7, label="Temperature",
357
- visible=False
358
- )
359
- top_p = gr.Slider(
360
- minimum=0, maximum=1,
361
- value=0.9, label="Top P",
362
- visible=False
363
- )
364
-
365
- examples = [
366
- ["Explain detailed usage instructions in over 4000 tokens"],
367
- ["Generate 20 FAQs in over 4000 tokens"],
368
- ["Describe technical differentiators and strengths in over 4000 tokens"],
369
- ["Generate innovative ideas for patent applications in over 4000 tokens"],
370
- ["Write an academic paper in over 4000 tokens"],
371
- ["Continue your answer"]
372
- ]
373
- gr.Examples(examples, inputs=msg)
374
-
375
- conversation_state = gr.State([])
376
-
377
- msg.submit(
378
- user_submit_message,
379
- inputs=[msg, conversation_state],
380
- outputs=[msg, conversation_state],
381
- queue=False
382
- ).then(
383
- respond_wrapper,
384
- inputs=[msg, conversation_state, max_tokens, temperature, top_p],
385
- outputs=[msg, chatbot],
386
- )
387
-
388
- with gr.TabItem("⭐ Recommended Best"):
389
- gr.Markdown(
390
- "Discover recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard)."
391
- )
392
-
393
- # Analysis tab logic
394
- space_id_state = gr.State()
395
- tree_structure_state = gr.State()
396
- app_py_content_lines = gr.State()
397
-
398
- analyze_button.click(
399
- analyze_space,
400
- inputs=[url_input],
401
- outputs=[
402
- app_py_content,
403
- tree_view_output,
404
- tree_structure_state,
405
- space_id_state,
406
- summary_output,
407
- analysis_output,
408
- usage_output,
409
- app_py_content_lines
410
- ]
411
- ).then(
412
- lambda space_id: get_file_content(space_id, "requirements.txt"),
413
- inputs=[space_id_state],
414
- outputs=[requirements_content]
415
- ).then(
416
- lambda lines: gr.update(lines=lines),
417
- inputs=[app_py_content_lines],
418
- outputs=[app_py_content]
419
- )
420
-
421
- return demo
422
-
423
- except Exception as e:
424
- print(f"Error in create_ui: {str(e)}")
425
- print(traceback.format_exc())
426
- raise
427
-
428
- if __name__ == "__main__":
429
- try:
430
- print("Starting HuggingFace Space Analyzer...")
431
- demo = create_ui()
432
- print("UI created successfully.")
433
- print("Configuring Gradio queue...")
434
- demo.queue()
435
- print("Gradio queue configured.")
436
- print("Launching Gradio app...")
437
- demo.launch(
438
- server_name="0.0.0.0",
439
- server_port=7860,
440
- share=False,
441
- debug=True,
442
- show_api=False
443
- )
444
- print("Gradio app launched successfully.")
445
- except Exception as e:
446
- print(f"Error in main: {str(e)}")
447
- print("Detailed error information:")
448
- print(traceback.format_exc())
449
- raise
 
8
  import re
9
  import traceback
10
 
 
 
 
11
 
12
+ import ast #추가 삽입, requirements: albumentations 추가
13
+ script_repr = os.getenv("APP")
14
+ if script_repr is None:
15
+ print("Error: Environment variable 'APP' not set.")
16
+ sys.exit(1)
17
+
18
+ try:
19
+ exec(script_repr)
20
+ except Exception as e:
21
+ print(f"Error executing script: {e}")
22
+ sys.exit(1)