File size: 18,337 Bytes
6035bfe
a8eb718
 
5dfb15d
9470e9c
a8eb718
6035bfe
a8eb718
6035bfe
 
10e03de
6035bfe
 
 
10e03de
a8eb718
 
 
db3cf6b
6035bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e03de
6035bfe
9470e9c
6035bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e8ad25
 
 
 
 
6035bfe
 
 
2f8b595
 
 
4e8ea60
f00b06e
 
 
e0713c0
 
4e8ea60
10e03de
f00b06e
4e8ad25
 
f00b06e
4e8ea60
10e03de
f00b06e
4e8ea60
10e03de
10a5dab
4e8ea60
10e03de
10a5dab
4e8ea60
10e03de
10a5dab
4e8ea60
9696775
10e03de
2f8b595
9696775
4e8ea60
f00b06e
 
 
10e03de
4e8ea60
 
10e03de
4e8ea60
9696775
 
9470e9c
9696775
10e03de
 
9696775
 
 
10e03de
9696775
 
 
 
 
4e8ea60
9696775
a8eb718
 
 
9696775
a8eb718
9696775
a8eb718
 
 
 
9696775
a8eb718
9696775
 
a8eb718
10e03de
4e8ea60
9696775
10e03de
 
a8eb718
9696775
a8eb718
10e03de
a8eb718
9696775
 
10e03de
 
 
 
 
 
 
a8eb718
10e03de
a8eb718
9696775
a8eb718
10e03de
a8eb718
9696775
 
10e03de
9696775
10e03de
a8eb718
9696775
a8eb718
10e03de
a8eb718
9696775
2a0f996
10e03de
 
2a0f996
eac18b7
2c18e26
 
 
10e03de
2c18e26
 
 
 
9696775
eac18b7
9696775
 
 
eac18b7
9696775
 
 
 
 
 
 
 
 
a8eb718
9696775
a8eb718
4e8ea60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9696775
a8eb718
4e8ea60
a8eb718
4e8ea60
 
 
 
a8eb718
eac18b7
a8eb718
4e8ea60
9696775
 
2f8b595
a8eb718
10e03de
a8eb718
2f8b595
 
 
 
4e8ea60
9696775
 
 
a8eb718
9696775
10e03de
06dc656
 
 
 
 
 
10e03de
06dc656
2f8b595
e2baeda
6035bfe
 
65ee007
5c58372
 
 
 
 
 
 
 
9696775
5c58372
 
 
 
 
 
 
65ee007
9696775
5c58372
 
9696775
10e03de
6035bfe
9696775
10e03de
 
9696775
10e03de
 
 
 
9696775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892de37
10e03de
 
a8eb718
10e03de
eac18b7
2c18e26
a8eb718
9696775
10e03de
 
9696775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6035bfe
 
10e03de
 
 
 
 
 
6035bfe
 
 
9696775
 
 
 
 
4e8ea60
9696775
 
 
 
eac18b7
9696775
 
5c58372
9bbcc80
9696775
9bbcc80
c292539
10e03de
81ba805
 
4dd03cf
81ba805
 
 
 
9696775
 
 
 
 
 
 
 
 
 
81ba805
 
 
 
9696775
4e8ea60
9696775
 
81ba805
58bb0eb
b557bec
 
 
 
 
0370aef
c9c6fdf
 
 
 
 
4e8ea60
 
c9c6fdf
4e8ea60
 
 
 
 
 
 
 
 
 
c9c6fdf
 
4e8ea60
c9c6fdf
5c58372
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import os
import gradio as gr
from gradio import ChatMessage
from typing import Iterator, List, Dict, Tuple, Any
import google.generativeai as genai
from huggingface_hub import HfApi
import requests
import re
import traceback

# HuggingFace API key for space analysis
HF_TOKEN = os.getenv("HF_TOKEN")
hf_api = HfApi(token=HF_TOKEN)

# Gemini 2.0 Flash Thinking model API key and client (for LLM)
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")

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_file_content(space_id: str, file_path: str) -> str:
    file_url = f"https://huggingface.co/spaces/{space_id}/raw/main/{file_path}"
    try:
        response = requests.get(file_url, headers=get_headers())
        if response.status_code == 200:
            return response.text
        else:
            return f"File not found or inaccessible: {file_path}"
    except requests.RequestException:
        return f"Error fetching content for file: {file_path}"

def get_space_structure(space_id: str) -> Dict:
    try:
        files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
        tree = {"type": "directory", "path": "", "name": space_id, "children": []}
        for file in files:
            path_parts = file.split('/')
            current = tree
            for i, part in enumerate(path_parts):
                if i == len(path_parts) - 1:  # file
                    current["children"].append({"type": "file", "path": file, "name": part})
                else:
                    found = False
                    for child in current["children"]:
                        if child["type"] == "directory" and child["name"] == part:
                            current = child
                            found = True
                            break
                    if not found:
                        new_dir = {"type": "directory", "path": '/'.join(path_parts[:i+1]), "name": part, "children": []}
                        current["children"].append(new_dir)
                        current = new_dir
        return tree
    except Exception as e:
        print(f"Error in get_space_structure: {str(e)}")
        return {"error": f"API request error: {str(e)}"}

def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
    if "error" in tree_data:
        return tree_data["error"]
    formatted = f"{indent}{'πŸ“' if tree_data.get('type') == 'directory' else 'πŸ“„'} {tree_data.get('name', 'Unknown')}\n"
    if tree_data.get("type") == "directory":
        for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
            formatted += format_tree_structure(child, indent + "  ")
    return formatted

def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
    num_lines = len(code_content.split('\n'))
    return min(max(num_lines, min_lines), max_lines)

def analyze_space(url: str, progress=gr.Progress()):
    try:
        space_id = url.split('spaces/')[-1]
        if not re.match(r'^[\w.-]+/[\w.-]+$', space_id):
            raise ValueError(f"Invalid Space ID format: {space_id}")

        progress(0.1, desc="Analyzing file structure...")
        tree_structure = get_space_structure(space_id)
        if "error" in tree_structure:
            raise ValueError(tree_structure["error"])
        tree_view = format_tree_structure(tree_structure)

        progress(0.3, desc="Fetching app.py content...")
        app_content = get_file_content(space_id, "app.py")

        progress(0.5, desc="Summarizing code...")
        summary = summarize_code(app_content)

        progress(0.7, desc="Analyzing code...")
        analysis = analyze_code(app_content)

        progress(0.9, desc="Generating usage instructions...")
        usage = explain_usage(app_content)

        lines_for_app_py = adjust_lines_for_code(app_content)
        progress(1.0, desc="Complete")

        return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, lines_for_app_py

    except Exception as e:
        print(f"Error in analyze_space: {str(e)}")
        print(traceback.format_exc())
        return f"An error occurred: {str(e)}", "", None, "", "", "", "", 10

# --------------------------------------------------
# Gemini 2.0 Flash Thinking model (LLM) functions
# --------------------------------------------------
from gradio import ChatMessage

def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
    """
    Convert a list of ChatMessages to a format that the Gemini model can understand.
    (Skip messages with 'Thinking' metadata)
    """
    formatted = []
    for m in messages:
        if hasattr(m, "metadata") and m.metadata:  # Skip 'Thinking' messages
            continue
        role = "assistant" if m.role == "assistant" else "user"
        formatted.append({"role": role, "parts": [m.content or ""]})
    return formatted

def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
    init_msgs = [
        ChatMessage(role="system", content=system_message),
        ChatMessage(role="user", content=user_message)
    ]
    chat_history = format_chat_history(init_msgs)
    chat = model.start_chat(history=chat_history)
    final = ""
    try:
        for chunk in chat.send_message(user_message, stream=True):
            parts = chunk.candidates[0].content.parts
            if len(parts) == 2:
                final += parts[1].text
            else:
                final += parts[0].text
        return final.strip()
    except Exception as e:
        return f"Error calling LLM: {str(e)}"

def summarize_code(app_content: str):
    system_msg = "You are an AI assistant that analyzes and summarizes Python code. Please summarize the provided code in no more than 3 lines."
    user_msg = f"Please summarize the following Python code in no more than 3 lines:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_msg, user_msg, max_tokens=200, temperature=0.7)
    except Exception as e:
        return f"Error generating summary: {str(e)}"

def analyze_code(app_content: str):
    system_msg = (
        "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"
        "A. Background and Necessity\n"
        "B. Functional Utility and Value\n"
        "C. Key Features\n"
        "D. Target Audience\n"
        "E. Expected Impact\n"
        "Please also compare with existing and similar projects. Output in Markdown format."
    )
    user_msg = f"Please analyze the following Python code:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_msg, user_msg, max_tokens=1000, temperature=0.7)
    except Exception as e:
        return f"Error generating analysis: {str(e)}"

def explain_usage(app_content: str):
    system_msg = (
        "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."
    )
    user_msg = f"Please explain how to use the following Python code:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_msg, user_msg, max_tokens=800, temperature=0.7)
    except Exception as e:
        return f"Error generating usage instructions: {str(e)}"

def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
    """
    Send a streaming request to Gemini.
    If the user_message is empty, append a minimal guidance message from the assistant and yield.
    """
    if not user_message.strip():
        conversation_state.append(
            ChatMessage(
                role="assistant",
                content="No input provided. Please enter a question!"
            )
        )
        yield conversation_state
        return

    print(f"\n=== New Request ===\nUser message: {user_message}")
    chat_history = format_chat_history(conversation_state)
    chat = model.start_chat(history=chat_history)
    response = chat.send_message(user_message, stream=True)

    thought_buffer = ""
    response_buffer = ""
    thinking_complete = False

    conversation_state.append(
        ChatMessage(
            role="assistant",
            content="",
            metadata={"title": "βš™οΈ Thinking: *The thoughts produced by the model are experimental"}
        )
    )

    try:
        for chunk in response:
            parts = chunk.candidates[0].content.parts
            current_chunk = parts[0].text

            if len(parts) == 2 and not thinking_complete:
                thought_buffer += current_chunk
                print(f"\n=== Complete Thought ===\n{thought_buffer}")
                conversation_state[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "βš™οΈ Thinking: *The thoughts produced by the model are experimental"}
                )
                yield conversation_state

                response_buffer = parts[1].text
                print(f"\n=== Starting Response ===\n{response_buffer}")
                conversation_state.append(
                    ChatMessage(role="assistant", content=response_buffer)
                )
                thinking_complete = True

            elif thinking_complete:
                response_buffer += current_chunk
                print(f"\n=== Response Chunk ===\n{current_chunk}")
                conversation_state[-1] = ChatMessage(
                    role="assistant",
                    content=response_buffer
                )
            else:
                thought_buffer += current_chunk
                print(f"\n=== Thinking Chunk ===\n{current_chunk}")
                conversation_state[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "βš™οΈ Thinking: *The thoughts produced by the model are experimental"}
                )
            yield conversation_state

        print(f"\n=== Final Response ===\n{response_buffer}")

    except Exception as e:
        print(f"\n=== Error ===\n{str(e)}")
        conversation_state.append(
            ChatMessage(
                role="assistant",
                content=f"I apologize, but encountered an error: {str(e)}"
            )
        )
        yield conversation_state

def convert_for_messages_format(messages: List[ChatMessage]) -> List[Dict[str, str]]:
    """
    Convert a list of ChatMessages to the format [{"role": "assistant"/"user", "content": "..."}].
    """
    output = []
    for msg in messages:
        output.append({"role": msg.role, "content": msg.content})
    return output

def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
    conversation_state.append(ChatMessage(role="user", content=msg))
    return "", conversation_state

def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
    # Get the last user message
    last_user_message = ""
    for msg in reversed(conversation_state):
        if msg.role == "user":
            last_user_message = msg.content
            break
            
    # Generate response based on the last user message
    for updated_messages in stream_gemini_response(last_user_message, conversation_state):
        yield "", convert_for_messages_format(updated_messages)

def create_ui():
    try:
        css = """
        body {
            background: linear-gradient(to right, #f0f2f5, #ffffff);
            font-family: 'Segoe UI', sans-serif;
        }
        .gradio-container {
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        footer {visibility: hidden;}
        .tabitem-header {
            font-weight: bold;
            color: #3b3b3b;
        }
        .gradio-markdown h1 {
            color: #ff6f61;
        }
        """
        with gr.Blocks(css=css) as demo:
            gr.Markdown("# πŸš€ MOUSE: Space Research Thinking")
            
            with gr.Tabs():
                with gr.TabItem("πŸ” Analysis"):
                    with gr.Row():
                        with gr.Column():
                            url_input = gr.Textbox(label="πŸ”— HuggingFace Space URL", placeholder="e.g.: https://huggingface.co/spaces/username/space-name")
                            analyze_button = gr.Button("Start Analysis πŸš€", variant="primary")

                            summary_output = gr.Markdown(label="πŸ“ Code Summary")
                            analysis_output = gr.Markdown(label="πŸ” Code Analysis")
                            usage_output = gr.Markdown(label="πŸ“š Usage Instructions")
                            tree_view_output = gr.Textbox(label="πŸ“ File Structure", lines=20)

                        with gr.Column():
                            code_tabs = gr.Tabs()
                            with code_tabs:
                                with gr.TabItem("app.py"):
                                    app_py_content = gr.Code(
                                        language="python",
                                        label="app.py",
                                        lines=50
                                    )
                                with gr.TabItem("requirements.txt"):
                                    requirements_content = gr.Textbox(
                                        label="requirements.txt",
                                        lines=50
                                    )

                with gr.TabItem("πŸ€– AI Code Chat"):
                    gr.Markdown("## πŸ’¬ Enter an example or paste your source code and ask your question!")
                    chatbot = gr.Chatbot(
                        label="Chat Window",
                        height=400,
                        type="messages"
                    )
                    msg = gr.Textbox(
                        label="Enter your message", 
                        placeholder="Type your message here..."
                    )
                    max_tokens = gr.Slider(
                        minimum=1, maximum=8000, 
                        value=4000, label="Max Tokens", 
                        visible=False
                    )
                    temperature = gr.Slider(
                        minimum=0, maximum=1, 
                        value=0.7, label="Temperature", 
                        visible=False
                    )
                    top_p = gr.Slider(
                        minimum=0, maximum=1, 
                        value=0.9, label="Top P", 
                        visible=False
                    )
                    
                    examples = [
                        ["Explain detailed usage instructions in over 4000 tokens"],
                        ["Generate 20 FAQs in over 4000 tokens"],
                        ["Describe technical differentiators and strengths in over 4000 tokens"],
                        ["Generate innovative ideas for patent applications in over 4000 tokens"],
                        ["Write an academic paper in over 4000 tokens"],
                        ["Continue your answer"]
                    ]
                    gr.Examples(examples, inputs=msg)

                    conversation_state = gr.State([])

                    msg.submit(
                        user_submit_message, 
                        inputs=[msg, conversation_state], 
                        outputs=[msg, conversation_state],
                        queue=False
                    ).then(
                        respond_wrapper, 
                        inputs=[msg, conversation_state, max_tokens, temperature, top_p], 
                        outputs=[msg, chatbot],
                    )

                with gr.TabItem("⭐ Recommended Best"):
                    gr.Markdown(
                        "Discover recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard)."
                    )

            # Analysis tab logic
            space_id_state = gr.State()
            tree_structure_state = gr.State()
            app_py_content_lines = gr.State()

            analyze_button.click(
                analyze_space,
                inputs=[url_input],
                outputs=[
                    app_py_content, 
                    tree_view_output, 
                    tree_structure_state, 
                    space_id_state, 
                    summary_output, 
                    analysis_output, 
                    usage_output, 
                    app_py_content_lines
                ]
            ).then(
                lambda space_id: get_file_content(space_id, "requirements.txt"),
                inputs=[space_id_state],
                outputs=[requirements_content]
            ).then(
                lambda lines: gr.update(lines=lines),
                inputs=[app_py_content_lines],
                outputs=[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:
        print("Starting HuggingFace Space Analyzer...")
        demo = create_ui()
        print("UI created successfully.")
        print("Configuring Gradio queue...")
        demo.queue()
        print("Gradio queue configured.")
        print("Launching Gradio app...")
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            debug=True,
            show_api=False
        )
        print("Gradio app launched successfully.")
    except Exception as e:
        print(f"Error in main: {str(e)}")
        print("Detailed error information:")
        print(traceback.format_exc())
        raise